javaweb过滤器配置

过滤器从url开始过滤,即便程序入口是html页面也能完成跳转

 

核心代码

package com.cyboil.utils;

 

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;

 

public class SessFilter

implements Filter

{

public void init(FilterConfig filterConfig)

throws ServletException

{

}

 

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException

{

System.out.println("过滤器启动!!");

HttpServletRequest servletRequest = (HttpServletRequest)request;

HttpServletResponse servletResponse = (HttpServletResponse)response;

HttpSession session = servletRequest.getSession();

 

String path = servletRequest.getRequestURI();

System.out.println("path==================" + path);

 

String empId = (String)session.getAttribute("userName");

System.out.println("empId=================" + empId);

if (path.indexOf("AnalogLogin") > -1) {

chain.doFilter(servletRequest, servletResponse);

return;

}

 

if (empId == null)

{

servletResponse.sendRedirect("http://localhost:46623/Login.aspx");

}

else

chain.doFilter(request, response);

}

 

public void destroy()

{

}

}

 

web.xml配置

This is the description of my J2EE component

This is the display name of my J2EE component

SessFilter

com.cyboil.utils.SessFilter

SessFilter

/coupon/*

 

 

你可能感兴趣的:(javaweb过滤器配置)