欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

session过期过滤器,servlet写的,应用在struts上 ServletStrutsWebJSPXML 

程序员文章站 2022-05-29 23:17:22
...
这一层是servlet的过滤器做的~~~

当然还是要web.xml配置一下.
  <filter>
    <filter-name>accessControl</filter-name>
    <filter-class>web.common.AccessControlFilter</filter-class>
    <init-param>
      <param-name>loginPage</param-name>
      <param-value>/usermanager/login_nasp.jsp</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>accessControl</filter-name>
    <url-pattern>*.do</url-pattern>
  </filter-mapping>



过滤掉了所有的do的文件提交时,都会先检测一下session是否存在,是否过期.

当然要写一个servlet类,如下:
package web.common;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class AccessControlFilter
    implements Filter {
  protected FilterConfig filterConfig;
  private String loginPage;

  public void init(FilterConfig config); throws ServletException {
    this.filterConfig = config;
    loginPage = config.getInitParameter("loginPage");;
    if (loginPage == null); {
      throw new ServletException("loginPage init param missing");;
    }
  }
  public void doFilter(final ServletRequest req, final ServletResponse
                       res, FilterChain chain); throws IOException,
      ServletException {
    HttpServletRequest hreq = (HttpServletRequest); req;
    HttpServletResponse hres = (HttpServletResponse); res;
    String isLog = (String); hreq.getSession();.getAttribute("validsession");
    if ( (isLog != null); && ( (isLog.equals("true");); || (isLog == "true"););); { //检查是否登录
      chain.doFilter(req, res);;
      return;
    }
    else
      hres.sendRedirect(loginPage);; //如果没有登录,把视图派发到登录页面
  }
  public void destroy(); {
    this.filterConfig = null;
  }

  public void setFilterConfig(final FilterConfig filterConfig); {
    this.filterConfig = filterConfig;
  }
}


tracy_katrina