• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How do we use global variables in servlets?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any read-write data accessible across threads will need to be synchronized, regardless of how it is stored.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 27
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic