public class BaseServlet extends HttpServlet{
resp.setContentType("text/html;charset=utf-8");
//获得方法名字是什么
String methodName = req.getParameter("method");
//System.out.println(methodName);
if(methodName==null || "".equals(methodName)){
resp.getWriter().println("method的参数为空");
return;
}
//谁过来就得到谁的class
Class clazz = this.getClass();
try {
//得到这个方法
Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
//执行这个方法
String path = (String)method.invoke(this, req,resp);
if(path != null){
req.getRequestDispatcher(path).forward(req, resp);
}
//System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");resp.setContentType("text/html;charset=utf-8");
//获得方法名字是什么
String methodName = req.getParameter("method");
//System.out.println(methodName);
if(methodName==null || "".equals(methodName)){
resp.getWriter().println("method的参数为空");
return;
}
//谁过来就得到谁的class
Class clazz = this.getClass();
try {
//得到这个方法
Method method = clazz.getMethod(methodName,HttpServletRequest.class,HttpServletResponse.class);
//执行这个方法
String path = (String)method.invoke(this, req,resp);
if(path != null){
req.getRequestDispatcher(path).forward(req, resp);
}
//System.out.println(path);
} catch (Exception e) {
e.printStackTrace();
}
}
}