使用(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是否超时的判断
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是JAVA通过使用filter过滤实现统计接口的调用次数的功能的步骤: 1.创建一个实现Filter接口的Java类,重写doFilter方法,在其实现统计接口调用次数的逻辑。具体代码如下: ```java public class CountFilter implements Filter { private int count = 0; @Override public void init(FilterConfig filterConfig) throws ServletException { // 初始化方法,可以在这里获取配置信息等 } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // 统计接口调用次数的逻辑 count++; System.out.println("接口调用次数:" + count); // 继续执行过滤链 chain.doFilter(request, response); } @Override public void destroy() { // 销毁方法,可以在这里释放资源等 } } ``` 2.在web.xml文件配置过滤和过滤映射。具体代码如下: ```xml <filter> <filter-name>countFilter</filter-name> <filter-class>com.example.CountFilter</filter-class> </filter> <filter-mapping> <filter-name>countFilter</filter-name> <url-pattern>/api/*</url-pattern> </filter-mapping> ``` 上述代码filter-name指定了过滤的名称,filter-class指定了过滤的Java类名。filter-mappingfilter-name指定了要映射的过滤名称,url-pattern指定了要过滤的URL模式。 3.在需要统计调用次数的接口上添加注解@WebFilter。具体代码如下: ```java @WebFilter(filterName = "countFilter", urlPatterns = "/api/*") public class MyServlet implements Servlet { // servlet实现代码 } ``` 上述代码filterName指定了要使用的过滤名称,urlPatterns指定了要过滤的URL模式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值