Initialization parameters for Web. xml

Source: Internet
Author: User

Initialization parameters for Web. xml

  

--------- first declares that this is the initialization configuration of the Context-param,init-param parameter in the Web ----------

---------------------------------------------------------------------------------------------------

First, the initialization of parameters

  There are two types of parameters that can be defined in Web. xml:

 (1) Parameters within the application range, stored in the ServletContext, are configured in Web. Xml as follows:   

< Context-param >          <param-name>context/param</param-name>           <param-value>avalible during application</ param-value></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:

<servlet>    <Servlet-name>Mainservlet</Servlet-name>    <Servlet-class>Cn.jbit.controller.MainServlet</Servlet-class>   <init-param> <param-name>param1</param-name> <param-value>avalible in Servlets in It () </param-value> </init-param>    <Load-on-startup>0</Load-on-startup></servlet>

In a servlet, you can use the code separately:

 PackageCn.jbit.controller;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet; Public classMainservletextendsHttpServlet { PublicMainservlet () {Super(); }     Public voidInit ()throwsservletexception {System.out.println ("The following parameters are stored in the ServletContext"); System.out.println (getservletcontext (). Getinitparameter("Context/param")); System.out.println ("The following two parameters param1 are stored in the servlet"); System.out.println ( This.Getinitparameter("param1")); }}

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

--------------------------------------------------------------------------------------------------------------- ------------------------

Second, the initialization parameters have been

 <!--application Mode --
<Context-param> <Param-name>Count</Param-name> <Param-value>1200</Param-value></Context-param>
<!--servlet-->
<servlet> <Description>Loginservlet</Description> <Display-name>Loginservlet</Display-name> <Servlet-name>Loginservlet</Servlet-name> <Servlet-class>Loginservlet</Servlet-class>
   <!--servlet init at initialization ()--
<Init-param> <Description>Number of system initializations</Description> <Param-name>Num</Param-name> <Param-value>100</Param-value> </Init-param>
</servlet>

<servlet-mapping> <Servlet-name>Loginservlet</Servlet-name> <Url-pattern>/loginservlet</Url-pattern> </servlet-mapping>

Methods and similarities and differences in obtaining parameters in a servlet:

  
response.setcharacterencoding ("GB2312"); System.out.println ("Getservletcontext ():" +Getservletcontext ()); ServletContext Context=getservletconfig (). Getservletcontext (); String num =context.getinitparameter ("num");//<init-param> Get No value String Count=context.getinitparameter ("Count");//<context-param> get to ValueSYSTEM.OUT.PRINTLN ("num:" +num);//NULLSystem.out.println ("Count:" +count);// -System.out.println ("num:" +getservletconfig (). Getinitparameter ("num"));//100 Gets the value System.out.println ("Count:" +getservletconfig (). Getinitparameter ("Count"));//null Get no value

Initialization parameters for 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.