0

I have made a project in where three string parameters entered by user are sent to a servlet named controller. There I have used RequestDispatcher to forward those three parameters to a JSP page and print them. I have did something and made it work but am confused why it works only if I put all the commands in the service() method. I would love to know the right or the standard way of achieving this same result. And also I will be glad to know the actual work of doGet(),doPost() and service() and ProcessRequest() methods. This is the Servlet code:

public class controller extends HttpServlet {

    public String TName,TUserName,TPassword;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response) 
        throws IOException, ServletException{     
        TName = request.getParameter("name");
        TUserName = request.getParameter("UserName");
        TPassword = request.getParameter("Password");    
        RequestDispatcher rd =getServletContext().getRequestDispatcher("/welcome.jsp");
        request.setAttribute("TName,TUserName,TPassword", rd);
        rd.forward(request, response);
    }

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

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
rsanath
  • 1,154
  • 2
  • 15
  • 24

1 Answers1

0

If you check the class you extent you will see following code

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String method = req.getMethod();
    long errMsg;
    if(method.equals("GET")) {
        errMsg = this.getLastModified(req);
        if(errMsg == -1L) {
            this.doGet(req, resp);
        } else {
            long ifModifiedSince = req.getDateHeader("If-Modified-Since");
            if(ifModifiedSince < errMsg / 1000L * 1000L) {
                this.maybeSetLastModified(resp, errMsg);
                this.doGet(req, resp);
            } else {
                resp.setStatus(304);
            }
        }
    } else if(method.equals("HEAD")) {
        errMsg = this.getLastModified(req);
        this.maybeSetLastModified(resp, errMsg);
        this.doHead(req, resp);
    } else if(method.equals("POST")) {
        this.doPost(req, resp);

as you see the request comes to service method first than distributed to corresponding specific method like "GET","POST" etc.

So if you want to make it run without using service you should override doGet or doPost or both.

cool
  • 1,746
  • 1
  • 14
  • 15