代码样例:
注册RegServlet
packagecom.neuedu.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.neuedu.model.service.IUserService;importcom.neuedu.model.service.impl.UserServiceImpl;importcom.neuedu.pojo.User;/**
* Servlet implementation class RegServlet
*/@WebServlet("/regServlet")publicclassRegServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/**
* @see HttpServlet#HttpServlet()
*/publicRegServlet(){super();// TODO Auto-generated constructor stub}/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");//接收用户输入Stringname=request.getParameter("uname");System.out.println(name);Stringpassword=request.getParameter("paw");Stringrepassword=request.getParameter("paw1");Stringemail=request.getParameter("email");StringidCard=request.getParameter("idcard");String[]cookieFlag=request.getParameterValues("CookieYN");System.out.println(cookieFlag);//打包userUseruser=newUser();user.setUserName(name);user.setUserPwd(password);user.setUserPwd1(repassword);user.setEmail(email);user.setIdCard(idCard);//调用service进行处理IUserServiceuserService=newUserServiceImpl();inti=userService.addUser(user);//返回处理结果response.setContentType("text/html;charset=utf-8");PrintWriterout=response.getWriter();if(i>0){out.println("success");}else{out.println("failure");}}}
登录LoginServlet
packagecom.neuedu.servlet;importjava.io.IOException;importjavax.servlet.ServletException;importjavax.servlet.annotation.WebServlet;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.neuedu.model.service.IUserService;importcom.neuedu.model.service.impl.UserServiceImpl;importcom.neuedu.pojo.User;/**
* Servlet implementation class LoginServlet
*/@WebServlet("/loginServlet")publicclassLoginServletextendsHttpServlet{privatestaticfinallongserialVersionUID=1L;/**
* @see HttpServlet#HttpServlet()
*/publicLoginServlet(){super();// TODO Auto-generated constructor stub}/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{doPost(request,response);}/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/protectedvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{request.setCharacterEncoding("utf-8");response.setCharacterEncoding("utf-8");//获取页面输入的用户名和密码Stringusername=request.getParameter("username");Stringpassword=request.getParameter("password");Useruser=newUser();user.setUserName(username);user.setUserPwd(password);IUserServiceservice=newUserServiceImpl();Userdbuser=service.getUser(user);if(dbuser!=null){//如果数据库里有此用户System.out.println("回首页");//判断是普通用户还是管理员//如果是普通用户,进首页if(dbuser.getPower()==0){//index.htmlresponse.sendRedirect(request.getContextPath()+"/user/index.html");}else{//如果是管理员,进管理员首页//admin/index.htmlresponse.sendRedirect(request.getContextPath()+"/admin/index.html");}}else{//回登录页重新登录request.getRequestDispatcher("/login.html").forward(request,response);}}}
接口
publicinterfaceIUserService{intaddUser(Useruser);UsergetUser(Useruser);}publicinterfaceIUserDao{publicintadd(Useruser);UsergetUser(Useruser);}
相关service
packagecom.neuedu.model.service.impl;importcom.neuedu.model.dao.IUserDao;importcom.neuedu.model.dao.impl.UserDaoImpl;importcom.neuedu.model.service.IUserService;importcom.neuedu.pojo.User;publicclassUserServiceImplimplementsIUserService{IUserDaouserDao=newUserDaoImpl();@OverridepublicintaddUser(Useruser){inti=0;//验证合法性//比如用户名是否重复//两次密码是否一致。。。。。。//如果验证通过,就存库Userdbuser=userDao.getUser(user);//调用dao存库if(dbuser!=null){//已经被注册过了return0;}else{i=userDao.add(user);}returni;}@OverridepublicUsergetUser(Useruser){returnuserDao.getUser(user);}}
dao:
packagecom.neuedu.model.dao.impl;importjava.sql.Connection;importjava.sql.PreparedStatement;importjava.sql.ResultSet;importjava.sql.SQLException;importcom.neuedu.model.dao.IUserDao;importcom.neuedu.pojo.User;importcom.neuedu.utils.DBUtil;publicclassUserDaoImplimplementsIUserDao{PreparedStatementps=null;Connectionconn=null;ResultSetrs=null;@Overridepublicintadd(Useruser){inti=0;try{conn=DBUtil.getConnection();ps=conn.prepareStatement("INSERT INTO t_user VALUES(NULL,?,?,?,?,?)");ps.setString(1,user.getUserName());ps.setString(2,user.getUserPwd());ps.setString(3,user.getEmail());ps.setString(4,user.getIdCard());ps.setInt(5,user.getPower());i=ps.executeUpdate();}catch(SQLExceptione){e.printStackTrace();}returni;}@OverridepublicUsergetUser(Useruser){Userdbuser=null;conn=DBUtil.getConnection();try{ps=conn.prepareStatement("select * from t_user where user_name=? and user_pwd=?");ps.setString(1,user.getUserName());ps.setString(2,user.getUserPwd());rs=ps.executeQuery();if(rs.next()){//如果有该用户dbuser=newUser();dbuser.setId(rs.getInt("user_id"));dbuser.setUserName(rs.getString("user_name"));dbuser.setUserPwd(rs.getString("user_pwd"));dbuser.setEmail(rs.getString("user_email"));dbuser.setIdCard(rs.getString("user_idcard"));dbuser.setPower(rs.getInt("user_power"));}}catch(SQLExceptione){e.printStackTrace();}returndbuser;}}
2人点赞
java web
转至:↓
链接:https://www.jianshu.com/p/e1e458b75001
来源:
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。