优化Servlet
目的
减少Servlet的数量,现在是一个功能一个Servlet,将其优化为一个模块一个Servlet,
相当于在数据库中一张表对应一个Servlet,在Servlet中提供不同的方法,完成用户的请求。
BaseServlet
public class BaseServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //System.out.println("baseServlet的service方法被执行了..."); //完成方法分发 //获取请求路径 String uri = req.getRequestURI();// /travel/user/add System.out.println("请求方法"+uri);// /travel/user/add //获取方法名称 String methodName = uri.substring(uri.lastIndexOf('/') + 1); System.out.println("方法名称"+methodName); //获取方法对象Method //谁调用我我代表谁 System.out.println(this); try { //忽略访问权限修饰符,获取方法 Method method = this.getClass().getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); //执行方法 //暴力反射 // method.setAccessible(true);true method.invoke(this,req,resp); } catch (NoSuchMethodException e) { throw new RuntimeException(e); } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } } }
UserServlet
@WebServlet("/user/*") // /user/add /user/find public class UserServlet extends BaseServlet { public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("userServlet的add方法..."); } public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("userServlet的find方法..."); } }
CategoryServlet
@WebServlet("/category/*") public class CategoryServlet extends BaseServlet { public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("CategoryServlet的add方法..."); } public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("CategoryServlet的find方法..."); } }
调用其他方法进行优化
UserServlet
@WebServlet("/user/*") // /user/add /user/find public class UserServlet extends BaseServlet { //声明UserService业务对象 private UserService service = new UserServiceImpl(); /* 注册功能 */ public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证校验 String check = request.getParameter("check"); //从sesion中获取验证码 HttpSession session = request.getSession(); String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER");//为了保证验证码只能使用一次 //比较 if(checkcode_server == null || !checkcode_server.equalsIgnoreCase(check)){ //验证码错误 ResultInfo info = new ResultInfo(); //注册失败 info.setFlag(false); info.setErrorMsg("验证码错误"); //将info对象序列化为json ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; } //1.获取数据 Map<String, String[]> map = request.getParameterMap(); //2.封装对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //3.调用service完成注册 //UserService service = new UserServiceImpl(); boolean flag = service.regist(user); ResultInfo info = new ResultInfo(); //4.响应结果 if(flag){ //注册成功 info.setFlag(true); }else{ //注册失败 info.setFlag(false); info.setErrorMsg("注册失败!"); } //将info对象序列化为json ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); } /* 登录功能 */ public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.获取用户名和密码数据 Map<String, String[]> map = req.getParameterMap(); //2.封装User对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //3.调用Service查询 //UserService service = new UserServiceImpl(); User u = service.login(user); ResultInfo info = new ResultInfo(); //4.判断用户对象是否为null if(u == null){ //用户名密码或错误 info.setFlag(false); info.setErrorMsg("用户名密码或错误"); } //5.判断用户是否激活 if(u != null && !"Y".equals(u.getStatus())){ //用户尚未激活 info.setFlag(false); info.setErrorMsg("您尚未激活,请激活"); } //6.判断登录成功 if(u != null && "Y".equals(u.getStatus())){ req.getSession().setAttribute("user",u);//登录成功标记 //登录成功 info.setFlag(true); } //响应数据 ObjectMapper mapper = new ObjectMapper(); resp.setContentType("application/json;charset=utf-8"); mapper.writeValue(resp.getOutputStream(),info); } /* 查找单个对象功能 */ protected void findOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从session中获取登录用户 Object user = req.getSession().getAttribute("user"); //将user写回客户端 ObjectMapper mapper = new ObjectMapper(); resp.setContentType("application/json;charset=utf-8"); mapper.writeValue(resp.getOutputStream(),user); } /* 推出功能 */ protected void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //1.销毁session req.getSession().invalidate(); //2.跳转登录页面 resp.sendRedirect(req.getContextPath() + "/login.html"); } /* 激活功能 */ protected void active(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取激活码 String code = req.getParameter("code"); if (code != null) { //调用Service完成激活 //UserServiceImpl service = new UserServiceImpl(); boolean flag = service.active(code); //判断标记 String msg = null; if (flag) { //激活成功 msg = "激活成功,请<a href='login.html'>登录</a>"; } else { //激活失败 msg = "激活失败,请联系管理"; } resp.setContentType("text/html;charset=utf-8"); resp.getWriter().write(msg); } } }
标签:info,BaseServlet,Userservlet,resp,req,json,user,933,new From: https://www.cnblogs.com/agzq/p/17167045.html