Question about servlets

M

Miguel De Anda

We're starting to move our asp work into java servlets and my boss said
something that I find a bit hard to believe. If we have a servlet like this:

class MyServlet ... {

private HttpServletRequest requestObject;

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
{
this.requestObject = request;
...
}
}

Does the requestObject stay in memory after the servlet is finished loading?
I guess my questio is, if we have a servlet, does every request to the
servlet use the same instance of it? Does it call the constructor only once
until the server is shut down or does it create a new instance of it on
every request?
 
M

Miguel De Anda

Miguel De Anda said:
We're starting to move our asp work into java servlets and my boss said
something that I find a bit hard to believe. If we have a servlet like this:

class MyServlet ... {

private HttpServletRequest requestObject;

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
{
this.requestObject = request;
...
}
}

Does the requestObject stay in memory after the servlet is finished loading?
I guess my questio is, if we have a servlet, does every request to the
servlet use the same instance of it? Does it call the constructor only once
until the server is shut down or does it create a new instance of it on
every request?

Forgot to mention we're using Tomcat.
 
V

VisionSet

Miguel De Anda said:
We're starting to move our asp work into java servlets and my boss said
something that I find a bit hard to believe. If we have a servlet like this:

class MyServlet ... {

private HttpServletRequest requestObject;

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
{
this.requestObject = request;
...
}
}

Does the requestObject stay in memory after the servlet is finished
loading?

requestObject will stay in memory after the 1st request and be replaced by
the new request on the next request. Obviously you shouldn't do this for
request data.
I guess my question is, if we have a servlet, does every request to the
servlet use the same instance of it?

Yes, unless you use the not recommended single thread model which may well
have been deprecated by now.
Does it call the constructor only once
until the server is shut down

Yes.
 
F

flacco

Miguel said:
We're starting to move our asp work into java servlets

Congratulations! Wise move.

and my boss said
something that I find a bit hard to believe. If we have a servlet like this:

class MyServlet ... {

private HttpServletRequest requestObject;

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
{
this.requestObject = request;
...
}
}

Does the requestObject stay in memory after the servlet is finished loading?

Yes, if you maintain a reference to it as a member of the servlet class
(though it will be replaced next time doGet is executed of course). If
you don't want to keep it around, make requestObject a local var in the
doGet method instead of a class member (a bit redundant though). Or you
can set it to null after you're done with it.

I guess my questio is, if we have a servlet, does every request to the
servlet use the same instance of it? Does it call the constructor only once
until the server is shut down or does it create a new instance of it on
every request?

The servlet itself is only created once. That's the nature of a servlet
- to load and handle requests. If you want separate instances of
objects, you should probably design that into stand-alone classes that
you instantiate from within the servlet's methods. That way, after each
execution of the method (e.g. doGet(...)), the object will go out of
scope and be gc'd.
 
W

Wendy S

Miguel said:
Does the requestObject stay in memory after the servlet is finished
loading?

Even though it's getting dated, Jason Hunter's Servlet book is still a
valuable introduction to the topic. Then maybe move on to a newer book.

And read (or at least skim) the Servlet Specification:
http://java.sun.com/products/servlet/download.html

I stayed away from the specs thinking that they would be too technical and
way over my head, but that was a mistake. The specification contains the
rules that the container must follow. If you read and understand most of
it, you will have a firm grasp of exactly what to expect your container to
do in any given situation.
 
W

William Brogden

Miguel De Anda said:
We're starting to move our asp work into java servlets and my boss said
something that I find a bit hard to believe. If we have a servlet like this:

class MyServlet ... {

private HttpServletRequest requestObject;

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
{
this.requestObject = request;
...
}
}

Does the requestObject stay in memory after the servlet is finished
loading?
Yes - and multiple requests will see the same instance variables -
HttpServletRequest
and HttpServletResponse objects are managed by the servlet container.
You should NEVER try to keep them around.

The HttpSession mechanism is what you use for user specific data.
I guess my questio is, if we have a servlet, does every request to the
servlet use the same instance of it? Yes

Does it call the constructor only once
until the server is shut down or does it create a new instance of it on
every request?
Once

It takes a distinct shift in attitude to program in the servlet environment
but once you make the shift you will find it is a very elegant API.
 
J

John C. Bollinger

Miguel said:
We're starting to move our asp work into java servlets and my boss said
something that I find a bit hard to believe. If we have a servlet like this:

class MyServlet ... {

private HttpServletRequest requestObject;

protected void doGet(HttpServletRequest request, HttpServletResponse
response)
{
this.requestObject = request;
...
}
}

ACK! Why in the world would you want to do that?
Does the requestObject stay in memory after the servlet is finished loading?
I guess my questio is, if we have a servlet, does every request to the
servlet use the same instance of it? Does it call the constructor only once
until the server is shut down or does it create a new instance of it on
every request?

The servlet is instantiated and initialized once per servlet context in
which it runs, and that instance is used for the lifetime of the servlet
context, except that the servlet container is free to use multiple
instances if the servlet implements SingleThreadedModel. If you do not
implement SingleThreadedModel then multiple requests may be processed
through the one servlet instance simultaneously, and your servlet will
break if it cannot handle concurrent usage. If you depend on the
requestObject variable as declared and initialized above, then your
servlet cannot handle concurrent usage.

The easiest way to not have to worry about concurrency in a servlet (or
any object) is to not maintain any non-constant state there. Although
that approach is frequently unsuitable in general, it is often suitable
and appropriate for servlets.

Note also that in the above situation, the object referred to by your
requestObject variable may not be valid outside the scope of the request
to which it pertains. (It will still exist, of course, but it may have
been reused for a subsequent request.) It is _always_ a bad idea to
cache request or response objects.


John Bollinger
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top