File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Does it make any need to have static variables inside a Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Does it make any need to have static variables inside a Servlet" Watch "Does it make any need to have static variables inside a Servlet" New topic
Author

Does it make any need to have static variables inside a Servlet

Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234


Hi ,

Since a servlet is a singleton , does it make sense to have static variables in a servlet .

I mean to say :
We can satisfy our requirement with instance variables , rather than using instance variables in a servlet


please share your views on this .


Save India From Corruption - Anna Hazare.
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

Having static variables within a Servlet is very dangerous because it'll be accessed by several threads at a time, so the static variable has no sense.

Servlets with static variables ... No way!


http://serfj.sourceforge.net - Simplest Ever REST Framework for Java
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

because it'll be accessed by several threads at a time


Thnaks for the answer , but the samething happens when there are instance variables in a Servlet . (Means all the Threads can access this data at the same time and as servlet is a singleton )


Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

If servlets were singleton it'd be the same for instance variables, but servlets aren't singletons, you can have several instance of a servlet.

Read more about your question on this JavaRanch topic I've just found.


Pravin Shirke
Ranch Hand

Joined: Apr 05, 2008
Posts: 150

Hi ravi,
servlets are not singleton please refer to the link provided by Eduardo.

Note: Eduardo

load on startup does not means that the container will create 5 instances. please go through scwcd faq's and links in javaranch.
hope it helps


[Vipassana] - It is seeing the reality as it is, And not as you want it to be.!!!
SCJP1.5.
Eduardo Yañez Parareda
Ranch Hand

Joined: Oct 09, 2008
Posts: 92

Pravin Shirke wrote:
load on startup does not means that the container will create 5 instances. please go through scwcd faq's and links in javaranch.
hope it helps


Thank you, I haven't found the answer within the FAQ, but I've found this:

"<load-on-startup> tag specifies that the servlet should be loaded
automatically when the web application is started.

The value is a single positive integer, which specifies the loading
order. Servlets with lower values are loaded before servlets with
higher values"

I had a mess in my head with that tag.
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

That link provided was nice , but there is no proper explanation to be frank .

I found the below interesting :

The Servlet API does not restrict the creation of a particular Servlet class to a single instance, therefore it is not a Singleton.


Could anybody prove , that there were more than one instances of a servlet with in a container ??

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9329
    
110

Could anybody prove , that there were more than one instances of a servlet with in a container ??


Answers to questions like this can be found in the spec. The Servlet spec has this:

The servlet declaration which is either via the annotation as described in Chapter 8, "Annotations and pluggability" or part of the deployment descriptor of the Web application containing the servlet, as described in Chapter 14, "Deployment Descriptor", controls how the servlet container provides instances of the servlet.
For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration. However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.

In the case where a servlet was deployed as part of an application marked in the deployment descriptor as distributable, a container may have only one instance per servlet declaration per Java Virtual Machine (JVMTM)1. However, if the servlet in a distributable application implements the SingleThreadModel interface, the container may instantiate multiple instances of that servlet in each JVM of the container.


[My Blog] [JavaRanch Journal]
Ravi Kiran Va
Ranch Hand

Joined: Apr 18, 2009
Posts: 2234

However, for a servlet implementing the SingleThreadModel interface, the servlet container may instantiate multiple instances to handle a heavy request load and serialize requests to a particular instance.


Great answer , thank you very much .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does it make any need to have static variables inside a Servlet
 
Similar Threads
Question about inheritance
Thread Safe Instance Variables
Thread safety and servlets
Static and Global variables
static instance variable in a Java class