码迷,mamicode.com
首页 > 其他好文 > 详细

Filter过滤器

时间:2017-06-05 17:57:17      阅读:285      评论:0      收藏:0      [点我收藏+]

标签:import   indexof   index   bean   tps   auto   use   ide   nbsp   

Filterservlet.java

 

package com.xxpx.servlet;

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 com.xxpx.bean.User;


public class FilterTest implements Filter{

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
            FilterChain arg2) throws IOException, ServletException {
        // TODO Auto-generated method stub
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        String requestURI = request.getRequestURI();
        int lastIndexOf = requestURI.lastIndexOf("/");
        String substring = requestURI.substring(lastIndexOf+1);
        if("login.jsp".equals(substring)){
            arg2.doFilter(servletRequest, servletResponse);
        }else{
            HttpServletResponse response = (HttpServletResponse)servletResponse;
            HttpSession session = request.getSession();
            User user = (User)session.getAttribute("user");
            if(user==null){
                response.sendRedirect("login.jsp");
            }else{
                arg2.doFilter(servletRequest, servletResponse);
            }
        }
        
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        System.out.println("开始过滤");
    }

}

web.xml配置

<filter>
   <filter-name>test</filter-name>
   <filter-class>com.xxpx.servlet.FilterTest</filter-class>
  </filter>
  <filter-mapping>
  <filter-name>test</filter-name>
  <url-pattern>/houtai/pages/*</url-pattern>
  <url-pattern>/houtai/main.jsp</url-pattern>
  </filter-mapping>

 

Filter过滤器

标签:import   indexof   index   bean   tps   auto   use   ide   nbsp   

原文地址:http://www.cnblogs.com/0280-hnn/p/6946158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!