zoukankan      html  css  js  c++  java
  • Servlet Java Web开发(6) 内置对象, 监听器和过滤器

    内置对象

    Jsp 九大内置对象:无需创建可以直接使用的9个对象。

     out//等同于response.getWriter();向客户端发送数据
    config//对应Servlet中ServletConfig对象
    page//转换成Servlet后的this对象
    pageContext//当前页面的上下文,范围最小的域对象,配置属性,获取其他内置对象
    exception//仅在错误页面中可以使用
    request//类HttpServletRequest的对象
    response//类HttpServletResponse的对象
    application//ServletContext类的对象
    session//类HttpSession的对象

     <jsp:include page ="xxx.jsp"/>

    <jsp:forward page="xxx.jsp"/>是运行时的请求包含和请求转发。

    <%@include file="xxx.jsp"%>是编译期的文件包含

    EL表达式的11个内置对象

    pageScope,requestScope,sessionScope,applicationScope,param,paramValues;header;headerValues;initParam;cookie;pageContext;

    基本都是Map类型,只做读操作

    其中有四个jsp内置对象

    pageScope//${pageScope.name}等同于pageContext.getAttribute(“name”);
    requestScope//${requestScope.name}等同于request.getAttribute(“name”);
    sessionScope//${sessionScope.name}等同于session.getAttribute(“name”);
    applicationScope//${applicationScope.name}等同于application.getAttribute(“name”);

    全域查找:

    ${person}表示依次在pageScope、requesScopet、sessionScope、appliationScope四个域中查找名字为person的属性。

    其他EL内置对象解释

    param
    //${param.username}等效于request.getParameter("username");
    //如果参数不存在,返回空字符串,不是null
    
    
    header
    //获取请求头中信息,比如${header.Host}
    
    initParam//对应了web.xml中的 <context-param>

    监视器 

     在三大域ServletContext,HttpSession和ServletRequest在如下事情发生时,做出响应。

    下面以ServletContext为例,其他2个类似。

    1创建和销毁

    创建类,实现接口ServletContextListener

    public class AListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            //................
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            //.......................
        }
    }

    在web.xml中添加

    <listener>
        <listener-class>com.hello.AListener</listener-class>
      </listener>

    2.属性操作。(添加属性,修改属性,移除属性)

    创建类,实现接口ServletContextAttributeListener

    public class BListener implements ServletContextAttributeListener {
        public void attributeAdded(ServletContextAttributeEvent scab) {
            System.out.println("您向application中添加了一个名为" + scab.getName() + ", 值为:"
                    + scab.getValue() + "的属性");
        }
    
        public void attributeReplaced(ServletContextAttributeEvent scab) {
            System.out.println(scab.getName() + "=" + scab.getValue() + ", "
                    + scab.getServletContext().getAttribute(scab.getName()));
        }
    
        public void attributeRemoved(ServletContextAttributeEvent scab) {
            System.out.println(scab.getName() + "=" + scab.getValue());
        }
    }

    在web.xml添加

      <listener>
        <listener-class>com.hello.BListener</listener-class>
      </listener>

    过滤器

    过滤器的操作

    先定义一个类实现Filter接口,然后在web.xml配置该Filter

    public class AFilter implements Filter {
        /**
         * 创建之后马上执行,用来做初始化!
         */
        public void init(FilterConfig filterConfig) throws ServletException {
        }
    
        /**
         * 每次过滤时都会执行
         */
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            System.out.println("AFilter#start");
            chain.doFilter(request, response);//放行!
            System.out.println("AFilter#end");
        }
    
        /**
         * 销毁之前执行,用来做对非内存资源进行释放
         */
        public void destroy() {
        }
    }
    <filter>
          <filter-name>AFilter</filter-name>
          <filter-class>com.hello.AFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>AFilter</filter-name>
          <url-pattern>/AServlet</url-pattern>
         <dispatcher>FORWARD</dispatcher>
            <dispatcher>REQUEST</dispatcher>
      </filter-mapping>

    注意:1Filter类是全局单例,每一个类有仅有一个对象。

        2.每一个Filter作用于一个URL,如果一个URL被多个Filter作用,该URL上的filter按照web.xml中Filter出现的

    顺序依次执行。Filter类中的chain.doFilter方法就是让过滤链上的下一个Filter作用。

       3.<dispatcher>配置可以没有,(此时默认为REQUEST)可以是REQUEST,FORWARD,INCLUDE,ERROR中若干组合。

    表明该URL以这种方式被访问时,Filter才会作用。

  • 相关阅读:
    示例vue 的keep-alive缓存功能的实现
    解析Vue.js中的computed工作原理
    CentOS7.2 问题收集 查看文件大小 查看端口
    Docker 配置阿里云镜像加速器
    CentOS7.2中systemctl的使用
    CentOS7.2 安装Docker
    Java 多线程中的任务分解机制-ForkJoinPool,以及CompletableFuture
    IntelliJ IDEA 在运行web项目时部署的位置
    Mysql相关问题收集
    Java命令使用 jmap,jps,jstack,jstat,jhat,jinfo
  • 原文地址:https://www.cnblogs.com/legion/p/9407914.html
Copyright © 2011-2022 走看看