session的使用
Posted h2503652646
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了session的使用相关的知识,希望对你有一定的参考价值。
一、截图
二、代码
SessionCounter.java
package hedong.com; import javax.servlet.ServletContext; import javax.servlet.annotation.WebListener; import javax.servlet.http.HttpSessionEvent; import javax.servlet.http.HttpSessionListener; /** * Application Lifecycle Listener implementation class SessionCounter * */ @WebListener public class SessionCounter implements HttpSessionListener { private static int onlinePeople = 0; /** * Default constructor. */ public SessionCounter() { // TODO Auto-generated constructor stub } /** * @see HttpSessionListener#sessionCreated(HttpSessionEvent) */ public void sessionCreated(HttpSessionEvent se) { // TODO Auto-generated method stub onlinePeople++; } /** * @see HttpSessionListener#sessionDestroyed(HttpSessionEvent) */ public void sessionDestroyed(HttpSessionEvent se) { // TODO Auto-generated method stub if(onlinePeople>0) { onlinePeople--; } } public static int getOnlinePeople() { return onlinePeople; } }
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <display-name>SwpuCS</display-name> <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> <listener> <listener-class> hedong.com.SessionCounter </listener-class> </listener> </web-app>
HomePage.jsp
`````` 在线人数为:<%=SessionCounter.getOnlinePeople() %> ``````
以上是关于session的使用的主要内容,如果未能解决你的问题,请参考以下文章
一个队asp.net session进行了再次封装的C#类的代码
hibernate在使用getCurrentSession时提示no session found for current thread