使用(Filter)过虑器实现对Session是否过时的判断

通过配置SessionFilterImpl过滤器实现对Session是否过时的全局检查。该过滤器在web.xml中定义,并应用于所有请求路径,自动重定向未登录用户至登录页面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用(Filter)过虑器实现对Session是否过时的判断
 
 
在开发时,在第个页面,或者BaseAction对Session是否超时正行判断,但随着项目越来越来.页面很的时候写起来会觉得烦,由于项目需要最近做了一个这样的例子。

    web.xml文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
<filter>
     
<filter-name>sf</filter-name>
     
<filter-class>com.pdw.websystem.SessionFilterImpl</filter-class>
  
</filter>
  
<filter-mapping>
    
<filter-name>sf</filter-name>
    
<url-pattern>/*</url-pattern>
  
</filter-mapping>
  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    
<init-param>
      
<param-name>config</param-name>
      
<param-value>/WEB-INF/struts-config.xml</param-value>
    
</init-param>
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>3</param-value>
    
</init-param>
    
<init-param>
      
<param-name>detail</param-name>
      
<param-value>3</param-value>
    
</init-param>
    
<load-on-startup>0</load-on-startup>
  
</servlet>

  
<servlet-mapping>
    
<servlet-name>action</servlet-name>
    
<url-pattern>*.do</url-pattern>
  
</servlet-mapping>
  
<session-config>
   
<session-timeout>2</session-timeout>
  
</session-config>
  
<listener>
   
<listener-class>com.pdw.websystem.SessionListenerImpl</listener-class>
  
</listener>
</web-app>


SessionFilterImpl.java文件如下:

package com.pdw.websystem;


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.UnavailableException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public class SessionFilterImpl implements Filter {


    
protected String encoding = null;


    
protected FilterConfig filterConfig = null;


    
protected boolean ignore = true;

    
public void destroy() {

        
this.encoding = null;
        
this.filterConfig = null;

    }



    
/**
     * Select and set (if specified) the character encoding to be used to
     * interpret request parameters for this request.
     *
     * 
@param request The servlet request we are processing
     * 
@param result The servlet response we are creating
     * 
@param chain The filter chain we are processing
     *
     * 
@exception IOException if an input/output error occurs
     * 
@exception ServletException if a servlet error occurs
     
*/

    
public void doFilter(ServletRequest request, ServletResponse response,
                         FilterChain chain)
 
throws IOException, ServletException {
    
     HttpServletRequest hrequest
=(HttpServletRequest)request;
     HttpServletResponse hresponse
=(HttpServletResponse)response;
     HttpSession session
=hrequest.getSession();
     
if(session.getAttribute("name")==null){
      hresponse.sendRedirect(
"logon.jsp");
     }

        chain.doFilter(request, response);

    }



    
/**
     * Place this filter into service.
     *
     * 
@param filterConfig The filter configuration object
     
*/

    
public void init(FilterConfig filterConfig) throws ServletException {

    }

}



 

使可轻松实现对Session是否超时的判断
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值