Introduction to the difference between Context-param and Init-param in Java web.xml

Source: Internet
Author: User
Tags xmlns java web

The difference between <context-param> and <init-param> in Web.xml is that <context-param> sets a parameter that is visible globally (ServletContext) in the application , <init-param> sets a parameter that is visible in the local (ServletRequest range) of the application.

<?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 >
<display-name>testdemo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

<context-param>
<param-name>appversion</param-name>
<param-value>1.0</param-value>
</context-param>

<servlet>
<servlet-name>test</servlet-name>
<servlet-class>test. Testservlet</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>yedward</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
Package test;

Import java.io.IOException;

Import javax.servlet.ServletException;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;

public class Testservlet extends HttpServlet {
Private static final long serialversionuid = 5222793251610509039L;

@Override
public void Init () throws Servletexception {
System.out.println (This.getinitparameter ("name"));
}

@Override
protected void Service (HttpServletRequest request, HttpServletResponse Resposne)
Throws Servletexception, IOException {
System.out.println (This.getinitparameter ("name"));
System.out.println (This.getservletcontext (). Getinitparameter ("appversion"));
System.out.println (Request.getservletcontext (). Getinitparameter ("appversion"));
}
}

The output from the above code is:

Yedward
Yedward
1.0
1.0
When you start a Web project, the container (such as Tomcat) goes to the configuration file Web.xml read two nodes:<listener></listener> and <context-param></ Context-param>, the container creates a servletcontext, all parts of the Web project will share this context, the container will <context-param></context-param> Convert to a key-value pair and give it to the ServletContext container to create a class instance in <listener></listener>, that is, to create a listener that will have contextinitialized in the monitor ( Servletcontextevent args) initialization method, obtained in this method:

ServletContext = Servletcontextevent.getservletcontext (); The value of the
Context-param = Servletcontext.getinitparameter (the "Context-param Key");
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, this action will be earlier than all the servlet. In other words, this time, the operation of the key value in <context-param> will be executed before the Web project is fully launched.

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.