Among the nine built-in objects of JSP (1). Servlet-related built-in objects: page, config

Source: Internet
Author: User

(1) Servlet-related built-in objects: page and config

 

Page: Object type, which is rarely used.

 

The page object represents the JSP page itself, just like the this keyword in Java. More accurately, it represents the Servlet after JSP is translated. Therefore, it can call the method defined by the Servlet class.

 

Example:

 

<% @ Page contentType = "text/html; charset = UTF-8" %>

 

<% @ Page info = "test !!! "%>

 

<Html>

 

<Body>

 

<% = (HttpJspPage) page). getServletInfo () %>

 

<% -- Force type conversion is required because the page Object is of the Object type -- %>

 

</Body>

 

</Html>

 

Access the JSP page through a browser. The following output is displayed in the browser: test !!!

 

 

 

Config (master): ServletConfig

 

The cogfig object is an object of the ServletConfig interface and stores some Servlet initialization information. It is used to obtain the initialization parameters of JSP configuration and is valid only within the JSP page range. The common method is as follows:

 

Config. getInitParameter ("name"): Get the Servlet initialization parameter value with the specified name.

 

Config. getInitParameterNames (): gets the Servlet initialization parameter list and returns an enumeration instance.

 

Config. getServletContext (): Get the Servlet context (ServletContext ).

 

Config. getServletName (): get the name of the generated Servlet.

 

For example, in JSP, you can directly use the getInitParameter () method of the config object to obtain the local initialization parameters configured in JSP:

 

Configure the following in the jspPro2 web. xml file of the JSP project:

 

<Servlet>

 

<Servlet-name> myIndex </servlet-name>

 

<Jsp-file>/index. jsp </jsp-file>

 

<Init-param>

 

<Param-name> name </param-name>

 

<Param-value> zhangsan </param-value>

 

</Init-param>

 

</Servlet>

 

<Servlet-mapping>

 

<Servlet-name> myIndex </servlet-name>

 

<Url-pattern>/test </url-pattern>

 

</Servlet-mapping>

 

Configure the following in the index. JSP file of the jsp project:

 

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" %>

 

<Html>

 

<Head>

 

<Title> config </title>

 

</Head>

 

<Body>

 

<%

 

String name = config. getInitParameter ("name ");

 

Out. print ("print information through the out object:" + name + "<br> ");

 

%>

 

Obtained initialization parameters: <% = name %>

 

</Body>

 

</Html>

 

Access www.2cto.com: http: // 172.16.0.71: 7070/jspPro2/test through a browser. The output is as follows:

 

Print information through the out object: zhangsan

The obtained initialization parameter is zhangsan.

From the column ynz1219

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.