Question about servlets

Discussion in 'Java' started by Miguel De Anda, Jun 27, 2003.

  1. 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?
    Miguel De Anda, Jun 27, 2003
    #1
    1. Advertising

  2. "Miguel De Anda" <> wrote in message
    news:kA2La.3967$...
    > 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.
    Miguel De Anda, Jun 27, 2003
    #2
    1. Advertising

  3. Miguel De Anda

    VisionSet Guest

    "Miguel De Anda" <> wrote in message
    news:kA2La.3967$...
    > 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
    VisionSet, Jun 27, 2003
    #3
  4. Miguel De Anda

    flacco Guest

    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.
    flacco, Jun 28, 2003
    #4
  5. Miguel De Anda

    Wendy S Guest

    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
    Wendy S, Jun 28, 2003
    #5
  6. "Miguel De Anda" <> wrote in message
    news:kA2La.3967$...
    > 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 =---
    William Brogden, Jun 28, 2003
    #6
  7. 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
    John C. Bollinger, Jun 30, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keren
    Replies:
    1
    Views:
    2,562
    Chris Smith
    Jul 30, 2003
  2. M
    Replies:
    5
    Views:
    430
    Christophe Vanfleteren
    Sep 20, 2003
  3. miko
    Replies:
    2
    Views:
    391
    John C. Bollinger
    Sep 18, 2003
  4. Brian
    Replies:
    5
    Views:
    450
    Brian
    Sep 6, 2004
  5. Ryan Gaffuri
    Replies:
    4
    Views:
    3,910
    Sudsy
    Sep 30, 2004
Loading...

Share This Page