JSP Series 2: JSP instruction elements: Page instruction, include instruction, and taglib instruction

Source: Internet
Author: User

JSP instruction elements: Page instruction, include instruction, and taglib instruction

1. Directive elements: controls the generated servlet structure.

JSP commands are used in the "conversion phase" to provide information about the entire JSP page, affecting the overall structure of the servlet generated by the JSP page.
The command does not generate any output to the current output stream.
 
1. command element Syntax:
<% @ Directive {ATTR = "value"} * %>
Note:
There cannot be any space between <and % and @, % and>.
Double quotation marks on both sides of the attribute value can be replaced with single quotation marks. The quotation mark cannot be completely omitted.
Use quotation marks in the attribute to use/for escape.
2. XML syntax of commands:
<JSP: Directive. directivetype ATTR = "value1" ATTR = "value2".../>
 
When compiling a JSP page compatible with XML, you can use the XML compatibility mode of commands as long as the XML syntax and standard syntax are not mixed in the same page.
 
3. Three types of commands:
Page, include, taglib.

2. Page command: Used to set attributes of JSP pages. These attributes are used to communicate with JSP containers and control the generated servlet structure.

The page command acts on the entire JSP page. You can store all commands in the document.
 
A, page command syntax:
<% @ Page ATTR = "value1" ATTR = "value2"... %>

Page instruction in XML syntax format:
<JSP: Directive. Page ATTR = "value1" ATTR = "value2".../>
 
B. 13 attributes of the page command
 
1. Import attribute: the unique attribute allowed to appear multiple times in the same document in the page command. Attribute values can be separated by commas.
Convert the JSP page to the package that the servlet should enter. For classes that do not explicitly specify a package, the location of the class package is determined based on the package where the JSP page is located (the directory of the generated servlet.
This means that the class will be searched for in the JSP page package.

Make sure to put the Utility Class and Bean package in the specified package. Do not rely on automatic import.

2. Language attributes:
Specifies the script language used in the script element. The default value is Java. In jsp2.0 specifications, only Java is supported.
 
3. Attributes of contenttype and pageencodeing:
The contenttype attribute sets the MIME type and character encoding of the response header sent to the client document. Separate multiple use; numbers.
The pageencodeing attribute is only used to change the character encoding.

The default MIME type of servlet is text/plain, and the default MIME type of JSP is text/html.

3. Session attribute: controls whether a page participates in a session.

The default value is true. If an existing session exists, the predefined session variable is bound to the existing session. Otherwise, a new session is created and bound to the session.
For high-traffic websites, setting false can save a lot of server memory.
Setting false indicates that a new session is not automatically created. When the JSP page is converted to servlet, access to the variable session leads to an error.
Setting it to false does not disable session tracing. It only prevents users who do not have a session from creating a new session on the JSP page.

* For pages that do not require session tracking, set it to false. When set to false, the session object is inaccessible.

4. Attributes of isellgnored:
Defines whether to execute or ignore El expressions on the JSP page. True indicates ignore, and false indicates execution.
The default value depends on the Web. XML version. The default value is true before servlet2.3 and false by servlet2.4.
This error occurs when El expressions are used due to inconsistent JSP versions. Use: isellgnored = "true ";

5. Buffer and autoflush attributes:
The buffer attribute specifies the buffer size used by the out object (jspwriter), in KB. The default value is 8 KB.
None indicates that no buffer is used. In this way, the JSP element that sets the header or status code must appear at the top of the file, before any HTML content.

The autoflush attribute controls whether to automatically clear the output buffer when the buffer is full (true by default) or throw an exception (false) after the buffer overflow ).
When buffer = none, autoflush = false is incorrect.

6. info attributes:
Defines a string that can be obtained through the getservletinfo method in servlet.

The JSP Container generates the getservletinfo method in the servlet and returns the string specified by the info attribute.

7. errorpage and iserrorpage attributes: Specify error pages for specific pages.
The errorpage attribute is used to specify a JSP page to handle any exceptions thrown but not captured on the current page. The specified page can access the exception information through the exception variable.
The iserrorpage attribute indicates whether the current page can be used as an error page of other JSP pages. True or false.
The error page should be placed under the WEB-INF directory, only for server access, does not generate a forwarding call, the client can only see the original request page url, do not see the error page url.

