File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Access instance variable of a servlet while it is running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Access instance variable of a servlet while it is running" Watch "Access instance variable of a servlet while it is running" New topic
Author

Access instance variable of a servlet while it is running

igwe kalu kalu ogba
Ranch Hand

Joined: Feb 03, 2005
Posts: 133
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

Joined: Jan 30, 2003
Posts: 1108
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>


"No one appreciates the very special genius of your conversation as the dog does."
Yuriy Zilbergleyt
Ranch Hand

Joined: Dec 13, 2004
Posts: 429
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

Joined: Jan 30, 2003
Posts: 1108
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

Joined: Feb 20, 2001
Posts: 580
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.


James Carman, President<br />Carman Consulting, Inc.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Access instance variable of a servlet while it is running