Get the values defined by Context-param and Init-param in Web. xml

Source: Internet
Author: User

Web. Xml can define two parameters:<context-param> and </init-param>
(1) Parameters within the application range, stored in the ServletContext, are configured in Web. Xml as follows:

 1  <  context-param  >  2   >  context/param</  param-name  >  3  <  param-value  >  avalible during Application</  Param-value  >  4  </ context-param  >  

(2) The parameters within the Servlet range can only be obtained in the servlet's init () method, which is configured in Web. Xml as follows:

1 <servlet> 2 <Servlet-name>Mainservlet</Servlet-name> 3 <Servlet-class>Com.wes.controller.MainServlet</Servlet-class> 4 <Init-param> 5 <Param-name>param1</Param-name> 6 <Param-value>Avalible in servlet init ()</Param-value> 7 </Init-param> 8 <Load-on-startup>0</Load-on-startup> 9 </servlet>

In a servlet, you can use the code separately:

Java code
Package com.test;

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

public class Testservlet extends HttpServlet ... {

Public Testservlet () ... {
Super ();
}
public void Init () throws servletexception ... {
System.out.println ("The following two parameters param1 are stored in the servlet");
System.out.println (This.getinitparameter ("param1"));
System.out.println ("The following parameters are stored in the ServletContext");
System.out.println (Getservletcontext (). Getinitparameter ("Context/param"));
}
}

The first parameter in the servlet can be obtained by Getservletcontext (). Getinitparameter ("Context/param")
The second parameter can only be obtained by This.getinitparameter ("param1") in the servlet's init () method

Init-param belongs to a servlet, and Context-param belongs to the entire application, not only in Servlets, but also in JSP files.

The config in the JSP is equivalent to the Servletcontext,<%=config.getservletcontext () here. Getinitparameter ("...")%>.
Action in Servletactioncontext.getservletcontext (). Getinitparameter ("...").

Get the values defined by Context-param and Init-param in Web. xml

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.