利用反射优化Servlet抽象出父类BaseServlet
在编写servlet的时候发现每个servlet里面的doPost方法都如:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }
而在doGet方法中大多是对uri的跳转
(通过在uri后面加上method参数可以定位到不同的方法,这样就可以少写一些servlet了)
如果新增模块,则该模块的Servlet中也同样包含相同的代码,因此,代码过度冗余,需要优化。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取客户端请求的方法名 String methodName = request.getParameter("method"); //根据名称判断调用的方法 if("categoryList".equals(methodName)) { categoryList(request,response); }else if("index".equals(methodName)) { index(request,response); }else if("productInfo".equals(methodName)) { productInfo(request,response); }else if("productList".equals(methodName)) { productList(request,response); } }
抽象出父类BaseServlet,并且在BaseServlet中使用反射机制实现方法的调用
public class BaseServlet extends HttpServlet { private static final long serialVersionUID = 7978401768598506854L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取方法名 String methodName = request.getParameter("method"); //2.得到请求Servlet的字节码 Class clazz = this.getClass(); try { //3.根据字节码和方法名获取方法对象 所有方法参数均为HttpServletRequest和HttpServletResponse类型 Method method = clazz.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.setAccessible(true); //设置方法均直接访问 //4.调用method方法,实现其中的功能 method.invoke(this, request, response); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
然后再写servlet就只用写具体的方法就行了
@WebServlet("/user") @SuppressWarnings("unused") public class UserServlet extends BaseServlet { private static final long serialVersionUID = 7665438418738590582L; private UserService userService = new UserServiceImpl(); //安全退出 private void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.getSession().invalidate(); //涉及 - cookie - 清除cookie response.sendRedirect(request.getContextPath() + "/index"); } //用户登录 private void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); //自动登录 - 记住用户名 User user = userService.login(username, password); if(user != null) { //成功 request.getSession().setAttribute("user", user); response.sendRedirect(request.getContextPath() + "/index"); } else { request.setAttribute("info", "用户名或密码错误!"); request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); } } //email - 账号激活 private void active(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String activeCode = request.getParameter("activeCode"); userService.active(activeCode); request.setAttribute("info", "账号激活成功, 请登录!"); request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); } /** * @Title: register * @Description: 用户注册 */ private void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //调用service Map<String, String[]> paramMap = request.getParameterMap(); userService.register(paramMap); //跳转至首页|登录页面 request.getRequestDispatcher("/WEB-INF/jsp/registerSuccess.jsp").forward(request, response); } }