Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2093
  • Last Modified:

Application Level Variables in a Servlet

Can you have application level variables in a servlet?  If so, how can you store an object at that level?  Could you give me a code example please.
1 Solution
Hai CodeHead,

  Whatever you have defined outside all the methods, will be treated as application level scope.  

public class TestServlet extends HttpServlet
   private String testString = null;
   public void init(....

   public void service(....

Eventhough, the testString is private, it is visible across users and in application scope.

Any variable that is defined within the method is private to that method.  Any variable that is outside all the methods will be treated in application scope.

All the best

Application level variables are declared as "static" varables in the servlet class.  

if you had the following declared in the class

private static int count = 4;

each user accessing the servlet would "see" the same value and once changed any future users accessing would see the changed value.

This works for all type of objects declared as static.
ServletContext application = getServletConfig().getServletContext();

String somethingToStore = "test";
application.setAttribute("an_appilcation_level_variable", somethingToStore);

String somethingToRetreive = (String) application.getAttribute("an_appilcation_level_variable");
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

The servlet context is in the application scope...so set your attributes there.

If, in your JSP you have:

<jsp:useBean id="myObject" class="my.package.name.MyClass" scope="application" />

In your servlet, you would have the following equivalent code:

Object obj = getServletContext().getAttribute("myObject");
if(obj==null || (!obj instanceof my.package.name.MyClass)) {
   obj = new my.package.name.MyClass();
   getServletContext().setAttribute("myObject", obj);

oops...sorry...I put the bang (!) inside the parentheses by accident...my bad.

pellep has the correct answer..

application variable can be set using ServletContext.. in one web application (WAR), there will be only one instance of ServletContext ..

comment form RichardA
>Application level variables are declared as "static" varables in the servlet class.  
>if you had the following declared in the class
>private static int count = 4;

There is not necessary to put "static" keyword in class since Servlet XXXX only has 1 instance in the whole web application (WAR), but there may be 1 or more thread accessing the same instance (depends on the request from browser).
Static keyword is needed if you want to access servlet's member from another servlet :
 Object o = Servlet123.doSomething();

In this case, doSomething should be declared as static so other servlet or classes (even jsp) can execute doSomething().

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now