Servlet Maintenance Method Assignment

I thought we could not override the method service()in any particular servlet. So what is the purpose httpservlet service method?

+4
source share
2 answers

Of **service method ()**only your actual method (get, post ... etc) decides to call.

The service () method in the HTTP servlet by default routes the request to another method based on the HTTP transfer method (POST and GET). For example, HTTP POST requests are sent to the doPost () method, HTTP GET requests are sent to the doGet () method. This routing allows the servlet to process various requests depending on the transmission method. Since routing is done in the service (), you do not need to redefine the service () in the servlet-HTTP. Instead, override doGet () and doPost () depending on the expected request type.

+4
source

The servlet service () method, which performs the task of determining the method that was called, for example, get / post / trace / head / options / put / delete. These are the G7 methods, as they are most commonly used.

, , .

public void doGet(javax.servlet.http.HttpServletRequest request,  
                  javax.servlet.http.HttpServletResponseresponse)
            throws javax.servlet.ServletException,java.io.IOException {...}

,

public void doPost(javax.servlet.http.HttpServletRequest request,  
                  javax.servlet.http.HttpServletResponseresponse)
            throws javax.servlet.ServletException,java.io.IOException {...}

<

public void service(javax.servlet.ServletRequest request,  
                  javax.servlet.ServletResponseresponse)
            throws javax.servlet.ServletException,java.io.IOException {...}
+2

All Articles