How to Set and obtain initialization parameters in JSP

Source: Internet
Author: User

How to Set and obtain initialization parameters in JSP

Get init parameter in JSP page

By reawer 2008-8-13
Initparamter access method 1 (built-in object ):

I. By servletconfig:
Parameter1: {config. getinitparameter ("p1 ")}
Parameter2: {config. getinitparameter ("p2 ")}
Parameter3: {config. getinitparameter ("P3 ")}

Config traverses all parameter:
Ii. By servletcontext:
Parameter1: {application. getinitparameter ("p1 ")}
Parameter2: {application. getinitparameter ("p2 ")}
Parameter3: {application. getinitparameter ("P3 ")}

Application traverses all parameter:

Method 2 (JSF) for initparamter access ):

Initparamter access method 3 (EL) -- equivalent to Application Object Access:

=$ {Initparam. P1}
=$ {Initparam. P2}
=$ {Initparam. P3}

How to Use initparameter in JSP
1. initialize parameters using the servlet corresponding to JSP
Providing initialization parameters for the JSP page is different from providing initialization parameters for the servlet in three aspects.
1) Use the JSP-file element to replace servlet-class. Therefore, the servlet elements in the WEB-INF/Web. xml file are as follows:

<Servlet>
<Description> initparameter </description>
<Display-Name> initparameterjsp </display-Name>
<Servlet-Name> initparameterjsp </servlet-Name>
<JSP-File>/JSP/initparameter. jsp </JSP-File>
<Init-param>
<Description> parameter test </description>
<Param-Name> paraname </param-Name>
<Param-value> paravalue </param-value>
</Init-param>
</Servlet>

2) almost always assign a clear URL pattern. For Servlets, the default URL starting with http: // localhost: 8080/webapp/servlet/is generally used.
Remember to use the registration name instead of the original name. This is also technically legal for JSP pages. For example, the available URL http: // localhost: 8080/webapp/servlet/pagename
The version that accesses *. jsp that has access to the initialization parameters. However, when using JSP pages, we often do not seem to like the general servlet URL.
Therefore, the best way is to use URL-pattern to associate the original URL of the JSP page with the registered servlet name.
In this way, the client can use the specified name of the JSP page for access. For example, for the servlet definition of this JSP, you can use the following servlet-mapping definition:

<Servlet-mapping>
<Servlet-Name> initparameterjsp </servlet-Name>
<! -- Associated URL mode. If it is not written here:/JSP/initparameter. jsp, The initparamter value in servlet cannot be obtained when the URL (/JSP/initparameter. jsp) is used for access -->
<URL-pattern>/JSP/initparameter </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-Name> initparameterjsp </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>

3) JSP pages use jspinit instead of init. The servlet automatically created from the JSP page may already use the Inti method.
Generally, http: // localhost: 8080/webapp/*. jsp is not written as/JSP/initparameter. jsp in URL-pattern.
A URL in the form will activate a version on this page that does not have access to initialization parameters, and thus will lose access to the paraname parameter. For example, access using the config built-in object on this page:
Config. getinitparameter ("paraname") ---- the result is null.
However, the Web. xml file has assigned a registration name for this JSP page, which has been associated with URL mode/JSP/initparameter. Access the service in the following format:
Http: // localhost: 8080/webapp/JSP/initparameter
You can obtain the following parameters:
Config. getinitparameter ("paraname"); // ---- the result is paravalue.

2. Use the initialization parameters within the scope of the application (Project)
Generally, an initialization parameter is assigned to a single servlet or JSP page. The specified servlet or JSP page uses the getinitparameter method of servletconfig to read these parameters.
However, in some cases, you want to set the system-wide initialization parameters that can be read by any servlet or JSP page using the getinitparameter method of servletcontext.
You can use the context-Param element to declare the initialization values within these system ranges.
1) the context-Param element should contain param-name, param-value, and optional description sub-elements, as shown below:

<Context-param>
<Description> This is the description of an example parameter. </description>
<! -- Parameter name -->
<Param-Name> appparaname </param-Name>
<! -- Parameter value -->
<Param-value> appparavalue </param-value>
</Context-param>

2) access method, using the built-in application object:
String value = application. getinitparameter ("parameter name"); // ---- get the result: appparavalue.

Related Article

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.