2010.08.04——— servlet过滤器

2010.08.04——— servlet过滤器

参考: http://xiaopihai.iteye.com/blog/398561

今天写了一个filter 非常的简单 如下:

package util;

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

import org.apache.log4j.Logger;

import pojo.TLB_USER;


public class UserFilter extends HttpServlet implements Filter{

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest req = (HttpServletRequest)request;      
		//System.out.println(req.getServletPath());      

		HttpServletResponse res = (HttpServletResponse)response;      
		HttpSession ses = req.getSession();      
		TLB_USER user =(TLB_USER) ses.getAttribute("user"); 
		String uri = req.getRequestURI();
		
		if(user ==null){
			res.sendRedirect("/CPJW_2/index5.jsp");
			log.info("没有登录 "+uri+" 不能访问");
		}else{      
			chain.doFilter(request, response);
		} 
	}
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}
	
	private static Logger log = Logger.getLogger(UserFilter.class);

}




然后 配置web.xml

<?xml version="1.0" encoding="utf-8"?>
<web-app version="2.5" 
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 
 <!-- 配置spring的监听器 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <!-- 配置spring的监听器 -->
 
 <!-- spring的applicationContext载入 -->
 <context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml,
  			   classpath:applicationContext_zhiliangjiandu.xml,
  			   classpath:applicationContext_zhujiemian.xml,
  			   classpath:applicationContext_shipinMSG.xml,
  			   classpath:applicationContext_xitongweihu.xml,
  			   classpath:applicationContext_report.xml
  </param-value>
 </context-param>
 <!-- spring的applicationContext载入 -->
 
 <!-- log4j载入 -->
 <context-param>  
   <param-name>log4jConfigLocation</param-name>  
   <param-value>WEB-INF/classes/log4j.xml</param-value>  
 </context-param> 
 <!-- log4j载入 -->
 
 <!-- 用户的过滤器 -->
 <filter>
  	<filter-name>userFilter</filter-name>
  	<filter-class>util.UserFilter</filter-class>
 </filter>
 <filter-mapping>
  	<filter-name>userFilter</filter-name>
  	<url-pattern>/*</url-pattern>
 </filter-mapping>
 <!-- 用户的过滤器 -->
 
  
 
    
  <welcome-file-list>
    <welcome-file>index5.jsp</welcome-file>
  </welcome-file-list>
</web-app>


但是 登陆页面是不能过滤 还有一个判断登陆的action也不能过滤

但是 一时不知道怎么写 而且页面还有一些css 图片 js等也应该是不需要过滤 但是/*就是等于把所有的都过滤了 也就是说过滤的应该只有.jsp和.action


所以修改如下:


filter修改如下:

if(user ==null&&!(uri.contains("/index5.jsp")||uri.contains("/userlogin.action"))){
			res.sendRedirect("/CPJW_2/index5.jsp");
			log.info("没有登录 "+uri+" 不能访问");
		}else{      
			chain.doFilter(request, response);
		} 




web.xml

<!-- 用户的过滤器 -->
 <filter>
  	<filter-name>userFilter</filter-name>
  	<filter-class>util.UserFilter</filter-class>
 </filter>
 <filter-mapping>
  	<filter-name>userFilter</filter-name>
  	<url-pattern>*.jsp</url-pattern>
 </filter-mapping>
 <filter-mapping>
  	<filter-name>userFilter</filter-name>
  	<url-pattern>*.action</url-pattern>
 </filter-mapping>
 <!-- 用户的过滤器 -->















































你可能感兴趣的:(spring,log4j,jsp,xml,servlet)