JavaWeb HttpSession应用

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package com.xzit.erpmodel.listeners;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
 * 监听HttpSession 公共监听器
 * @author zengy
 *
 */

public class HttpSessionListenerImp implements HttpSessionListener {

    @Override
    public void sessionCreated(HttpSessionEvent arg0) {
        // TODO Auto-generated method stub
        HttpSession sess = arg0.getSession(); //获取新建的Session对象
        ServletContext context =  sess.getServletContext();    //获取ServletContext上下文对象
        
        Object obj = context.getAttribute("onlineNum");    //获取ServletContext上下文域中存储在线人数的属性
        if(obj == null) {
            context.setAttribute("onlineNum", Integer.valueOf(1)); //这是第一个登录成功的人
        }else {
            //在线人数增加1
            context.setAttribute("onlineNum", Integer.valueOf(Integer.parseInt(obj.toString()))+1);
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent arg0) {
        HttpSession sess = arg0.getSession(); //获取新建的Session对象
        ServletContext context =  sess.getServletContext();    //获取ServletContext上下文对象
        
        // TODO Auto-generated method stub
        Object obj = context.getAttribute("onlineNum");    //获取ServletContext上下文域中存储在线人数的属性
        context.setAttribute("onlineNum", Integer.valueOf(Integer.parseInt(obj.toString()))-1);
    }

}
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.xzit.erpmodel.users.domain.*"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
        <link type="text/css" rel="stylesheet" href="css/base.css" />
    
<div style="width:100%;height:120px;margin-left:0px;margin-top:0px;background-color:#eeeeff;" >
    <a href="worker.jsp">
    <img alt="" src="imgs/top3.png" width="100%" height="120px">
    </a>
</div>
<div class="f_0">
    <label class="hand">
        快 速 留 言&nbsp;|&nbsp;
        个 人 中 心&nbsp;|&nbsp;
        留 言  管 理&nbsp;|&nbsp;
        总 部 门 户&nbsp;|&nbsp;
        会 议 信 息&nbsp;|&nbsp;
        资 源 中 心&nbsp;&nbsp;
    </label>    
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;    
    <%
        Object obj = session.getAttribute("currentUser");
        SysUsers user = null;
        if(obj != null){
            user = (SysUsers)obj;
            %>
        当前用户:
        <label class="hand"><%=user.getName()%>&nbsp;&nbsp;
        <%
            if(session.isNew()){
                %>
                    &nbsp;&nbsp;首次光临
            <%}
        %>
            
        
        </label>
        &nbsp;&nbsp;
        <%
            Object lineNum = application.getAttribute("onlineNum");
            if(lineNum != null){%>
                
                当前在线用户<%=lineNum%><%}
        %>
        <label class="hand" style="color:graytext;">
            退出系统
        </label>&nbsp;&nbsp;&nbsp;&nbsp;
        <%    
        }else{%>
            未登录&nbsp;&nbsp;&nbsp;&nbsp;
        <%}
    %>
</div>
<?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>erpmms</display-name>
  
  <!-- 在当前web应用程序配置文件web.xml中设置Session过期时间(分钟) -->
  <session-config>
        <session-timeout>10</session-timeout>
  </session-config>
  
  <listener>
      <listener-class>com.xzit.erpmodel.listeners.HttpSessionListenerImp</listener-class>
  </listener>
  
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

posted @ 2022-02-06 20:40  伊万  阅读(31)  评论(0编辑  收藏  举报