0

This is something im trying to implement. I have written the doGet method , how do i map the doPost method now ?

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);
} 
Stewart
  • 17,616
  • 8
  • 52
  • 80
user2749258
  • 11
  • 1
  • 6
  • 2
    I don't understand what you're asking. What do you mean with "map the doPost method"? Also, please indent your code properly. – JB Nizet Sep 10 '13 at 09:39

3 Answers3

0

If you want to handle POST just like GET you could do

protected void doPost((HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doGet(request,response);
}
piet.t
  • 11,718
  • 21
  • 43
  • 52
0

if you want to treat POST and GET in similar way then you can add a third method

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

and call it from both

doGet and do Post

eg

doSomething(request,response);
ankit
  • 4,919
  • 7
  • 38
  • 63
0

This is the default code generated by Netbeans IDE.

Keep your code in common method and map it to your invoking method.

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);
}
NamingException
  • 2,388
  • 1
  • 19
  • 41