• ----------------------过滤器filter简单登录和乱码过滤----


    web.xml里面的代码

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" 
        xmlns="http://java.sun.com/xml/ns/javaee" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name></display-name>
    
      <!-- 编码过滤器 -->
      <filter>
        <filter-name>CharactorEncodingFilter</filter-name>
        <filter-class>cn.happy.filter.CharacterEncodingFilter</filter-class>
      </filter>
      
       <!-- 鉴权过滤器 -->
      <filter>
        <filter-name>SessionFilter</filter-name>
        <filter-class>cn.happy.filter.SessionFilter</filter-class>
        <!-- 初始化参数 -->
        <init-param>
           <param-name>execuldeUrl</param-name>
           <param-value>/servlet/(Login|Register)Servlet</param-value>
        </init-param>
      </filter>
      
      
      
      <!-- 按照书写先后顺序  ,执行顺序按照Sevlet-Mapping 书写顺序 -->
      <filter-mapping>
           <filter-name>CharactorEncodingFilter</filter-name>
           <url-pattern>/*</url-pattern>
      </filter-mapping>
      
      <!-- 需要权限的页面 -->
      <filter-mapping>
           <filter-name>SessionFilter</filter-name>
           <url-pattern>/jsp/*</url-pattern>
      </filter-mapping>
      
      <filter-mapping>
           <filter-name>SessionFilter</filter-name>
           <url-pattern>/servlet/*</url-pattern>
      </filter-mapping>
      
      <servlet>
        <description>This is the description of my J2EE component</description>
        <display-name>This is the display name of my J2EE component</display-name>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>cn.happy.servlet.LoginServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/servlet/LoginServlet</url-pattern>
      </servlet-mapping>
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
      </welcome-file-list>
    </web-app>

    servlet里面的代码

    package cn.happy.filter;
    
    import java.io.IOException;
    import java.util.regex.Pattern;
    
    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;
    
    public class SessionFilter implements Filter {
    	Pattern patternUrl;
        //能从配置文件获取内容
    	public void init(FilterConfig config) throws ServletException {
    		String url=config.getInitParameter("execuldeUrl");  ///servlet/LoginServlet
    		if(url!=null&&!url.equals("")){ //配置了例外
    			//为了获取到一个正则对象  
    			patternUrl= Pattern.compile(url); //我们定制的正则规范 /servlet/LoginServlet
    		}
    	}
    	
    	public void doFilter(ServletRequest request, ServletResponse response,
    			FilterChain chain) throws IOException, ServletException {
    		
    		HttpServletRequest req=(HttpServletRequest)request;
    		HttpServletResponse res=(HttpServletResponse)response;
    
    		//路径中包含 /servlet/LoginServlet  方向
    		String servletPath = req.getServletPath();
    		System.out.println(servletPath+"===========================");
    		if(patternUrl.matcher(servletPath).matches()){
    			chain.doFilter(request, response);
    			return;
    		}
    		
    		
    		Object sessionObj = req.getSession().getAttribute("uname");
    		if(sessionObj!=null){  //证明用户已经登录了
    			//放行
    			chain.doFilter(request, response);
    		}else{
    			System.out.println("session filter not session data");
    			//证明用户没有登录,返回到登录   /Jsp-Day05Filter
    			res.sendRedirect(req.getContextPath()+"/login.jsp");
    		}
    	}
    	public void destroy() {
    		
    	}
    
    	
    
    }
    

      

  • 相关阅读:
    vue中引用font-awesome
    创建VUE项目
    Core项目添加EF
    DBHelper,之前常用的代码,保存起来。
    appium+python的APP自动化(2)
    appium+python的APP自动化(1)
    单例模式
    双重检测机制解决缓存穿透问题
    mysql数据库
    java的三大特性----封装、集成、多态
  • 原文地址:https://www.cnblogs.com/laosunlaiye/p/7383832.html
Copyright © 2020-2023  润新知