zoukankan      html  css  js  c++  java
  • 过滤器

     

     

     

    过滤器

    把我们不需要的东西去除,其实就是给定一个规范把那个请求的一些条件筛选

    注意一定要转发,不然会卡死arg2.doFilter(req, resp);通过该方法把req请求和resp请求分开

    写一个类实现implements Filter,最重要的就是doFileter方法

    需要在xml配置然后就可以使用了

    xml中的配置

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0" metadata-complete="false">
      <display-name>2019_08_23_comprehensiveconnection</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
      <filter>
          <filter-name>test</filter-name>
          <filter-class>cn.jiedada.filter.FilterTest</filter-class>
          <init-param>
              <param-name>encoding</param-name>
              <param-value>UTF-8</param-value><!-- 在init中通过encoding获得UTF-8 -->
          </init-param>
      </filter>
      <filter-mapping>
          <filter-name>test</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>
      <filter>
          <filter-name>test1</filter-name>
          <filter-class>cn.jiedada.filter.LoginFilterTest</filter-class><!-- 通过该权限明获得过滤器 -->
      </filter>
      <filter-mapping>
          <filter-name>test1</filter-name>
          <url-pattern>/system/*</url-pattern><!-- 拦截范围为什么 -->
      </filter-mapping>
      <filter>
          <filter-name>test2</filter-name>
          <filter-class>cn.jiedada.filter.AddNullFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>test2</filter-name>
          <url-pattern>/add.user</url-pattern>
      </filter-mapping>
    </web-app>
    View Code

    空值判断过滤

    package cn.jiedada.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;
    
    public class AddNullFilter implements Filter{
    
        @Override
        public void destroy() {
            // TODO Auto-generated method stub
            
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2)
                throws IOException, ServletException {
            String name = req.getParameter("name");
            String pwd = req.getParameter("pwd");
            if(name==null || name.trim().equals("")){
                req.setAttribute("errorTrim", "用户名不能为空");
                req.getRequestDispatcher("/system/user_add.jsp").forward(req, resp);
                return;
            }else if(pwd==null || pwd.trim().equals("")){
                req.setAttribute("errorTrim", "密码不能为空");
                req.setAttribute("username", name.trim());
                req.getRequestDispatcher("/system/user_add.jsp").forward(req, resp);
                return;
            }
            //定一要转发
            arg2.doFilter(req, resp);
        }
    
        @Override
        public void init(FilterConfig arg0) throws ServletException {
            // TODO Auto-generated method stub
            
        }
    
    }
    View Code

    编码设置过滤器

    package cn.jiedada.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;
    
    public class FilterTest implements Filter{
        FilterConfig config;
        @Override
        public void destroy() {
        }
    
        @Override
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain arg2)
                throws IOException, ServletException {
            String encoding = config.getInitParameter("encoding");
            req.setCharacterEncoding(encoding);
            resp.setContentType("text/html;charset="+encoding);
            arg2.doFilter(req, resp);
        }
    
        @Override
        public void init(FilterConfig config) throws ServletException {
            // TODO Auto-generated method stub
            this.config=config;
        }
    
    }
    View Code
  • 相关阅读:
    快速排序算法(c#)
    NHibernate 中createSqlQuery的执行
    Asp.net页面下客户端按钮提交页面到其他Action
    希尔(插入)排序 c#代码
    Asp.net MVC 中冒号的作用
    Net注册JS的几种方式和区别
    Asp.net MVC 使用json数据格式交互
    DataSet的手工创建
    反射基础
    uva10082 WERTYU
  • 原文地址:https://www.cnblogs.com/xiaoruirui/p/11444928.html
Copyright © 2011-2022 走看看