How would I implement events “On Application Start” and “On Session Start” using JSP?

Ronald Roberts

Answer for IBM WebSphere Application Server 3 (others should be similar):

For OnAppStart, you can extend the JSP processor:

import javax.servlet.*;
import javax.servlet.http.*;
public class MyJspServlet extends com.sun.jsp.runtime.JspServlet {
	public void init() {
		System.out.println(" *** [init] *** I am being called because the AppServer loaded JSP Processor.");
	}
}

You need to modify the WebApp of the AppServer to recognize that you want this Processor to handle the JSPs. In WebSphere, (from Topology tab) choose the AppServer, then the WebApp in question, then choose the JSPServlet, and change the Servlet Class Name to MyJspServlet.

For OnSessionStart, you can use the HttpSessionBindingListener:

... in JSP or servlet ...

session.putValue("bindings.listener",new MyListener(getServletContext()));

... in a separate file ...

import javax.servlet.http.*;
import javax.servlet.*;
public class MyListener implements HttpSessionBindingListener
{
	ServletContext context;
	public ODSessionListener(ServletContext context)
	{
		this.context = context;
	}
	public void valueBound(HttpSessionBindingEvent event)
	{
		// called when this class is put [putValue] into the session
		System.out.println(" *** [valueBound] *** Someone just bound my listener to a session!");
	}
	public void valueUnbound(HttpSessionBindingEvent event)
	{
		// called when this class is removed [removeValue or invalidate] from the session
		System.out.println(" *** [valueBound] *** Someone just bound my listener to a session!");
	}
}
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact