public class BaseServlet extends HttpServlet {
@Override
public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得method参数
String methodName = req.getParameter("method");
//分析出 传递的参数 就是方法名称 Class --- > Method/Field
Class clazz = this.getClass();//??? this代表真正被调用的对象
//获得被访问的对象中的对用的方法
Method method;
try {
method = clazz.getMethod(methodName, HttpServletRequest.class,HttpServletResponse.class);
//执行方法
String redirectView = (String) method.invoke(this, req,resp);
//抽取重定向代码
if(redirectView!=null){
resp.sendRedirect(req.getContextPath()+"/"+redirectView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
baseServlet抽取
最新推荐文章于 2022-05-25 21:18:57 发布