• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do we use global variables in servlets?

 
harke baj
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any read-write data accessible across threads will need to be synchronized, regardless of how it is stored.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic