HttpSessionEvent
修改会话对象时会触发HttpSessionEvent事件。此事件的相应监听器接口是HttpSessionListener。
我们可以在此事件中执行一些操作,例如计算总登录用户数和当前登录用户数,维护用户详细信息的日志,例如登录时间,注销时间等。
1 HttpSessionListener的方法
HttpSessionListener接口中声明了两种方法,必须由开发者实现这些方法才能执行某些操作。
- public void sessionCreated(HttpSessionEvent e): 创建会话对象时调用。
- public void sessionDestroyed(ServletContextEvent e): 会话无效时,该方法将被调用。
2 HttpSessionListener案例
在下面的示例中,我们使用HttpSessionListener监听器来统计总登录用户数和当前登录用户数。
2.1 编写页面
index.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>一点教程网-HttpSessionListener的使用</title>
<meta http-equiv="content-type" content="text/html" charset="UTF-8">
</head>
<body>
<form action="servlet1" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="userpass"><br>
<input type="submit" value="登录"/>
</form>
</body>
</html>
2.2 编写统计登录用户监听器
LoginUserListener:
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class LoginUserListener implements HttpSessionListener {
ServletContext ctx=null;
static int total=0,current=0;
public void sessionCreated(HttpSessionEvent e) {
total++;
current++;
ctx=e.getSession().getServletContext();
ctx.setAttribute("totalusers", total);
ctx.setAttribute("currentusers", current);
}
public void sessionDestroyed(HttpSessionEvent e) {
current--;
ctx.setAttribute("currentusers",current);
}
}
2.3 编写登录Servlet
LoginServlet:
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class LoginServlet extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String n=request.getParameter("username");
out.print("欢迎 "+n);
HttpSession session=request.getSession();
session.setAttribute("uname",n);
//retrieving data from ServletContext object
ServletContext ctx=getServletContext();
int t=(Integer)ctx.getAttribute("totalusers");
int c=(Integer)ctx.getAttribute("currentusers");
out.print("<br>总登录用户数= "+t);
out.print("<br>当前登录用户数= "+c);
out.print("<br><a href='logout'>注销</a>");
out.close();
}
}
2.4 编写注销Servlet
LogoutServlet:
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
* 一点教程网 - http://www.yiidian.com
*/
public class LogoutServlet extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException
{
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
HttpSession session=request.getSession(false);
session.invalidate();//会话失效
out.print("你已经成功注销登录");
out.close();
}
}
2.5 配置web.xml
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>LogoutServlet</servlet-name>
<servlet-class>LogoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LogoutServlet</servlet-name>
<url-pattern>/logout</url-pattern>
</servlet-mapping>
<!--配置监听器-->
<listener>
<listener-class>LoginUserListener</listener-class>
</listener>
</web-app>
2.6 运行测试
热门文章
优秀文章