• Filter过滤器 不登陆无法访问其他页面


    package com.cscy.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.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class LoginFilter implements Filter {
    
    	@Override
    	public void destroy() {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void doFilter(ServletRequest request, ServletResponse response,    
    			FilterChain chain) throws IOException, ServletException {  
    		// 获得在下面代码中要用的request,response,session对象   
    		HttpServletRequest servletRequest = (HttpServletRequest) request;   
    		HttpServletResponse servletResponse = (HttpServletResponse) response;   
    		HttpSession session = servletRequest.getSession();   
    		// 获得用户请求的URI   
    		String path = servletRequest.getRequestURI();   
    		//System.out.println(path);   
    		// 从session里取信息   
    		String name = (String) session.getAttribute("name");     
    		// 登陆页面无需过滤   
    		if(path.indexOf("/login.jsp") > -1) {    
    			chain.doFilter(servletRequest, servletResponse);    
    			return;   }   
    		// 判断如果没有取到信息,就跳转到登陆页面   
    		if (name == null || "".equals(name)) {    
    			// 跳转到登陆页面    
    			servletResponse.sendRedirect("/dome/login.jsp");  
    			} else {    
    				// 已经登陆,继续此次请求    
    				chain.doFilter(request, response);   				
    		}
    	}
    
    	@Override
    	public void init(FilterConfig filterConfig) throws ServletException {
    		// TODO Auto-generated method stub
    		
    	}
    
    }
    

      

    <!-- 配置 登陆 过滤器 -->
        <filter>
            <filter-name>login</filter-name>
            <filter-class>com.cscy.Filter.LoginFilter</filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>login</filter-name>
            <url-pattern>*.jsp</url-pattern>
    <!--此处写/*代表浏览器地址栏中的所有请求都会被拦截 包括css文件和js文件 .jsp这是仅拦截以.jsp结尾的请求 -->
        </filter-mapping>
    

      

  • 相关阅读:
    POJ 1466 最大独立点集
    POJ 3159 差分约束
    POJ 3411 DFS
    POJ 2665 模拟,,
    POJ 3134 Power Calculus ID-DFS +剪枝
    POJ 1543 暴搜
    455. Assign Cookies
    715. Range Module
    530. Minimum Absolute Difference in BST
    493. Reverse Pairs(BST, BIT, MergeSort)
  • 原文地址:https://www.cnblogs.com/songjiabo/p/9117537.html
Copyright © 2020-2023  润新知