Web. XML configuration Detailed Context-param

Source: Internet
Author: User
Tags format definition

Web. XML configuration Detailed Context-param

    • Format definition:
[HTML]  view plaincopy  
  1. <context-param>  
  2. <param-name> Contextconfiglocation</param-name>   
  3. < param-value>contextconfiglocationvalue></param-value>  
  4. </context-param >  

Role: This element is used to declare context initialization parameters within the scope of the application (the entire Web project).

Param-name sets the parameter name of the context. Must be a unique name

Param-value the value of the parameter name set

    • Initialization process:
      1. When you start a Web project, the container (such as Tomcat) reads the two nodes <listener> and <contex-param> in the. XML configuration file.
      2. The container then creates a ServletContext (context) that can be used by the entire Web project within the scope of the application.
      3. The container then converts the read to <context-param> into a key-value pair and gives it to ServletContext.
      4. The container creates a class instance in <listener></listener>, that is, the listener is created (note: The class defined by listener can be a custom class but must inherit Servletcontextlistener).
      5. There is a contextinitialized (Servletcontextevent event) initialization method in the listener class, which can be passed Event.getservletcontext () in this method. Getinitparameter ("Contextconfiglocation") to get the value Context-param set. There must also be a contextdestroyed (Servletcontextevent event) Destruction method in this class. To release resources before closing the app, such as closing the database connection.
      6. After you get the value of this context-param, you can do something about it. Note that this time your Web project is not fully started yet. This action will be earlier than all servlets.

By the above initialization process, it is known that the container loading process for Web. XML is context-param >> listener >> fileter >> servlet

    • How to use
      1. On the page

        ${initparam.contextconfiglocation}

      2. In the servlet
        String Paramvalue=getservletcontext (). Getinitparameter ("Contextconfiglocation")

Web. XML configuration Detailed Context-param

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.