Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Question about servlets

Reply
Thread Tools

Question about servlets

 
 
Miguel De Anda
Guest
Posts: n/a
 
      06-27-2003
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?


 
Reply With Quote
 
 
 
 
Miguel De Anda
Guest
Posts: n/a
 
      06-27-2003

"Miguel De Anda" <(E-Mail Removed)> wrote in message
news:kA2La.3967$(E-Mail Removed) m...
> 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.


 
Reply With Quote
 
 
 
 
VisionSet
Guest
Posts: n/a
 
      06-27-2003
"Miguel De Anda" <(E-Mail Removed)> wrote in message
news:kA2La.3967$(E-Mail Removed) m...
> 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.

--
Mike W


 
Reply With Quote
 
flacco
Guest
Posts: n/a
 
      06-27-2003
Miguel De Anda wrote:
> 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.

 
Reply With Quote
 
Wendy S
Guest
Posts: n/a
 
      06-28-2003
Miguel De Anda wrote:

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

--
Wendy in Chandler, AZ
 
Reply With Quote
 
William Brogden
Guest
Posts: n/a
 
      06-28-2003

"Miguel De Anda" <(E-Mail Removed)> wrote in message
news:kA2La.3967$(E-Mail Removed) m...
> 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.




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      06-30-2003
Miguel De Anda wrote:
> 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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java servlets: Hi All! I want to display xml file in browser using servlets datta.saru Software 0 05-15-2006 03:30 PM
Realtime servlets? Newbie Question Brian Java 5 09-06-2004 01:00 AM
Tomcat 3.3.1a question - servlets not under WEB-INF\classes\ M Java 5 09-19-2003 11:09 PM
servlets question (reuse ) miko Java 2 09-18-2003 03:44 PM
Socket/Servlets Question - Please HELP! Keren Java 1 07-30-2003 04:07 PM



Advertisments