使用(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是否超时的判断
通过配置SessionFilterImpl过滤器实现对Session是否过时的全局检查。该过滤器在web.xml中定义,并应用于所有请求路径,自动重定向未登录用户至登录页面。
2091

被折叠的 条评论
为什么被折叠?



