• Post Reply Bookmark Topic Watch Topic
  • New Topic

Static variables/objects in servlets?  RSS feed

 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, can we use static variables or static objects in servlets ?? I want to restrict same user to enter into the application from different system when the user is already working, he needs to get a message saying "User is already in use ".

I have created static SET object in LoginServlet and inside doget() I checked if SET contains user ,if yes display him above message otherwise add the user in SET object and forward it to next page . Later while logging out user is removed from SET .

Can I follow any other approach other than using static object SET in servlets??

Thank you,
Shiva
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can we use static variables or static objects in servlets ??

Yes, Servlet Specifications or Java syntax do not restrict to use static variables in Servlet. But it is not advisable to use static variables in Servlet as single Servlet is being used across all threads by Servlet Container at runtime.

For your use case, one of the better (if not best) option is to save User details in Application Listener. Define Application/Session Activation Listener and create your Set in that listener. And then do your validations in it.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The listener will also be shared.

The best place to store application-wide data is in the application context (via servletContext.setAttribute). Otherwise, you are introducing thread-safety problems in your application that will cause puzzling and hard-to-debug problems.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The best place to store application-wide data is in the application context (via servletContext.setAttribute). Otherwise, you are introducing thread-safety problems

Just to clarify, though: Putting something in the application context does not make it thread-safe. It exposes it to all concurrent threads, and your code thus needs to protect the data if it's mutable (which this data would be).
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct -- and important to point out -- care must always be take with shared read/write data in a web app.

If the data needs to persist, the database can be a good place for it as well.
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
I did not knew that Listeners are shared among threads.
 
Shiva Gajjala
Ranch Hand
Posts: 77
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies .I will try using application Context object as well as Database table.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!