Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet instance

 
khateeb syed
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

While reading about servlet life cycle in a book, i came across this:
Only a single instance of each servlet gets created with each user request resulting in a new thread that is handed off to doGet or doPost as appropriate.
I understand everything except the servlet being instantiated, can someone tell me what it means?

Thanks.
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty much what it says. If I have a servlet in an app server then, even if there are 1000 users accessing the servlet at one time, only one instance is ever created - like with a new() statement.

All of the user requests are handled by a thread for that user. This is why it is very important to make sure that any data within a servlet is thread safe even though you never create or manage any threads.
 
Alessandro Ilardo
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm quite new in J2EE and I still missing a fundamental step: is it better process the data received by the request directly in the servlet or in class instanced?
 
Scott Dunbar
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not quite following you. In a HttpServlet has methods that are separated by HTTP method - a doPost() for HTTP POST requests, a doGet() for HTTP GET requests and so on.

As far as where you pull the data out of the HttpServletRequest that is really up to you. Back in the "old days" I just did it in the doGet()/doPost() methods by hand using getParameter() (which I can't link to because of the prehistoric age of the software running Javaranch ). Newer applications may benefit from using something like Java server faces or Struts. A part of these technologies is to make HTML form processing much easier.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Dunbar:
A part of these technologies is to make HTML form processing much easier.


You'll get differing opinions on that. There are those that would say that these technologies are just obfuscating factors that actually make things more difficult to accomplish under the guise of being "modern".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic