Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access instance variable of a servlet while it is running

 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • 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.
 
John Dunn
slicker
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • 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>
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 1108
  • Mark post as helpful
  • send pies
  • 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.
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • 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.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic