views:

147

answers:

3

in asp there's an Application object, which is like the Session but it's shared among all sessions...

http://msdn.microsoft.com/en-us/library/ms525360.aspx

You can use the Application object to share information among all users of a given application. An ASP-based application is defined as all the .asp files in a virtual directory and its subdirectories. Because the Application object can be shared by more than one user, there are Lock and Unlock methods to ensure that multiple users do not try to alter a property simultaneously.

I use this object to implement a simple cache for small sets of data...

What is java / jsp equivalent?

thanks a lot...

+1  A: 

ServletContext is similar to the ApplicationObject. You can use the setAttribute method to add information shared by all users. From within a servlet, you can call getServletContext() to gain access to it. I am not sure however if it provides locking/unlocking functionality.

If you are using Spring or another IoC container, you can easily define a bean that is accessible from all users. I suppose this is a better solution for both worlds.

kgiannakakis
thanks!, I'll give it a try before taking this answer as correct...
opensas
A: 

I know of two ways to accomplish this in JSP.

  1. Define a bean in the application scope:

    <jsp:useBean id="appCounter" class="com.company.AppCounter" scope="application" />

  2. Create a singleton class. Example

hoffmandirt
Singletons should be avoided in JEE containers.
kgiannakakis
I don't think that singletons should be avoided, but used carefully. Obviously the better choice here is to use the servlet context. Also, in the future please give an explanation when you make statements like that.
hoffmandirt
Read http://code.google.com/p/google-singleton-detector/ and http://www.artima.com/weblogs/viewpost.jsp?thread=213214. This is not so easy to explain in an answer or a comment.
kgiannakakis
+1  A: 

Application scope in JSP is identical to ServletContext.

Licky Lindsay