• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access instance variable of a servlet while it is running

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello, it's me again. I wanted to know if it is possible to access an instance variable of a servlet while the servlet is running. As part of my servlet code , I have as an instance variable:

public static int total_count = 0;

and total_count increases in value as the servlet is run. Is it possible to access total_count from a seperate class file which is not a servlet? and by this i mean, to access the current value as the servlet runs in another java program? thanks.
 
slicker
Posts: 1108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Absolutely.

<%@ page session="false" %>
<!-- Would need if the method was not static
<%@ page import="myPackage.MyClass" %>
-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<HTML>
<HEAD>

<!-- Would need if the method was not static
<% MyClass myClass = (MyClass)request.getAttribute("MyTempNm"); %>
<title><%= myClass.myNonStaticMethod() %></title>
-->

<title><%= myPackage.MyClass.myStaticMethod() %></title>
<HEAD>
<BODY>
<H1>I love to just look at the results of this method!!</H1>
</BODY>
</BODY>
</HTML>
 
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and by this i mean, to access the current value as the servlet runs in another java program

By "another java program" do you mean a different JVM? If that's the case, you would no more be able to access the static member of a servlet from that program than you would a static member of any other class. Unless of course you have the two programs communicate using sockets or something.

On the other hand, if the two programs are running under the same JVM in the same j2ee/web container, they may only be able to access the same static members if the class is in a shared library (as opposed to WEB-INF/lib or WEB-INF/classes of each application.)
 
John Dunn
slicker
Posts: 1108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duh. Sorry, I completely missed that.

You ~could~ have a specific requestLine variable
(i.e. ?cmd=getMyStaticValue) that would return the value. Then you can make a URLConnection to the servlet with the above querystring and it'll return the value.
 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by igwe kalu kalu ogba:
hello, it's me again. I wanted to know if it is possible to access an instance variable of a servlet while the servlet is running. As part of my servlet code , I have as an instance variable:

public static int total_count = 0;

and total_count increases in value as the servlet is run. Is it possible to access total_count from a seperate class file which is not a servlet? and by this i mean, to access the current value as the servlet runs in another java program? thanks.



First of all, that's not an instance variable.
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the class in which you want to access the instance variable has access to ServletContext, you can get a reference to that servlet and hence have access to the variable.

Alternatively, use the 'Class' class to get an instance of the Servlet and then use Class.getField(String fieldName) to get the value of the instance variable.

For reference:

http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Class.html
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic