BaseServlet抽取
减少Servlet的数量,现在是一个功能一个Servlet,将其优化为一个模块一个Servlet,
相当于在数据库中一张表对应一个Servlet,在Servlet中提供不同的方法,完成用户的请求。
BaseServlet:
public class BaseServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //完成方法分发 //1.获取请求路径 String uri = req.getRequestURI(); System.out.println("请求uri:"+uri); //获取方法名称 String methodName = uri.substring(uri.lastIndexOf('/') + 1); System.out.println("方法名称:"+methodName); //获取方法对象Method //this:谁调用我?我代表谁 try { //获取方法 Method method=this.getClass().getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class); //执行方法 method.invoke(this,req,resp); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
UserServlet:测试
@WebServlet("/user/*") public class UserServlet extends BaseServlet { public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("add..."); } public void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("find..."); } }
访问:
控制台:
UserServlet:把上面写的Servlet全部写到UserServlet这个方法中,复制过来就行
@WebServlet("/user/*") public class UserServlet extends BaseServlet { //声明一个UserService业务对象 private UserServiceImpl service = new UserServiceImpl(); /** * 注册功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void rehist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证码的校验 String check = request.getParameter("check"); //从session中获取验证码 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); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; } //获取数据 Map<String, String[]> map = request.getParameterMap(); //封装对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //调用service完成注册 //UserServiceImpl service = new UserServiceImpl(); boolean flag = service.regist(user); ResultInfo info = new ResultInfo(); //响应结果 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); } /** * 登录功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //验证码的校验 String check = request.getParameter("check"); //从session中获取验证码 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); //将json数据写回客户端 //设置content-type response.setContentType("application/json;charset=utf-8"); response.getWriter().write(json); return; } //获取用户名和密码数据 Map<String, String[]> map = request.getParameterMap(); //封装user对象 User user = new User(); try { BeanUtils.populate(user,map); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } //调用Service查询 //UserServiceImpl service = new UserServiceImpl(); User u = service.login(user); request.getSession().setAttribute("user",u); ResultInfo info = new ResultInfo(); //判断用户对象是否为null if (u==null){ //用户名或密码错误 info.setFlag(false); info.setErrorMsg("用户名或密码错误"); } //判断用户是否激活 if (u!=null && !"Y".equals(u.getStatus())){ //用户尚未激活 info.setFlag(false); info.setErrorMsg("您尚未激活,请激活"); } //判断登录成功 if (u!=null && "Y".equals(u.getStatus())){ //登录成功 info.setFlag(true); } //响应数据 ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),info); } /** * 查询单个对象 * @param request * @param response * @throws ServletException * @throws IOException */ public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session中获取登录用户 Object user = request.getSession().getAttribute("user"); //将user写回客户端 ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),user); } /** * 退出功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //销毁session request.getSession().invalidate(); //跳转登录页面 response.sendRedirect(request.getContextPath()+"/login.html"); } /** * 激活功能 * @param request * @param response * @throws ServletException * @throws IOException */ public void active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //判断激活码 String code = request.getParameter("code"); if (code!=null){ //调用service完成激活 //UserServiceImpl service = new UserServiceImpl(); boolean flag = service.active(code); //判断标记 String msg = null; if (flag){ //激活成功 msg = "激活成功,请<a href='/travel/login.html'>登录</a>"; }else { //激活失败 msg = "激活失败,请联系管理员!"; } response.setContentType("text/html;charset=utf-8"); response.getWriter().write(msg); } } }
login.html:该路径
header.html:
register.html:
UserServiceImpl:regist方法中的路径
优化完成
标签:info,BaseServlet,抽取,request,throws,json,旅游网,new,response From: https://www.cnblogs.com/yuzong/p/17121906.html