0

这是我试图实现的东西。我已经编写了 doGet 方法,现在如何映射 doPost 方法?

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String forward="";
    String act = request.getParameter("act");
    if (act != null && !act.equalsIgnoreCase("null") &&
           act.equalsIgnoreCase("login")) {
       forward= "/Login.jsp";
    } else if (act!= null && !act.equalsIgnoreCase("null") &&
            act.equalsIgnoreCase("register")) {
        forward = LIST_USER;
        request.setAttribute("users", dao.getAllUsers());
    } else {
        forward = "/Login.jsp";
    }

    RequestDispatcher view = request.getRequestDispatcher(forward);
    view.forward(request, response);
} 
4

3 回答 3

0

这是 Netbeans IDE 生成的默认代码。

将您的代码保留在通用方法中并将其映射到您的调用方法。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
} 

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
} 

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}
于 2013-09-11T06:08:02.497 回答
0

如果你想像 GET 一样处理 POST 你可以做

protected void doPost((HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
}
于 2013-09-10T09:41:24.067 回答
0

如果您想以类似的方式处理 POST 和 GET,那么您可以添加第三种方法

doSomething(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

并从两者中调用它

doGet 和做 Post

例如

doSomething(request,response);
于 2013-09-10T10:02:34.353 回答