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

阅读更多

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

    web.xml文件内容如下:



  
     sf
     com.pdw.websystem.SessionFilterImpl
  
  
    sf
    /*
  
  
    action
    org.apache.struts.action.ActionServlet
    
      config
      /WEB-INF/struts-config.xml
    
    
      debug
      3
    
    
      detail
      3
    
    0
  

  
    action
    *.do
  
  
   2
  
  
   com.pdw.websystem.SessionListenerImpl
  

 

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是否超时的判断

你可能感兴趣的:(Servlet,Struts,Web,XML,SUN)