This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Servlets and the fly likes How do we use global variables in servlets? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "How do we use global variables in servlets?" Watch "How do we use global variables in servlets?" New topic
Author

How do we use global variables in servlets?

harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
Hi all,

How can I use global variables in servlets? Like if I want some variables that can be accessed by all the servlets within the application. The variables can be read or written.

I looked into the forums and found two methods: using servletcontext and static.

They had their own disadvantages:

servletcontext: have problem with synchronization

static: they belong to a class and not to an application. ( I didn't get their exact disadvantages. Could someone tell me?)


Are there other methods for using global variables?

How about using database? Will I have synchronization problem?

thanks,
harke
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56180
    
  13

Any read-write data accessible across threads will need to be synchronized, regardless of how it is stored.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

harke baj wrote:servletcontext: have problem with synchronization


what about *session* ? Ofcourse, Session has concurrency issue if, two browsers(same type) are open in a client machine. but it is a rare scenario
harke baj
Greenhorn

Joined: Feb 02, 2010
Posts: 28
I want the variables to be accessible throughout the application for all users. So httpsession wont be useful, i think, as it is only for one user.

So i need synchronization even if I use database?


thanks,
harke
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56180
    
  13

Seetharaman Venkatasamy wrote:but it is a rare scenario

Actually not rare at all. Multiple threads can be accessing the same session not only from multiple browser instances, but from different frames, iframes, and Ajax requests.
Ankur Goel Goel
Greenhorn

Joined: Oct 13, 2009
Posts: 2

I think you can use beans. You can use static variable in any bean and that variable will use its value till the server is reset.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56180
    
  13

Beans or not, the issue is the same. The point is where the data is stored and if it needs to be synchronized.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

harke baj wrote:I want the variables to be accessible throughout the application for all users. So httpsession wont be useful, i think, as it is only for one user.


Cool . you need to mention requirement clearly .

this is from your post

harke baj wrote:How can I use global variables in servlets? Like if I want some variables that can be accessed by all the servlets within the application. The variables can be read or written.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Bear Bibeault wrote: Multiple threads can be accessing the same session not only from multiple browser instances, but from different frames, iframes, and Ajax requests.


Ohh. thanks bear
parampreet sethi
Greenhorn

Joined: Aug 03, 2009
Posts: 27

You can use application level variables as well. We have three levels of variables depending upon their scope in any web application.

1. Page level - specific to that page or request
2. Session level - specific to a session
3. Application level - specific to the application

To store variables at application level, below mentioned is the sample code:



The other way would be to use the below mentioned code:

In the JSP, you should have




In the servlet, you would have the following equivalent code:



Param
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56180
    
  13

parampreet sethi wrote:We have three levels of variables depending upon their scope in any web application.
Four. You left request scope off the list.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: How do we use global variables in servlets?
 
Similar Threads
use diffrent servlet values (how to unload a servlet)
how 2 servlets communicates
ServletContext & synchronization?
few intrvw questions
Dealing with multi-threading in servlets