• 过滤器与监听器


    Filter

    一个 Filter 程序就是一个 Java 类,这个类必须实现 Filter 接口。javax.servlet.Filter 接口中定义了三个方法:init、doFilter、destory。

    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    
    //控制uri访问权限
    @WebFilter(filterName = "UrlFilter",urlPatterns = {"/admin.jsp"})
    public class UrlFilter implements Filter {
        public void destroy() {
            System.out.println("过滤结束。。");
        }
    
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
            HttpServletResponse response = (HttpServletResponse)resp;
            HttpServletRequest  request = ( HttpServletRequest)req;
            //向下转型
            String user = (String)request.getSession().getAttribute("user");
            request.getRequestURI();
            //如果用户名为空,返回原页面
            if(user==null){
                response.sendRedirect("/index.jsp");
            }
            //不为空  就放行
            chain.doFilter(req, resp);
        }
        public void init(FilterConfig config) throws ServletException {
            System.out.println("开始过滤");
        }
    }
    import javax.servlet.*;
    import javax.servlet.annotation.WebFilter;
    import java.io.IOException;
    //过滤字符    /*代表根目录所有的都过滤  多个urlPatterns = "{"",""}"
    @WebFilter(filterName = "MyFilter",urlPatterns = "/*")
    public class MyFilter implements Filter {
        public void destroy() {
            System.out.println("过滤结束。。");
        }
    
        public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
            req.setCharacterEncoding("UTF-8");
            resp.setContentType("UTF-8");
            System.out.println("执行过滤");
            chain.doFilter(req, resp);
        }
    
        public void init(FilterConfig config) throws ServletException {
            System.out.println("开始过滤");
        }
    }
    //在web.xml配置filter与应射   (servlet3.0以上,tomact版本支持的话,这种方式可以用注解代替)
    <
    filter> <filter-name>FirstFilter</filter-name> <filter-class>FirstFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>GB2312</param-value> </init-param> </filter> <filter-mapping> <filter-name>FirstFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>

    Servlet3.0提供注解(如以上两个例子),这样我们在web应用中使用监听器过滤器等时,不再需要在web.xml文件中配置监听器的相关描述信息了。

    执行顺序 :如果在web.xml配置就按照xml的顺序来。如果在filter中注解配置,按照字符集编码的首字母顺序来执行

    Listener

    https://blog.csdn.net/menghuanzhiming/article/details/79042182

  • 相关阅读:
    linux | 管道符、输出重定向
    php 升级php5.5 、php7
    mysql 启动失败
    centos7.2安装phpmyadmin
    php file_get_contents失败[function.file-get-contents]: failed to open stream: HTTP request failed!解决
    go println与printf区别
    前端 head 中mate 详解
    centos 7 安装mysql
    iOS数据持久化—数据库SQLite模糊查询
    C 语言字符串和格式化输入与输出
  • 原文地址:https://www.cnblogs.com/jiayiblog/p/11049428.html
Copyright © 2020-2023  润新知