#Understanding 2 #service(-,-) methods and 7 #doXxx(-,-) #methods of #Servlet #API: #Advance #Java #Servlet #Programming #Tutorial | #adv java:
javax.servlet.GenericServlet class is not capable to fully take care(all the operation/functionalities) of servlets based on HTTP protocol. Then, for this Sun MicroSystem provides, javax.servlet.http.HttpServlet class. This class (HttpServlet) extends javax.servlet.GenericServlet class. Developers/Programmers usually develop their servlet class, as a child of javax.servlet.HttpServlet.
doXxx(-,-) methods refers to doGet(-,-), doPost(-,-), doDelete(-,-), doPut(-,-), doHead(-,-), doTrace(-,-), doOptions(-,-), these methods when invoked, then these methods will send back 405 status code to the client, which basically means that the method of HTTP request (GET/Post/Delete/PUT/HEAD/ TRACE/ Options) made by client, is not serviced by this servlet.
About the code available in javax.servlet.http.HttpServlet class:
Whenever a request made to a servlet based on HTTP protocol by a web-client/browser, the conatainer invokes the public service(-,-) method/1st service(-,-) method. This public service(-,-) simply typecast request, response arguments and invokes protected service(-,-) method/2nd service(-,-) method. This protected service method invokes other doXxx method, depending on the method of HTTP request i.e. (GET/POST/DELETE/ PUT/ HEAD/TRACE/ OPTIONS) made by the client, invokes the corresponding doXxx(-,-) method i.e (doGet(-,-), doPost(-,-), doDelete(-,-), doPut(-,-), doHead(-,-), doTrace(-,-), doOptions(-,-))
While devleoping our servlet program by extending it from javax.servlet.http.HttpServlet class we can place request processing logic in our servlet program either by using one of the two service(-,-) or by using doXxx(-,-) methods.
1) public void service(ServletRequest req, ServletResponse res)throws ServletException, IOException
(public service(--) or 1st service(--) method)
2) protected void service(HttpServletRequest req, HttpServletReponse res) throws ServletException, IOException
(protected service(-,-) or 2nd service(-,-) method)
3) public void doXxx(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
Total 7 number of doXxx(-,-) method are available like doGet(-,-), doPost(-,-), doDelete(-,-), doPut(-,-), doHead(-,-), doTrace(-,-), doOptions(-,-)
Ex:
public void doOptions(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
public void doGet(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
public void doDelete(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
public void doPut(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
public void doHead(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
public void doTrace(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
public void doPost(HttpServletRequest req, HttpServletReponse res)throws ServletException, IOException
Eventhough there are 7 doXxx(--) methods are available in javax.servlet.http.HttpServlet class, the regularly used methods are doGet(-,-), doPost(-,-). Only public service(-,-)/1st service(-,-) method is the life cycle method. Servlet container calls this public sevice(-,-)/1st service(-,-) method as life cycle method when the request arrival event is raised.
Информация по комментариям в разработке