Meaningless Drivel is fun!
The moose likes Servlets and the fly likes Servlet instance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet instance" Watch "Servlet instance" New topic

Servlet instance

khateeb syed

Joined: Jun 25, 2005
Posts: 9

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?

Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
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.

<a href="" target="_blank" rel="nofollow">Java forums using Java software</a> - Come and help get them started.
Alessandro Ilardo
Ranch Hand

Joined: Dec 23, 2005
Posts: 218
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?

trying to decode a woman mind....
Scott Dunbar
Ranch Hand

Joined: Sep 23, 2004
Posts: 245
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

Joined: Jan 10, 2002
Posts: 63865

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".

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Servlet instance
jQuery in Action, 3rd edition