随笔心得

记我所见,记我所想

BlogJava 首页 新随笔 联系 聚合 管理
  34 Posts :: 0 Stories :: 16 Comments :: 0 Trackbacks

在web.xml中的配置

 <filter><!--过滤器1 主要是对字符编码的处理 这里用的是spring框架的过滤器-->
        <filter-name>encodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>GB2312</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
   
    <filter><!--过滤器2 对session的过滤-->
        <filter-name>SessionFilter</filter-name>
        <filter-class>com.i5ic.common.filter.SessionFilter</filter-class>
     
    </filter>

<filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>
 <servlet-mapping>
  <servlet-name>iwicweb</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>



以下为过滤器2 的实现
package com.i5ic.common.filter;

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.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

//import cn.chiness.office.model.Employee;

public class SessionFilter implements Filter {

 private final String  loginPage = "/iwicweb";
 protected FilterConfig filterConfig = null;
 private Logger log = Logger.getLogger(SessionFilter.class);
 
 public void init(FilterConfig filterConfig) throws ServletException {
  // TODO Auto-generated method stub
  this.filterConfig = filterConfig;
 }

 public void doFilter(ServletRequest request, ServletResponse reponse,
   FilterChain filterDo){
  // TODO Auto-generated method stub
  log.info("Start Session-fileter");
  System.out.println("Start Session-fileter");
  HttpServletRequest httpRequest = (HttpServletRequest)request;
  HttpServletResponse httpResponse = (HttpServletResponse)reponse;
  try{
   HttpSession session = httpRequest.getSession(false);
   if(session == null)
   {
    System.out.println("Session is pass max Age!");
    httpResponse.sendRedirect(loginPage);
    return ;
   }
   
   Object loginEmp = (Object) session.getAttribute("loginInfo");
   if(loginEmp == null)
   {
    log.info("You are'nt login!");
    System.out.println("You are'nt login!");
    httpResponse.sendRedirect(loginPage);
   }
   else
   {
    log.info("Filter is Pass -->Success!");
    try {
     filterDo.doFilter(request,reponse);
    } catch (ServletException e) {
     
     log.info("filterDao.doFilter  exception throws ");
     e.printStackTrace();
    }
   }
  }catch(IOException ex){
   log.info(ex.getMessage());
   log.info("Session is not Validate!");
   System.out.println("Session is not Validate!");
   try {
    httpResponse.sendRedirect(loginPage);
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   
  }
  
 }

 public void destroy() {
  // TODO Auto-generated method stub
  this.filterConfig = null;
 }

}

posted on 2007-07-20 11:35 源自有缘 阅读(302) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: