`

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

阅读更多

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

分享到:
评论

相关推荐

    使用dwr2.0实现服务器推客户端

    在本文中,我们将深入探讨如何使用Direct Web Remoting (DWR) 2.0来实现服务器推技术,以便实时地将信息传递给客户端...此外,DWR 2.0已有些许过时,现在可能更多地使用WebSocket或其他现代推送技术来实现服务器推送。

    nginx官方文档中文版

    Nginx 是一个开源的 Web 服务器软件,可以作为 Web 服务器、反向代理服务器、缓存服务器、负载均衡器和媒体流服务器等。下面是 Nginx 官方文档中文版的知识点总结: 安装和配置 * 从源码构建 Nginx * Nginx 初学者...

    阿里巴巴前端笔试题1

    }`判断每个键对应的值是否大于2,从而筛选出符合要求的键名。 #### 三、问答题解析 ##### 9. 请实现一个fibonacci函数,要求其参数和返回值如下所示: ```javascript /** * @desc: fibonacci * @param: count {...

    PHP学习笔记 用户注册模块用户类以及验证码类

    需要注意的是,这里的代码使用了旧版的MySQL扩展,而非更安全的mysqli或PDO,实际开发中应避免使用过时的数据库API。 **验证码类** 虽然在提供的信息中没有具体提及验证码类的代码,但通常验证码类会包含以下功能:...

    零基础Java学习路线.md

    同时,后端技术如Servlet、Filter、Listener、Session、Cookie、JSP、EL表达式和JSTL也需要学习。在这一阶段,理解并掌握新的模板引擎技术,如Freemarker和Thymeleaf,来替代那些已经过时的技术如JSP,是十分必要的...

    jpivot学习总结.doc

    approxRowCount 该属性可以用来提高性能,可以通过指定一个数值以减少判断级别、层次、维度基数的时间,该属性在通过使用 XMLA 连接 Mondrian 很有用处。 caption 标题 , 在表示层显示的 captionColumn 用来...

    com.educacionit.java.standard.8.class.27993:标准JAVA

    此外,Java 8对日期和时间API进行了重大改进,引入了`java.time`包,替代了过时的`java.util.Date`和`java.util.Calendar`。新的API更加易用,更符合实际需求,提供了如`LocalDate`、`LocalTime`和`LocalDateTime`等...

Global site tag (gtag.js) - Google Analytics