过滤器和监听器实现用户的在线登录人数,以及设置session时长。

过滤器:

package com.bjsxt.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class InfoFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		//设置编码格式
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		//强转成httpservlet
		HttpServletRequest req=((HttpServletRequest)request);
		HttpServletResponse resp=((HttpServletResponse)response);
		//获取当前请求的URI
		String uri = req.getRequestURI();
		System.out.println("当前请求的URI:"+uri);
		//获取此次请求的method
		String method = req.getParameter("method");
		//放行登录页面:继续放行登录请求,静态资源
		if ("/55/login.jsp".equals(uri) || ("/55/data".equals(uri))&& "userLogin".equals(method) ||uri.startsWith("/55/css/") ||uri.startsWith("/55/js/") ||uri.startsWith("/55/images/")) {                                                     
			//放行
			chain.doFilter(request, response);
		}else {
			// 获取session对象
			HttpSession session = req.getSession();
			Object obj = session.getAttribute("User");
			//判断
			if (obj!=null) {
				//放行
				chain.doFilter(request, response);

			}else {
				resp.sendRedirect("/55/login.jsp");

			}
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}

 

监听器:

package com.bjsxt.servlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class MyListener implements HttpSessionListener,ServletContextListener{
	//监听application对象
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		int count=0;
		//获取application对象
		ServletContext sc = sce.getServletContext();
		sc.setAttribute("count", count);
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
	
	}
	
	//监听session对象
	@Override
	public void sessionCreated(HttpSessionEvent se) {
	//获取application对象中的计数器
		ServletContext sc = se.getSession().getServletContext();
		int count=(int)sc.getAttribute("count");
		//计数器自增
		++count;
		//将计数器存储到application中
		sc.setAttribute("count", count);
		
		
	}

	@Override
	public void sessionDestroyed(HttpSessionEvent se) {
		//获取application对象中的计数器
		ServletContext sc = se.getSession().getServletContext();
		int count=(int)sc.getAttribute("count");
		//计数器自减
		--count;
		//将计数器存储到application中
		sc.setAttribute("count", count);
		
	}
	
}

 

web.xml:




	MyFiltet
	com.bjsxt.filter.InfoFilter


	MyFiltet
	/*

  
  com.bjsxt.servlet.MyListener
  
  15-project
  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    UserServlet
    com.bjsxt.servlet.UserServlet
  
  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    OutServlet
    com.bjsxt.servlet.OutServlet
  
  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    RegServlet
    com.bjsxt.servlet.RegServlet
  
  
    This is the description of my J2EE component
    This is the display name of my J2EE component
    BaseServlet
    com.bjsxt.servlet.BaseServlet
  
 





  
    UserServlet
    /use
  
  
    OutServlet
    /out
  
  
    RegServlet
    /reg
  
  
    BaseServlet
    /data
  
  
    index.html
    index.htm
    index.jsp
    default.html
    default.htm
    default.jsp
  

 

过滤器和监听器实现用户的在线登录人数,以及设置session时长。_第1张图片

 

 

过滤器和监听器实现用户的在线登录人数,以及设置session时长。_第2张图片

在十秒之后session消除,再次点击需要重新登录:

过滤器和监听器实现用户的在线登录人数,以及设置session时长。_第3张图片

 

你可能感兴趣的:(JavaEE)