package com.maike.dao; import com.maike.model.User; public interface UserDao { int insert(String userName,String userPwd); User selectByName(String userName); }UserDao
package com.maike.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.maike.service.UserService; import com.maike.service.UserServiceImpl; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { UserService userService = new UserServiceImpl(); private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String userName = request.getParameter("userName"); String uesrPwd = request.getParameter("userPwd"); int k = userService.judgeLogin(userName,uesrPwd); if(k > 0) { request.getRequestDispatcher("success.jsp").forward(request, response); }else { request.getRequestDispatcher("login.jsp").forward(request, response); } } }LoginServlet
package com.maike.controller; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.maike.service.UserService; import com.maike.service.UserServiceImpl; /** * Servlet implementation class RegisterServlet */ @WebServlet("/RegisterServlet") public class RegisterServlet extends HttpServlet { UserService userService = new UserServiceImpl(); private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public RegisterServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub response.getWriter().append("Served at: ").append(request.getContextPath()); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub request.setCharacterEncoding("utf-8"); String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); String userRePwd = request.getParameter("userRePwd"); int k = userService.insertUser(userName,userPwd,userRePwd); if(k > 0) { request.getRequestDispatcher("login.jsp").forward(request, response); }else { request.getRequestDispatcher("register.jsp").forward(request, response); } } }RegisterServle
package com.maike.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import com.maike.model.User; import com.maike.util.ConnectionUtil; public class UserDaoImpl implements UserDao { Connection con = null; PreparedStatement pt = null; ResultSet rs = null; String sql; @Override public int insert(String userName, String userPwd) { // TODO Auto-generated method stub int k = 0; sql = "insert into user value(null,?,?)"; try { con = ConnectionUtil.getConnection(); pt = con.prepareStatement(sql); pt.setString(1, userName); pt.setString(2, userPwd); k = pt.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { ConnectionUtil.closeRe(con, pt); } return k; } @Override public User selectByName(String userName) { // TODO Auto-generated method stub sql = "select * from user where user_name = ?"; User user = new User(); try { con = ConnectionUtil.getConnection(); pt = con.prepareStatement(sql); pt.setString(1, userName); rs = pt.executeQuery(); while(rs.next()) { user.setUserId(rs.getInt("user_id")); user.setUserName(rs.getString("user_name")); user.setUserPwd(rs.getString("user_pwd")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { ConnectionUtil.closeRe(con, pt, rs); } return user; } }UserDaoImpl
标签:总结,userName,String,request,HttpServlet,import,今日,response From: https://www.cnblogs.com/zhaoyueheng/p/17181014.html