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?
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.
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: