Solved

Application Level Variables in a Servlet

Posted on 2002-04-09
6
2,070 Views
Last Modified: 2013-11-24
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.
0
Comment
Question by:CodeHead
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 9

Expert Comment

by:mmuruganandam
ID: 6929183
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

Thanks
Muruganantham
0
 
LVL 1

Expert Comment

by:RichardA
ID: 6929189
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.
0
 
LVL 4

Accepted Solution

by:
pellep earned 50 total points
ID: 6929230
ServletContext application = getServletConfig().getServletContext();

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

String somethingToRetreive = (String) application.getAttribute("an_appilcation_level_variable");
0
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
LVL 2

Expert Comment

by:xxMariusxx
ID: 6929714
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);
}

Marius
0
 
LVL 2

Expert Comment

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

Marius
0
 
LVL 5

Expert Comment

by:LexZEUS
ID: 6930657
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().
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 83
glassfish admin console 1 53
Java Eclipse Loop 3 50
Java import explained 4 49
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question