static method and variables in servlets and APIs' classes

Discussion in 'Java' started by Garg, Feb 18, 2008.

  1. Garg

    Garg Guest

    Hi All,

    In my current project i found that a large number of methods are
    public static. I am not getting why we need static methods and
    variables in servlets and apis' as we all know that on each request a
    new instance of servlet is been created.

    I tried to find the answer but i confused some people take about
    different instances of one object of servlet and some talk about
    different treads of that same instance. Now i am trying to relate the
    servlet and api's object, instance, treads with static variables and
    method defined.

    Please help in that.

    Thanks
    Tarun Garg
     
    Garg, Feb 18, 2008
    #1
    1. Advertising

  2. Mon, 18 Feb 2008 10:00:44 -0800 (PST), /Garg/:

    > In my current project i found that a large number of methods are
    > public static. I am not getting why we need static methods and
    > variables in servlets and apis' as we all know that on each request a
    > new instance of servlet is been created.


    Speak for yourself, I know just the opposite: while not requirement
    (AFAIK) a single servlet instance handles all the requests. Tagging
    a servlet class with the SingleThreadModel [1] interface may make
    the servlet container create different servlet instances to handle
    the different requests but may synchronize the access to a single
    servlet instance, also.

    [1]
    http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/SingleThreadModel.html

    --
    Stanimir
     
    Stanimir Stamenkov, Feb 18, 2008
    #2
    1. Advertising

  3. Garg

    Mark Space Guest

    Garg wrote:
    > Hi All,
    >
    > In my current project i found that a large number of methods are
    > public static. I am not getting why we need static methods and


    Java, and servlets, is just programming. You make static methods and
    variables in a servlet for the same reason you'd make them in a regular
    Java program.

    > variables in servlets and apis' as we all know that on each request a
    > new instance of servlet is been created.


    Er, no, we don't know that. It's not true. One instance is created of
    each servlet, then it gets re-used for all requests until it's
    destroyed. Or that's the most common implementation anyway. But
    certainly nothing requires or implies that one instance is created per
    request. Not even SingleThreadedModel does that.

    >
    > I tried to find the answer but i confused some people take about
    > different instances of one object of servlet and some talk about
    > different treads of that same instance. Now i am trying to relate the


    Your English is so bad here I can barely guess what you are saying. Do
    you mean "some people take" or "some people talk?"

    Any, please review your posts better before you send them, you'll better
    answers.

    A public static variable is a global variable, and it would be the same
    in a regular Java program. A private static variable could be used as a
    singleton.

    Static methods are useful for decomposition when there's no reason to
    attach a method to a specific object. They're just generic operations.
    Often they get used for factory methods, but there are many other uses.

    If you have some questions about a specific API or method or variable,
    please say what it is, since that might also bear on the question.
     
    Mark Space, Feb 18, 2008
    #3
  4. Garg

    Lew Guest

    Stanimir Stamenkov wrote:
    > Mon, 18 Feb 2008 10:00:44 -0800 (PST), /Garg/:
    >
    >> In my current project i found that a large number of methods are
    >> public static. I am not getting why we need static methods and
    >> variables in servlets and apis' as we all know that on each request a
    >> new instance of servlet is been created.

    >
    > Speak for yourself, I know just the opposite: while not requirement
    > (AFAIK) a single servlet instance handles all the requests. Tagging a
    > servlet class with the SingleThreadModel [1] interface may make the
    > servlet container create different servlet instances to handle the
    > different requests but may synchronize the access to a single servlet
    > instance, also.
    >
    > [1]
    > http://java.sun.com/products/servlet/2.5/docs/servlet-2_5-mr2/javax/servlet/SingleThreadModel.html


    It is also possible that the servlet container creates multiple instances of a
    servlet, but not nearly as many as there are requests. This is true for
    clustered servers, for example, and certainly useful for multi-processor
    systems. You very well could see one servlet instance per processor hardware
    thread or core. You could theoretically see more than one instance per core,
    if a server were smart enough to figure out that I/O or other blocking
    operations would make that more efficient.

    The bottom line is that you cannot count on there being more than one instance
    of a servlet active at a time, nor can you count on there being only one.

    --
    Lew
     
    Lew, Feb 19, 2008
    #4
  5. Lew wrote:
    > It is also possible that the servlet container creates multiple
    > instances of a servlet, but not nearly as many as there are requests.
    > This is true for clustered servers, for example, and certainly useful
    > for multi-processor systems. You very well could see one servlet
    > instance per processor hardware thread or core. You could theoretically
    > see more than one instance per core, if a server were smart enough to
    > figure out that I/O or other blocking operations would make that more
    > efficient.


    ????

    I can really not see the link between cores and instances and
    between blocking operations and instances.

    Arne
     
    Arne Vajhøj, Feb 19, 2008
    #5
  6. Garg

    Lew Guest

    Arne Vajhøj wrote:
    > Lew wrote:
    >> It is also possible that the servlet container creates multiple
    >> instances of a servlet, but not nearly as many as there are requests.
    >> This is true for clustered servers, for example, and certainly useful
    >> for multi-processor systems. You very well could see one servlet
    >> instance per processor hardware thread or core. You could
    >> theoretically see more than one instance per core, if a server were
    >> smart enough to figure out that I/O or other blocking operations would
    >> make that more efficient.

    >
    > ????
    >
    > I can really not see the link between cores and instances and
    > between blocking operations and instances.


    I see my mistake - this would normally be handled by spawning a new thread on
    the current instance, not by creating new instances.

    Never mind.

    --
    Lew
     
    Lew, Feb 19, 2008
    #6
  7. Garg

    Garg Guest

    This is what i understood.
    * Multi threading means that same instance of a program is running. It
    doesn't mean that two separate instances of the program is running.
    * Servlet container is responsible for the creation, execution and
    destruction of the servlet.
    * Instance variables are defined in the class and they are tied with
    the instance of the class.
    * To make the servlet threads safe declare all the variable in the
    method. It makes them local variables otherwise they will remain as
    instance variables.
    * By putting the synchronize code block, thread safe can be achieve.
    * And by using interface SinglethreadModel.
    * A public static variable is a global variable.
    * A private static variable can be use as singleton.
    * Static methods put the method to the class level so creation of the
    class object is not required to call that method.

    I do have more questions but i will put them once my understanding
    gets approved.

    Thanks for time
    Tarun Garg
     
    Garg, Feb 20, 2008
    #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. Suresh

    static variables in servlets

    Suresh, Sep 10, 2003, in forum: Java
    Replies:
    4
    Views:
    11,610
    John C. Bollinger
    Sep 10, 2003
  2. M
    Replies:
    5
    Views:
    445
    Christophe Vanfleteren
    Sep 20, 2003
  3. Davo C S
    Replies:
    0
    Views:
    338
    Davo C S
    Aug 9, 2004
  4. thecrow
    Replies:
    24
    Views:
    1,225
    John McGrath
    May 28, 2005
  5. Martin Jones
    Replies:
    2
    Views:
    340
    Martin Jones
    Apr 7, 2012
Loading...

Share This Page