Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

service method - when does it end  RSS feed

 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading Headfirst Servlets and JSP, and I will preface this with the fact that I usually unintentionally strike someone's nerve when I ask questions in here.

Page 95/96 show the Container's role in a servlet life cycle. I know the Container loads the class, runs the constructor, calls the init() method, service() method and destroy() methods.

On page 96, you see an illustration (#6) about the service() method ending and the request response ojbects becoming toast.

I know that init() is called only once.

Page 100, "Thread C" says that the service() method sequence happens each time there is a client request.

When does the service() method end? Is it when your doGet() or doPost() methods return the request/response objects back to the Container to be sent on to the client browser?

If init() is called only once, and that is where you put your database connection info, how do you know if your servlet thread is dead or not in order to process another service() method per client request?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by leo donahue:
When does the service() method end? Is it when your doGet() or doPost() methods return the request/response objects back to the Container to be sent on to the client browser?

The base service method will call the appropriate "do" method and then return. Remember, it's just a Java method like any other.

If init() is called only once, and that is where you put your database connection info
init() is a very poor place to create a db connection.
 
leo donahue
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:
init() is a very poor place to create a db connection.


I said that, only because the book said that, twice.

So, the Container manages a Servlet, calls the service () method and the service() method thread can die off, but the Container is still able to process new service() method requests in new threads or from a thread pool on the already initialized Servlet?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by leo donahue:

So, the Container manages a Servlet, calls the service () method and the service() method thread can die off, but the Container is still able to process new service() method requests in new threads or from a thread pool on the already initialized Servlet?


Absolutely true , the same servlet would continue to serve requests unitl the servlet is destroyed.
 
gianni ipez
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am reading the same book, I can assure that the authors do not say that putting the db connection in the init() method of a servlet is good.
In opposite they indicate to put db infos on an init parameter at ServletContext level (visible to all application and not just by one specific servlet)
ciao
Gianni
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65825
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clarification: this does not mean that it's ok to store DB connections in the application context -- storing the info necessary to obtain a DB connection is what can be stored there.

Web apps are best-served by using container-manager connection pooling. If you are using Tomcat, this subject has been discussed many times in the Tomcat forum.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!