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