过滤器之登陆过滤

次过滤器主要用于,判断用户是否登陆,以防直接访问应用中的网页进行提交

Filter 类的写法,

package  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. * ;

import  com.tarena.crm.domain.admin.Manager;

public   class  LoginFilter   extends  HttpServlet   implements  Filter {

    
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req
=(HttpServletRequest)request;
        HttpServletResponse res
=(HttpServletResponse) response;
        
if(req.getSession().getAttribute("manager")==null){
            res.sendRedirect(
"/crm/core/login.jsp");
        }
else
        chain.doFilter(request, response);        
    }


    
public void init(FilterConfig arg0) throws ServletException {
        
    }




}

 web.xml中的写法:

<url-pattern>/core/adminManage/*</url-pattern>对一个目录进行过滤

或者:

<url-pattern>/core/adminManage/test.jsp</url-pattern>对某一个特定网页进行过滤

     < filter >
        
< filter-name > setlog </ filter-name >
        
< filter-class > filter.LoginFilter </ filter-class >
    
</ filter >
    
< filter-mapping >
        
< filter-name > setlog </ filter-name >
        
< url-pattern > /core/adminManage/* </ url-pattern >
    
</ filter-mapping >

你可能感兴趣的:(manager,filter,null,Class,import)