Solution:
The files used in this exercise are:
1. session.jsp
2. web.xml
3. SessionCount.java
<html>
<head>
<title>Session</title>
</head>
<body>
<h1>Session</h1>
There are currently
<%=com.java2s.SessionCount.getNumberOfSessions()%> active sessions.
</body>
</html>
Enter the above code in Notepad, and save the file as ‘Session.jsp’ in %TOMCAT_HOME%/webapps/ counter.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<listener>
<listener-class>com.SessionCount</listener-class>
</listener>
<taglib>
<taglib-uri>http://java.sun.com/jstl/core</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
</web-app>
Enter the code in Notepad, and save the file as ‘web.xml’ in %TOMCAT_HOME%/webapps/ counter/WEB-INF.
package com;Enter the above Java code in Notepad, and save the file as ‘SessionCount.java’. Compile the file from command prompt, and copy the class file in %TOMCAT_HOME%/webapps/counter/ WEB-INF/classes/com.
import javax.servlet.http.*;
public class SessionCount implements HttpSessionListener
{
private static int numberOfSessions = 0;
public void sessionCreated (HttpSessionEvent evt)
{
numberOfSessions++;
}
public void sessionDestroyed (HttpSessionEvent evt)
{
numberOfSessions--;
}
public static int getNumberOfSessions()
{
return numberOfSessions;
}
}
The output of the program is as shown in the Figure 12.3.
0 nhận xét:
Post a Comment