java web 由 web.xml 联想

(参考其他资料摘抄)

WEB容器启动,会为每个web应用创建一个servletContext对象,代表当前web应用。

1. 说到servletContext有必要了解下servlet,百度的标准解释:

Servlet是一种服务器端的Java应用程序,具有独立于平台和协议的特性,可以生成动态的Web页面。 它担当客户请求(Web浏览器或其他HTTP客户程序)与服务器响应(HTTP服务器上的数据库或应用程序)的中间层。 Servlet是位于Web 服务器内部的服务器端的Java应用程序,与传统的从命令行启动的Java应用程序不同,Servlet由Web服务器进行加载,该Web服务器必须包含支持Servlet的Java虚拟机

2.  WEB项目的启动,一个java web项目通常都是从web.xml入手。

2.1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。 

2.2、紧急着,容创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。 

2.3、容器将<context-param>转换为键值对,并交给servletContext。

  2.3.1: 通过<context-param>初始化参数: 

     <context-param>   

      <param-name>url</param-name>   

      <param-value>jdbc:mysql://localhost:3306/4g</param-value>   

     </context-param>  

  2.3.2: 多个Servlet通过ServletContext对象实现数据共享:

    InitServletService方法中利用ServletContext对象存入需要共享的数据

    /*获取ServletContext对象*/  

    ServletContext context = this.getServletContext();   

    //存入共享的数据    

    context.setAttribute("url", "jdbc:mysql://localhost:3306/4g"); 

    在其它的Servlet中利用ServletContext对象获取共享的数据 

    /*获取ServletContext对象*/  

    ServletContext context = this.getServletContext();   

    //获取共享的数据   

    String name = context.getAttribute("url");   

    System.out.println("共享的内容url值是:"+url); 

  2.3.3 web项目中经常在<context-param>中通过 contextConfigLocation 来配置 sping

    注:contextConfigLocation 参数定义了要装入的 Spring 配置文件。

    <context-param>
         <param-name>contextConfigLocation</param-name>

      <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  <!-- applicationContext.xml为spring配置文件的位置 -->

      <param-value>classpath*:conf/spring/applicationContext_core*.xml,
                     classpath*:conf/spring/applicationContext_dict*.xml

      </param-value>

    </context-param>

    注: spring的 配置文件在启动时,加载的是web-info目录下的applicationContext.xml,运行时使用的是web-info/classes目录下的applicationContext.xml。

2.4、容器创建<listener>中的类实例,创建监听器。

3. web.xml 其它配置:

  3.1 超时时间配置:

    <session-config> 用来定义web站台中的session参数

      <session-timeout></session-timeout> 用来定义这个web站台所有session的有效期限,单位为分钟

    </session-config>

  3.2 异常处理配置

    <error-page> </error-page>用来处理错误代码或异常的页面,有三个子元素:
        <error-code></error-code> 指定错误代码
        <exception-type></exception-type> 指定一个JAVA异常类型
        <location></location> 指定在web站台内的相关资源路径

    例如:

      <error-page>
          <error-code>404</error-code>
          <location>/error404.jsp</location>
      </error-page>
      <error-page>
          <exception-type>java.lang.Exception</exception-type>
          <location>/exception.jsp</location>
      </error-page>

  3.3 JSP页面标签配置

    <taglib></taglib> 用来设定JSP网页所用到的Tag Library路径,有两个子元素:
       <taglib-uri></taglib-uri> 定义TLD文件的URI,在JSP网页中用taglib指令便可取得该URI 的TLD文件
       <taglib-location></taglib-location> 指定TLD文件相对于web站台的存放位置

  比如:
    <taglib>
          <taglib-uri>myTaglib</taglib-uri>
          <taglib-location>/WEB-INF/tlds/MyTaglib.tld</taglib-location>
    </taglib>

原文地址:https://www.cnblogs.com/sin7/p/2874409.html