• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

service() in HttpServlet

 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some doubt in ServletModel.
How the Servlet get instantiated ? we all know that it first call init() method, then service method() & destroy().
How the service() is mapped to corresponding GET or POST.
so for HttpServlet is this flow ???

init()
doGet()/doPost()
destroy()

or
init()
service()
doGet()/doPost()
destroy()

Thanks & Regards
Mike
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The latter, really. The container implements the service method so that it calls the appropriate doXXX method. The service method should never be overriden.
 
Hisham Marie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would add that the servlet service method calls the getMethod() from the request object to specify which doXXX method it has to call.
Regards
Hisham
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But which service() method it calls.
service(ServletRequest req,ServletResponse res)
or
service(HttpServletRequest req,HttpServletResponse res)
Thanks & Regards
Mike
 
Andreas Christoforides
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upon receiving a request, the servlet container/engine first calls the
service(ServletRequest req,ServletResponse res) method of the servlet.
Then the
service(ServletRequest req,ServletResponse res) method will call the
service (HttpServletRequestreq,HttpServletResponse res) method.
The service (HttpServletRequestreq,HttpServletResponse res) method will then analyze the request and call the appropriate doXXX() method.
This is the flow of control from the servlet container/engine to the doXXX() methods as defined in the HttpServlet javadoc and the SCWCD Exam Kit book.
Hope that was helpfull,
Andreas
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using the service method then it is instead of doGet and doPost. You can not use both service and doGet/doPost.
/Rene
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hoo cool...
Thanks & Regards
Mike
 
Doug Kearns
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are in doubt about things like this why not have a look at the Tomcat source code?
Regards,
Doug
 
Raj Paul
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Doug,
I didnt get ur question, see tomcat source code ?
Thanks & Regards
Mike
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Tomcat" is the reference implementation of the Servlet and JSP APIs. If you are unsure of the meaning of the specification in any area, then you can look at the Tomcat source code to see what it does and how it does it.
In all but some pretty obscure places it can be treated as a definitive implementation. By the time you find the differences, you'll have enough understanding that you can read and argue about the specification directly.
 
rajesh karmani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The reply from Andreas is fully comprehensive. The servlet cotainer calls the generic service method which delegates the call to the http-specific service method by type-casting the arguments in HttpServlet. This method calls the req.getMethod() to determine the doXXX() method.
Rajesh Kumar
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic