Differences between context-Param and init-Param in Web. xml

Source: Internet
Author: User

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


Context/Param Avalible during application

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


Mainservlet
Com. Wes. Controller. mainservlet

Param1 Avalible in servlet Init ()
0

In servlet, you can use the following code:

Package com. Wes. Controller;

Import javax. servlet. servletexception;
Import javax. servlet. http. httpservlet;

Public class mainservlet extends httpservlet {

Public mainservlet (){
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 "));
}
}

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.