Use of servlet getinitparameter

Source: Internet
Author: User

Tag: getinitparameter

The getinitparameter method comes from genericservlet. Generally, when creating a servlet, It inherits from httpservlet, and httpservlet is a subclass of genericservlet. Therefore, our servlet can call this method to obtain the web. configuration information in the XML configuration file:

1. Some web. XML Information:

<! -- Global configuration --> <context-param> <param-Name> global </param-Name> <param-value> okes </param-value> </context-param> <servlet> <servlet-Name> testservlet </servlet-Name> <servlet-class> COM. web. servlets. testservlet </servlet-class> <! -- Local configuration, that is, testservlet configuration --> <init-param> <param-Name> name </param-Name> <param-value> wangzp, tanhq </param-value> </init-param> <param-Name> name1 </param-Name> <param-value> wangzp1, tanhq1 </param-value> </init-param> </servlet> <servlet-mapping> <servlet-Name> testservlet </servlet-Name> <URL-pattern>/ test. DO </url-pattern> </servlet-mapping>

2. If local configuration and global configuration are obtained

Public void doget (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {response. setcontenttype ("text/html; charset = UTF-8"); printwriter out = response. getwriter ();/*** get the attribute * Name: <init-Name> name </init-param> * value in <init-param> </init-Name>: <init-value> value </init-value> * You can use getinitparameter (name) to obtain the value */out in the value. println (getinitparameter ("name");/*** when multiple of the preceding labels exist, you can use getinitparameternames * to obtain all the attribute names, then get the corresponding property value */enumeration enums = getinitparameternames (); While (enums. hasmoreelements () {system. out. println (enums. nextelement ();}/*** get global attribute configuration: * 1. You must use getservletconfig () or getservletcontext () and call the getinitparameter method to obtain the global attribute configuration; * 2. Global tag <context-param> </context-param> */out. println (getservletcontext (). getinitparameter ("Global"); out. flush (); out. close ();}

3. Summary

3.1 get local servlet configuration attributes: Use getinitparameter or getinitparameternames;

3.2 obtain the global servlet configuration attributes: Use the servletconfig or servletcontext object to call getinitparameter to obtain the attributes.


This article from the "Java program" blog, please be sure to keep this source http://793404905.blog.51cto.com/6179428/1540259

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.