利用反射实现Servlet公共类的抽取

一次请求的执行过程:

请求:发送请求地址-->到达web.xml中,找到地址对应的servlet类-->通过反射调用该类的构造函数,创建该servlet类的对象-->通过当前对象调用该servlet的init方法-->发现没有-->从其父类HttpServlet找init,还是没有-->再找其父类GenericServlet,有init,调用该init方法加载当前servlet类-->调用当前servlet的service方法-->发现没有,找父类HttpServlet-->父类有该方法,调用它,service中获取请求方式-->然后调用do post()方法,当前servlet的do post()方法-->最后销毁,调用disdroy。嗯,其实调用父类的init,service方法这种说貌似不妥,应该是调用继承自父类的这两个方法

对原本的service方法进行重写,利用反射调用子类的具体执行方法

 1 package com.jixh.ss.web.base;
 2 
 3 import java.io.IOException;
 4 import java.lang.reflect.Method;
 5 
 6 import javax.servlet.ServletException;
 7 import javax.servlet.http.HttpServlet;
 8 import javax.servlet.http.HttpServletRequest;
 9 import javax.servlet.http.HttpServletResponse;
10 
11 public class BaseServlet extends HttpServlet {
12     
13     private static final long serialVersionUID = 1L;
14 
15     @Override
16     public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
17 
18         // localhost:8080/store/productServlet?method=updateUser,url中必须带有所要调用的子类具体方法的方法名
19         String method = req.getParameter("method");
20 
21         if (null == method || "".equals(method) || method.trim().equals("")) {
22             method = "execute";
23         }
24 
25         // 注意:此处的this代表的是子类的对象
26         // System.out.println(this);
27         // 子类对象字节码对象
28         @SuppressWarnings("rawtypes")
29         Class clazz = this.getClass();
30 
31         try {
32             // 查找子类对象对应的字节码中的名称为method的方法.这个方法的参数类型是:HttpServletRequest.class,HttpServletResponse.class
33             @SuppressWarnings("unchecked")
34             Method md = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
35             if(null!=md){
36                 String jspPath = (String)md.invoke(this, req, resp);
37                 if (null != jspPath) {
38                     req.getRequestDispatcher(jspPath).forward(req, resp);
39                 }
40             }
41         } catch (Exception e) {
42             e.printStackTrace();
43         }
44 
45     }
46 
47     // 默认方法
48     public String execute(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
49         return null;
50     }
51 
52 }

 

子类Servlet只进行具体的业务处理

 1 package com.jixh.ss.web.servlet;
 2 
 3 import java.util.List;
 4 
 5 import javax.servlet.http.HttpServletRequest;
 6 import javax.servlet.http.HttpServletResponse;
 7 
 8 import com.jixh.peanut.domian.FirstMenu;
 9 import com.jixh.peanut.domian.User;
10 import com.jixh.peanut.domian.UserPower;
11 import com.jixh.peanut.service.MenueService;
12 import com.jixh.peanut.service.UserService;
13 import com.jixh.peanut.service.impl.MenueServiceImpl;
14 import com.jixh.peanut.service.impl.UserServiceImpl;
15 import com.jixh.peanut.util.DateUtils;
16 import com.jixh.peanut.util.UUIDUtils;
17 import com.jixh.peanut.web.base.BaseServlet;
18 
19 import net.sf.json.JSONObject;
20 
21 
22 public class userServlet extends BaseServlet {
23     private static final long serialVersionUID = 1L;
24 
25 
26     // 修改用户
27     public String updateUser(HttpServletRequest request, HttpServletResponse response) {
28         try {
29             // 允许跨域访问的响应头
30             response.setHeader("Access-Control-Allow-Origin", "*");
31             
32             String username = request.getParameter("username");
33             String password = request.getParameter("password");
34             // 验证用户名密码
35             UserService userService = new UserServiceImpl();
36             User user = userService.userLoging(username, password);
37             if (user != null) {
38                 // 验证通过,进行下一步
39                 String uid = (String) request.getSession().getAttribute("thisUser");
40                 String utype = request.getParameter("utype");
41                 int ustate = Integer.parseInt(request.getParameter("ustate"));
42                 String powerIdArray = request.getParameter("powerIdArray");
43                 System.out.println(uid+"=="+username + "==" + password + "==" + utype + "==" + ustate + "==" + powerIdArray);
44                 //修改用户表
45                 boolean result = userService.updateUser(uid, utype, ustate);
46                 System.out.println("修改用户结果为"+result);
47                 if(result) {
48                     response.getWriter().write("success");
49                 }else {
50                     response.getWriter().write("faild");
51                 }
52             } else {
53                 // 直接响应
54                 response.getWriter().write("error");
55             }
56         } catch (Exception e) {
57             e.printStackTrace();
58         }
59         return null;
60     }
61 }
posted @ 2018-12-12 22:19  jixhua  阅读(619)  评论(0编辑  收藏  举报