|
Page 5 of 5
Now, the most important thing is that we need to make the class “implements” the ServletContextListener. So here is my latest SessionListener.java.
Now, we have an error in our class. Why? Because once we try to implement the javax.servlet.HttpSessionListener interface, we need to define a few implementation methods for it. That’s why I mentioned earlier that it is the pre-defined interfaces. This is where our job will be started. You need to add two methods for it.
- public void sessionCreated(HttpSessionEvent arg0)
- public void sessionDestroyed(HttpSessionEvent arg0)
So here is my latest SessionListener.java
public class SessionListener implements javax.servlet.http.HttpSessionListener {
public void sessionCreated(HttpSessionEvent arg0) {
System.out.println("Session is created");
}
public void sessionDestroyed(HttpSessionEvent arg0) {
System.out.println("Session is destroyed");
}
}
|
Okay, if you add one attribute to the session, the session will be created for you and it should print out “Session is created”. Once you invalidate the session, the session should be destroyed and “Session is destroyed” will be printed out.
Now, we need to add one more entry in our web.xml.
<listener>
<listener-class>com.mycompany.listener.SessionListener</listener-class>
</listener>
One thing you need to know that this listener entry MUST be before the <servlet> entry (if any). So here is my latest web.xml looks like. Yes, Listener only needs <listener></listener>
So this is how my latest web.xml looks like after implementing both ServletContextListener and HttpSessionListener.
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>
ListenerWebApp
</display-name>
<listener>
<listener-class>com.mycompany.listener.ServletListener</listener-class>
</listener>
<listener>
<listener-class>com.mycompany.listener.SessionListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
Ok, actually we have completed our SessionListener. The only thing is that how to test it. There is one thing you can do. Create one JSP on your web application and we will assign some attributes to the sessions (adding attribute means that we are creating the Session). Create your JSP using the wizard and name it whatever you want. For my case, I would like to name it testSession.jsp.
Ok, now let’s add some codes to add attribute to the session. We will achieve it using Scriptlet. Scriptlet is basically Java codes that are embedded in our JSP. It helps us in creating our application dynamic. So please add below codes in your JSP.
<%
System.out.println("Adding attribute to Session...");
request.getSession().setAttribute("test", "test");
System.out.println("Adding attribute to Session completed!");
System.out.println("Removing Session...");
request.getSession().invalidate();
System.out.println("Removing Session completed!");
out.println("Successfully adding and removing attributes from Session");
%>
This scriptlet can be placed anywhere in your JSP. For this example, I can just put it within my <body></body> tag. If you are in the right track, it should look like below screenshot.
Let’s test it. Export your project into WAR files and deploy it to Tomcat. Access the testSession.jsp that we just created and carefully look at the Tomcat console.
As expected, when the first time Tomcat is loaded, our ServletContextListener is still executed.
Now, open your browser and let’s try to execute the testSession.jsp by typing the URL manually into your browser. In my case, it should be http://localhost:8080/ListenerWebApp/testSession.jsp.
This is my Tomcat console now.
Conclusion
Well, you should have successfully implemented both ServletContextListener as well as HttpSessionListener. These two listeners are the most widely used and I hope that by reading the tutorial, you have a clear way and objective when you are moving forward with J2EE programming. There are still much to be learnt and please do not worry as this is not hard. My suggestion is to try the sample in this tutorial and try to customize it. Your skills will be much improved by practicing more. There are also two additional Listeners that are not mentioned in this tutorial which are ServletContextAttributeListener and HttpSessionAttributeListener. They are just slightly different with our tutorial. Have a look at them and try them out.
You can find the Eclipse project for the tutorial source codes here.
The README file for the sources is available here.
Related Tips
<< Start < Prev 1 2 3 4 5 Next > End >> |
You can share your information about this topic using the form below!
Please do not post your questions with this form! Thanks.