SSM 过滤器的配置 Filter

<!-- 过滤器拦截指定后缀-->	
  	<!-- //localhost:8080/sanzu/test/****都会被拦截 -->
	<filter>
		 <filter-name>SessionFilter</filter-name>
		 <filter-class>com.jk.login.filter.MyFilter</filter-class>
		 <init-param>   
	          <param-name>charset</param-name>   
          	  <param-value>UTF-8</param-value>   
	      </init-param>   
	      <init-param>   
	          <param-name>contentType</param-name>   
	          <param-value>text/html;charset=UTF-8</param-value>   
	      </init-param>   
	</filter>
	
	<filter-mapping>
		 <filter-name>SessionFilter</filter-name>
		 <!-- 要拦截的哪一级目录 -->
		 <url-pattern>/test/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置session超时时间,单位分钟 -->
	<session-config>
		<session-timeout>30</session-timeout>
	</session-config>

web-info/web.xml配置,并配置相应的interceptor类

相应的过滤器类

package com.jk.login.filter;

import java.io.IOException;
import java.io.PrintWriter;

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 org.apache.log4j.Logger;


public class MyFilter implements Filter{

	private static final Logger logger = Logger.getLogger(MyFilter.class);
	
	public void destroy() {
		System.out.println("销毁过滤器方法");
		logger.info("销毁过滤器方法");
	}

	
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
			throws IOException, ServletException {
		
		HttpServletResponse response = (HttpServletResponse) res;
		HttpServletRequest  request=(HttpServletRequest) req;
        String servletPath = request.getServletPath();
        logger.info("请求路径过滤信息路径为:" + servletPath);
        
        //访问login.jsp时,才放过,并且login.jsp的后续操作,继续执行,不拦截
        if (servletPath.equals("/login.jsp")  ) {
            chain.doFilter(req, res);
            return;
		}else {
			String contextPath=request.getContextPath();
 	        response.setCharacterEncoding("utf-8");
		    request.setCharacterEncoding("utf-8");
			response.setContentType("text/html");
			PrintWriter pw = response.getWriter();
			//否则拦截,跳转指定的页面
			pw.print("<script>window.top.location.href='"+contextPath + "/aa.jsp'"+";</script>");
		}
		
	}

	
	public void init(FilterConfig arg0) throws ServletException {
		System.out.println("初始化过滤器的方法");
		logger.info("初始化过滤器的方法");
	}
	

}



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值