aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes which service() method is called Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "which service() method is called" Watch "which service() method is called" New topic
Author

which service() method is called

Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
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
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi Chandra,

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)
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
Thanks for your response,

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
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
The conatainer calls the first service method, which in turn calls the http service method to handle to http requests.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bimal Patel
Ranch Hand

Joined: Aug 29, 2003
Posts: 130
Hi,

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.
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
This is the case of overload. So I think only second is called. Am I right?

Thanks
Chandu
Frederic Esnault
Ranch Hand

Joined: Feb 13, 2006
Posts: 284
The answer is very clear when you know about the API.

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
Chandra Sagi
Ranch Hand

Joined: May 05, 2005
Posts: 162
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
Hitesh Pawar
Greenhorn

Joined: Mar 03, 2011
Posts: 3
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) ).
Hitesh Pawar
Greenhorn

Joined: Mar 03, 2011
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which service() method is called