多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
BaseServlet.java ~~~ package zyw.web; 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 java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @WebServlet(name = "BaseServlet") public class BaseServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); try { // 1 获取方法名字 String method = req.getParameter("method"); // 2 获取到当前对象的字节码文件 Class clazz=this.getClass(); // 3 拿到字节码对象中的方法 Method clazzMethod = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class); // 4 执行方法 clazzMethod.invoke(this,req,resp); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } ~~~ UserServlet.java ~~~ package zyw.web; import org.apache.commons.beanutils.BeanUtils; import zyw.bean.User; import zyw.service.UserService; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.sql.SQLException; import java.util.Map; @WebServlet(name = "UserServlet",urlPatterns = "/user") public class UserServlet extends BaseServlet { /* protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String method = request.getParameter("method"); if ("login".equals(method)){ login(request,response); }else if("register".equals(method)){ register(request,response); } }*/ public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String password = request.getParameter("password"); UserService userService=new UserService(); User user=null; try { //调用service中登录方法 user = userService.login(name, password); } catch (SQLException e) { e.printStackTrace(); } if (user!=null){ //登录成功后我们再获取是否保存密码的信息,如果失败了保存密码就没有意义了 String remember = request.getParameter("remember"); if (remember.equals("yes")){ // 将用户名和密码加入到cookie中 Cookie nameCookie = new Cookie("name", name); Cookie passwordCookie = new Cookie("password", password); //设置cookie的有效期 防止销毁 nameCookie.setMaxAge(60*10);//10分钟 passwordCookie.setMaxAge(60*10); //将cookie发送给客户端保存 response.addCookie(nameCookie); response.addCookie(passwordCookie); } //登录成功跳转生鲜种类列表界面 response.sendRedirect(request.getContextPath()+"/category-list.jsp"); }else { //登录失败提示 response.setContentType("text/html;charset=utf-8"); response.getWriter().write("用户登录失败"); } } public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); String id = request.getParameter("id"); String password = request.getParameter("password"); String email = request.getParameter("email"); User user=new User(); Map<String,String[]> stringMap =request.getParameterMap(); try { //分别将属性设置到对象中 // BeanUtils.setProperty(user,name,name); //将属性的map集合封装到对象中 BeanUtils.populate(user,stringMap); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } UserService userService=new UserService(); // boolean register = userService.register(user); boolean register = userService.register(user); if (register) { response.sendRedirect(request.getContextPath()+"login.jsp"); }else { response.setContentType("text/html;charset=utf-8"); response.getWriter().write("注册失败"); } } } ~~~ 与上节比较UserServlet.java修改内容(1.父类变成BaseServlet,2.去掉doget与dopost方法,3.方法变为public) ![](https://box.kancloud.cn/d63221bf80152e197f74212b8fd09946_896x341.png) ![](https://box.kancloud.cn/5de42e8b3bdfa9c8104f6c14c29ba321_378x49.png) ![](https://box.kancloud.cn/e4eae6a76a1748e82c6c9f574e611ffb_270x55.png)