which service() method is called
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I agree All servlets extend HttpServlet and it has a method service()
but which service() method Is this right that
The container calls the service(ServletRequest,ServletResponse) method of HttpServlet.
The service(ServletRequest,ServletResponse) method of HttpServlet calls then service(HttpServletRequest,HttpServletResponse) method of the same class,as the service() method is overloaded in HttpServlet class.
or container directly call the service(HttpServletRequest,HttpServletResponse) method
Thanks & Regards
Gaurav
I guess rules apply similar to overloaded methods. If your reference object is HttpServlet then service(HttpServletRequest,HttpServletResponse) method is called.
Fellow ranchers,
Correct me if I am wrong.
Thanks
Chandu
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
If you override(and not overload) the service method in your servlet, obviously the container will call that method and not the HttpServlet's service method. I hope you know what override means, same signature; impl. in subclass.
But while doing that, you'll loose what HttpServlet's service() method does. It may have some processing on the request and then trying to find out which kind of request is this - POST or GET or any other. On basis of which, it would invoke such method.
Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
The actual question was
Which service() method is called when we invoke a Servlet, is it service(ServletRequest, ServletResponse) or service(HttpServletRequest, HttpServletResponse). My response was the second method. Is that correct?
Thanks in advance
Chandu
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Bosun (SCJP, SCWCD).
So much trouble in the world -- Bob Marley
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Originally posted by Bosun Bello:
The conatainer calls the first service method, which in turn calls the http service method to handle to http requests.
I don't think so. If you override the service method in your servlet, it would only be called regardless of any kind of request i.e. POST, GET, HEAD etc. If you have doGet/doPost or any other kind of such request handling methods, firstly, HttpServlet's service method(which again I think takes HttpServletResponse and HttpServletRequest) would be called and it would redirect to the appropriate method i.e. doPost or doGet of your implemented servlet.
To override service() method is not a good idea. As you loose some default features you get from the default implementation of that method in HttpServlet.
Work Hard, Expect The Worst...<br /> <br />Bimal R. Patel<br />(SCJP 1.2, SCWCD 1.4)
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks
Chandu
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Servlet interface declares service(ServletRequest, ServletResponse) method.
GenericServlet implements all ServletMethod EXCEPT the service method, left over for any concrete immplementation, like the HTTP one, with HttpServlet.
The HttpServlet class extends GenericServlet AND provides the first real implementation of service() method with this signature :
void service(ServletRequest, ServletResponse) throws IOException, ServletException.
And gives another service method with Http-prefixed Request/Response.
Container knows Servlet API, not HttpServlet API; so Container calls the service(ServletRequest, ServletResponse) method, which adheres to Servlet interface contract.
The actual implementation of this service method does one thing : it dispatches the request to the protected service(HttpServletRequest, HttpServletResponse).
So, the order is :
Container calls :
1. the public service(ServletRequest, ServletResponse) method;
2. public service method calls the protected service (HttpSerlvetRequest, HttpServletResponse) method.
EDIT : About your statement :
I guess rules apply similar to overloaded methods. If your reference object is HttpServlet then service(HttpServletRequest,HttpServletResponse) method is called.
This is wrong because, as I said, the public method ("overloaded" one called on your HttpServlet.service call) is the service(ServletRequest,...) one.
The service with Http- prefixed Req/res is protected, so it's not an overloaded version (cannot be more restrictive, remember SCJP
), so it's not called by Container.[ March 07, 2006: Message edited by: Frederic Esnault ]
SCJP 5 - SCWCD 1.4 - SCBCD 1.3 - Certification study documents/resources: http://esnault.frederic.free.fr/certification
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
I did not know that accesss modifier for service method in HttpServlet is protected.
I looked in the API and found it to be
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
The order of execution when a servlet gets called is:
init(ServletConfig sc)
init()
service(ServletRequest, ServletResponse)
service(HttpServletRequest, HttpServletResponse)
Correct me if I am wrong.
Thanks for your help.
Chandu
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Chandra Sagi wrote:Hi Frederic,
I did not know that accesss modifier for service method in HttpServlet is protected.
I looked in the API and found it to be
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
The order of execution when a servlet gets called is:
init(ServletConfig sc)
init()
service(ServletRequest, ServletResponse)
service(HttpServletRequest, HttpServletResponse)
Correct me if I am wrong.
Thanks for your help.
Chandu
Hi Chandu,
The order in which servlet container calls service method is as follows:
1) Container first calls service(ServletRequest req, ServletResponse response) upon the servlet called. If this method not overrided then superclass(HttpServlet) default version will be called.
2) The default version then calls service(HttpServletRequest req , HttpServletResponse res) overloaded method which you have overriden in your servlet.
Further if you override service(ServletRequest req, ServletResponse response) then you won't get default implementation(i.e. calling service(HttpServletRequest req , HttpServletResponse res) ).
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Hitesh Pawar wrote:
Chandra Sagi wrote:Hi Frederic,
I did not know that accesss modifier for service method in HttpServlet is protected.
I looked in the API and found it to be
protected void service(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException,
java.io.IOException
The order of execution when a servlet gets called is:
init(ServletConfig sc)
init()
service(ServletRequest, ServletResponse)
service(HttpServletRequest, HttpServletResponse)
Correct me if I am wrong.
Thanks for your help.
Chandu
Hi Chandu,
The order in which servlet container calls service method is as follows:
1) Container first calls service(ServletRequest req, ServletResponse response) upon the servlet called. If this method not overrided then superclass(HttpServlet) default version will be called.
2) The default version then calls service(HttpServletRequest req , HttpServletResponse res) overloaded method which you have overriden in your servlet.
Further if you override service(ServletRequest req, ServletResponse response) then you won't get default implementation(i.e. calling service(HttpServletRequest req , HttpServletResponse res) ).
Now regarding your second query:
The order in which init() is called is as follows:
1)The container first calls init(ServletConfig cfg) which will be inherited in your servlet from GenericServlet abstract class. The config object woll store servlet config information.
2)The init(ServletConfig cfg) will then call init(). Now if you don't override it then you will get default implementation otherwise your specialized initialization will be executed.
| Sometimes you feel like a nut. Sometimes you feel like a tiny ad. Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |









