struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Galbreath" <mark_galbre...@qat.com>
Subject RE: How to Access an Application Scope object in a Struts Action Class
Date Wed, 15 Jan 2003 00:57:46 GMT
That's because a JSP application scope is a servlet context scope object.
Use this instead:

ServletContext context = this.getServlet().getServletContext();
Map monitor = ( Hashtable) context.getAttribute( "monitor" );

Mark

-----Original Message-----
From: Haseltine, Celeste [mailto:CHaseltine@magticket.com] 
Sent: Tuesday, January 14, 2003 7:41 PM


I'm relatively new to Struts, and I am working in Struts 1.02 (since we are
not comfortable using a beta version of struts on a new project), and I am
stuck on a simple problem.  If I create a Hashtable bean object called
monitor, with application scope, in my Welcome page using

<jsp:useBean id="monitor" scope="application" class="java.util.Hashtable"/>

how do I access this application scope object in all of my Struts action
classes?  I've tried using 		Hashtable monitor =
(Hashtable)session.getAttribute(Constants.MONITOR_KEY);
if (monitor != null) {
			-----do something---
		}
and I get a null pointer error, as seen from my error log below:

01/14 18:30:47 error 
java.lang.NullPointerException
	at com.NYTC.struts.LogonAction.perform(LogonAction.java:144)

where line 144 is the if (monitor != null) statement.

My objective in using the monitor bean is to keep track of every user who
logs in, and if they still have an active session, recover any data and then
invalidate the old session, and create a new session for them.  We were
doing this in our previous web apps, but we were using a Model 1 approach,
so I am having some trouble converting this concept to a Model 2 approach.
In our Model 1 apps, we just used the monitor to keep track of user's
sessions, and invalidate any active session still on server as follows:

if (monitor.containsKey(user)){
	HttpSession oldSession = (HttpSession)monitor.get(user);
	System.out.println("User already has an active session in memory "+
oldSession.getId());
	---retrieve old data in between here and then invalidate old
session------
	oldSession.invalidate();
	System.out.println("Got rid of the old session");
} else {
	System.out.println("No old session was found");
}	 				
monitor.put(user, session);

How do I accomplish this using a Model 2 approach??

Thanks in advance for any insight/advice

Celeste




--
To unsubscribe, e-mail:
<mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail:
<mailto:struts-user-help@jakarta.apache.org>




--
To unsubscribe, e-mail:   <mailto:struts-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-user-help@jakarta.apache.org>


Mime
View raw message