BeanServlet抽取
优化Servlet
减少servlet的数量 现在是一个功能一个Servlet 将其优化为一个模块一个Servlet,相当于在数据库中一张表对应一个Servlet 在Servlet中提供不同的方法 完成用户的请求
BaseServlet类
package com.bai.web.servlet; import javax.servlet.ServletException; 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; public class BaseServlet extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //完成方法分支 //获取请求路径 String uri = req.getRequestURI(); System.out.println("请求uri"+uri); //获取方法名称 String methodName = uri.substring(uri.lastIndexOf("/") + 1); System.out.println("方法名称"+methodName); //获取方法对象 System.out.println(this); try { //忽略访问权限修饰符 Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class,HttpServletResponse.class); //执行方法 //暴力反射 method.setAccessible(true); method.invoke(this,req,resp); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }
UserServlet类
package com.bai.web.servlet; 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; @WebServlet("/user/*") public class UserServlet extends BaseServlet { protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("add方法执行..."); } protected void find(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("find方法执行..."); } }
运行结果
这里使用的是反射机制
使用这个方法将所有的路径进行改变
将这些类作为方法移动到UserServlet中
代码
package com.bai.web.servlet; import com.bai.domain.ResultInfo; import com.bai.domain.User; import com.bai.service.impl.UserServiceImpl; import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.beanutils.BeanUtils; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.util.Map; @WebServlet("/user/*") public class UserServlet extends BaseServlet { private UserServiceImpl service = new UserServiceImpl(); /** * 注册功能 * @param req * @param resp * @throws ServletException * @throws IOException */ public void regis(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //验证校验 String check = req.getParameter("check"); //从session中获取 HttpSession session = req.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 resp.setContentType("application/json;charset=utf-8"); resp.getWriter().write(json); return; } //获取数据 Map<String, String[]> map = req.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 resp.setContentType("application/json;charset=utf-8"); resp.getWriter().write(json); } /** * 登录页面 * @param req * @param resp * @throws ServletException * @throws IOException */ public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //获取用户名和密码数据 Map<String, String[]> map = req.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); ResultInfo info = new ResultInfo(); //验证校验 String check = req.getParameter("check"); //从session中获取 HttpSession session = req.getSession(); String checkcode_server = (String) session.getAttribute("CHECKCODE_SERVER"); session.removeAttribute("CHECKCODE_SERVER");//保证验证码只能使用一次 //比较 if (checkcode_server == null || !checkcode_server.equalsIgnoreCase(check)) { //验证码错误 //用户名或密码错误 info.setFlag(false); info.setErrorMsg("验证码错误"); //将info对象序列化为json ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(info); //将json数据写回客户端 //设置content-type resp.setContentType("application/json;charset=utf-8"); resp.getWriter().write(json); return; } //判断用户对象是否为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())) { req.getSession().setAttribute("user", u);//登录成功标记 //登录成功 info.setFlag(true); } //响应数据 ObjectMapper mapper = new ObjectMapper(); resp.setContentType("application/json;charset=utf-8"); mapper.writeValue(resp.getOutputStream(), info); } /** *获取用户功能 * @param req * @param resp * @throws ServletException * @throws IOException */ public 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); } /** * 退出功能 * @param req * @param resp * @throws ServletException * @throws IOException */ public void exit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //销毁session req.getSession().invalidate(); //跳转登录页面 resp.sendRedirect(req.getContextPath() + "/login.html"); } /** * 激活码功能 * @param req * @param resp * @throws ServletException * @throws IOException */ public 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); } } }
随后将这些绑定的数据进行更改
注册页面 register.html
登录页面 login.html
首页页面 header.html
退出页面 header.html
激活 UserServiceImpl类中
标签:info,BeanServlet,抽取,resp,req,import,servlet,throws From: https://www.cnblogs.com/aimz01/p/16624450.html