Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

instance of a servlet

 
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Darryl failla said recently in another thread (technique for shared servlet data):
It seems to me that in the web server environment, you can not guarantee that new instances of a servlet will not be created when needed.

Another mention of servlet instances from the servlets forum (old thread):
...somewhere before your class, another class was loaded, probably something like servletrunner.class. This class takes care of everthing needed for persistance etc. It also takes care of creating your servlet instance object.... after it ensures that there is an instance that has ran the init() method. Everytime you make a request you get a new instance of your servlet. Including the first request, one is made for persistance and one is made to execute your doPost, doGet or service method.

Seeing "servlet instances" got me confused, given that that we write servlets without ever a Servlet myServlet = new Servlet() (or some such obvious object creation).

So, when someone says "serlvet instance", it can simply be understood as "servlet request"?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All servlets must implement the Servlet interface. As with many key applet methods, the methods of interface Servlet are invoked automatically by the server on which the servlet is installed, also known as the servlet container.

"It (the servlet container) also takes care of creating your servlet instance object." as you quoted above, so you don't have to create a servlet object by using "new".
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, do I understand it right - the servlet container does this for me each time a servlet is requested?
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Think I got it now. From Core Servlets:
"Normally the system makes a single instance of your servlet and then creates a new thread for each user request..."
Time to go brush up on threads...
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Pauline McNamara:
"Normally the system makes a single instance of your servlet and then creates a new thread for each user request..."


"Normally" is the key word. You should never write your servlets under the assumption that there will only be one instance of a servlet. The servlet engine can create as many or as few as it determines are needed to achieve optimum performance.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Thomas Paul:

"Normally" is the key word. You should never write your servlets under the assumption that there will only be one instance of a servlet. The servlet engine can create as many or as few as it determines are needed to achieve optimum performance.


Aha. Actually, that quote was taken from the paragraph introducing the SingleThreadModel interface.
Thanks, Thomas.
 
village idiot
Posts: 1208
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, I need a little clarification here. What exactly is a servlet engine, and where does it hang out? When I did these assignments, I guess I just used the functionality of the servlet interface without really understanding what it was doing for me!
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A servlet engine handles all the request and response mechanisms so you don't have to worry about it. It instantiates the servlets as needed, sends the requests to the appropriate servlets, and sends responses to the appropriate client. It also handles threading issues and all that stuff.
You simply write the servlet to do the processing, the engine handles the rest.
The term servlet engine seems to be used interchangeably with servlet container, though I don't think there is any real difference. Tomcat is a servlet container, so is Orion. When you fire up the server, the engine or container starts up and gets ready to receive requests. The web.xml file you define in your web app configures the container to handle requests that come in for your web app.
You derive HttpServlet to provide the functionality you need. By deriving HttpServlet, the engine can deal with all servlets generically (through doGet and doPost), and not worry about what it is your servlet is actually doing. The engine doesn't care that your servlet wants to take in a string and reverse it, it just wants to send it a request and handle a response.
You can get even more generic by dealing with GenericServlet, or even the Servlet interface, though I've found there are very few occasions where you need to do that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic