有一个抽象MainServlet类,继承了HttpServlet。又写了一个普通类AdminServlet,继承了MainServlet。当请求指向AdminServlet时,又走了MainServlet中的post方法,很是不解啊。。。另外,在post方法中打印this,竟然是AdminServlet的hash值。为什么啊???
这是代码,
MainServlet :
public abstract class MainServlet extends HttpServlet{
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String methodName = req.getParameter("cmd");
if(methodName==null || methodName.trim().equals("")){
methodName="execute";
}
try {
Method method = this.getClass().getMethod(methodName,
HttpServletRequest.class,HttpServletResponse.class);
method.invoke(this,req, resp);
} catch (Exception e) {
throw new RuntimeException(e.getMessage(),e);
}
}
public abstract void execute(HttpServletRequest req,HttpServletResponse resp)
throws Exception;
}
AdminServlet :
public class AdminServlet extends MainServlet {
private AdminService service = new AdminService();
public void execute(HttpServletRequest req, HttpServletResponse resp)
throws Exception {
String name = req.getParameter("name");
String password = req.getParameter("password");
Admin admin = null;
if(admin==null){
System.err.println("wrong");
}
} |