If you specify an error page for the entire web application or for different types of errors in the application, use the error-page element in Web. xml.

If a page defines a specific error page through this attribute, any error pages defined in the web. xml file will not be used.

The error object exception can only be used on the error handling page.

8. isthreadsafe attribute: This method is replaced by explicit synchronization.
Controls whether the servlet generated by the JSP page allows concurrent access (true by default ).
The implementation of blocking concurrent access is based on the singlethreadmodel interface. Therefore, avoid using the isthreadsafe attribute.

9. extends attributes: Do not use them. This is what server providers should do. The parent class must be an implementation class for the httjsppage interface. Httjsppage is a jsppage extension interface applied to HTTP requests and responses.
Specify the servlet superclass generated on the JSP page.

3. include command: Include the file to the JSP page during page conversion.

The include command is used to statically include a file on a JSP page. The file can be a JSP page, HTML page, text file, or Java code.
Insert the JSP code on the homepage "Before converting to servlet. When the contained page is changed, the home page must also be updated (translated ).
For file inclusion, use JSP: include. The include command is used only when the fields and methods required on the home page are defined in the contained file or when the response header on the home page is set in the contained file.
 
1. Include Syntax:
<% @ Include file = "url" %>
The file property value is interpreted as a URL relative to the current JSP file.

2. Do not use HTML tags that are repeated with the home page in the contained files. This will affect the same tags in the original JSP file and cause errors.
Because the original file and the contained file can access the variables and methods defined by each other, the naming of variables and methods should be avoided.

3. In JSP, as long as there are three functions, external content can be included in JSP documents.
A, JSP: include action

B, include command

C, JSP: plugin action

4. include command:
The server inserts each byte of the contained file into the home page and processes the generated page as a single JSP page.

5. jsp: include action: Include the "output" of other pages during the "request. This behavior is identical to the include method of the requestdispatcher class.

Advantage: the page is changed, so you do not need to modify the page.
Disadvantage: The contained page cannot use any JSP construction that may affect the homepage.
That is to say, you cannot use the complete HTML document as an contained page, but you can only contain HTML tags suitable for file insertion points.

Parameters (add Request Parameters Using JSP: Param action elements ):
Use the same request object on the contained page and the home page. You can view the parameters on the included page, but not on the included page.

A. Page attribute: Specify the contained page, which should point to the relative URL of a resource.
If the relative URL does not start with a slash (/), it indicates the position relative to the home page. Start with a slash (/) to indicate the root directory relative to the Web application.
If the server processes the resource, it will be interpreted as relative to the current web application; if only the browser processes the resource, it will be interpreted as relative to the root directory of the server.

For a URL starting with/, the Server Always interprets it relative to the current web application, and the browser always interprets it relative to the root directory of the server.

The pages that you can include can be hidden from the client under the WEB-INF directory.

B. Flush attribute: Specifies whether to clear the input stream of the home page before the page is included (false by default ).

6. Differences between the include command and JSP: include action:
Call time:
JSP: include action is included during the request.
The include command is included during the "Conversion" period.
Content:
JSP: include actions include "output" and HTML text of JSP pages or Servlets.
The include command is the actual content of the file.

Number of servle to be converted:
JSP: include action during the request, the requested page is translated into an independent servlet. Corresponds to the servlet on the home page.
During the conversion, the include command inserts the content on the included page into the home page to obtain the servlet translated from the home page.

Can I set response headers and parameters that affect the home page:
JSP: The incurred action is not acceptable. The include command is acceptable.
Update:
The JSP: include action is inserted during the request, so its changes do not need to be updated on the home page.
The include command update means the servlet update on the home page.
 

4. taglib command: declare the custom tag used by the user.

Declare that you use a custom tag to import the tag library descriptor file to the JSP page.

Taglib command syntax:
<% @ Taglib (uri = "tigLibURL" or tagDir = "tagDir") prefix = "tagPrefix" %>

Uri attribute: Location of the tag library descriptor. Unique tag library descriptor associated with the prefix. You can use absolute or relative URLs.
TagDir property: indicates that the prefix will be used to identify the Tag file under the WEV-INF/tags directory.

Prefix attribute: the prefix of a tag to distinguish multiple custom tags. You cannot use a reserved prefix or an empty prefix, which complies with the naming conventions of the XML namespace.
 

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.