Obtain the values defined by context-Param and init-Param in Web. xml.

Source: Internet
Author: User

Web. two parameters can be defined in XML: <servlet> <servlet-Name> mainservlet </servlet-Name> <servlet-class> COM. wes. controller. mainservlet </servlet-class> <init-param> <param-Name> param1 </param-Name> <param-value> avalible in servlet Init () </param-value> </init-param> <param-Name> URL </param-Name> <param-value> JDBC: mysql: // localhost/wjlmgqs </param-value> </init-param> <load-on-startup> 0 </load-on-startup> </servlet>

(1) parameters within the application range are stored in servletcontext and configured in Web. XML as follows:

XML Code
<Context-param>
<Param-Name> Context/Param </param-Name>
<Param-value> avalible during application </param-value>
</Context-param>

(2) servlet-specific parameters can only be obtained in the servlet Init () method. The configuration in Web. XML is as follows:

XML Code

In servlet, you can use the following code:

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 servlet"); system. out. println (this. getinitparameter ("param1"); system. out. println ("the following parameters are stored in servletcontext"); system. out. println (getservletcontext (). getinitparameter ("context/Param "));}}

The first parameter can be obtained through getservletcontext (). getinitparameter ("context/Param") in servlet.
The second parameter can only be obtained through this. getinitparameter ("param1") in the servlet Init () method.

Init-Param belongs to a servlet, and context-Param belongs to the whole application, which can be obtained not only in the servlet, but also in the JSP file.

In JSP, config is equivalent to servletcontext, <% = config. getservletcontext (). getinitparameter ("...") %>.
Servletactioncontext. getservletcontext (). getinitparameter ("...").

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.