Detailed description of the page instruction usage in jsp and the jsppage instruction

Source: Internet
Author: User

Detailed description of the page instruction usage in jsp and the jsppage instruction

This example describes the usage of the page command in jsp. Share it with you for your reference. The details are as follows:

I. JSP commands

The JSP directive affects the overall structure of the servlet generated by the JSP page. The following template provides two possible forms of commands. Double quotation marks on both sides of the attribute value can be replaced with single quotation marks, but the quotation marks cannot be completely omitted. If you want to use quotation marks in the property values, add a backslash before them, 'Use \', "Use \"

<% directive attribute="value" %><% directive attribute1="value1"attribute2="value2"......attribute3="value3" %>

In JSP, there are three main types of commands: page, include and taglib. The page command allows you to control the servlet structure through class import, servlet superclass customization, content type settings, and other such things. The page command can be stored anywhere in the document. The second instruction, include, allows you to insert a file into the JSP page when the JSP file is converted to the servlet. The include command should be placed in the place where the file is to be inserted in the document. The third instruction, taglib, defines the custom tag.

Ii. JSP page commands

The page command can define the following case-sensitive attributes (roughly listed based on the frequency of use): import, contentType, pageEncoding, session, isELIgnored (JSP 2.0 only), buffer, autoFlush, info, errorPage, isErrorPage, isThreadSafe, language, and extends.

2.1 import attributes

Use the import attribute of the page command to specify the package that the servlet converts the JSP page. In JSP, the package is absolutely required. If no package is used, the system considers that the referenced class is in the same package as the current class. For example, assume that a JSP page contains the following scriptlet:
Copy codeThe Code is as follows: <% Test t = new Test (); %>

Here, if Test is in an input package, there is no ambiguity. However, if Test is not in the package, or the page does not explicitly import the package to which Test belongs, the system will think that Test is in the package of the automatically generated servlet. But the problem is that the package of the automatically generated servlet is unknown! When a server creates a servlet, its package is often determined based on the directory of the JSP page. Other servers may use different methods. Therefore, you cannot expect classes that do not use packages to work normally. The same applies to beans, because beans are just classes that follow some simple naming conventions and structure conventions.

By default, servlet imports java. lang. *. javax. servlet. *. javax. servlet. jsp. *. javax. servlet. http. *. Some server-specific packages may also be included. When writing JSP code, do not rely on any server-specific classes that are automatically imported. This will make the code unportable.

When using the import attribute, you can use the following two forms:

<%@ page import="package.class" %><%@ page import="package.class1, ..., package.classN" %>

For example, the following command indicates that no explicit package identifier is required for all classes in the java. util package and cn. foololdfat package during use:
Copy codeThe Code is as follows: <% @ page import = "java. uti1. *, cn. foololdfat. *" %>

Import is the only property in the page that can appear multiple times in the same document. Although the page command can appear anywhere in the document, it generally does not place the import Statement near the top of the document, or before the corresponding package is used for the first time.

2.2 contentType and pageEncoding attributes

The contentType attribute sets the Content-Type Response Header, indicating the MIME Type of the document to be sent to the client program. For more information about MIME types, see Java Web development (5) HTTP response header.

The contentType attribute can be used in the following two forms:

<%@ page contentType="MIME-TYPE" %><%@ page contentType="MIME-Type; charset=Character-Set" %>

For example, commands
Copy codeThe Code is as follows: <% @ page contentType = "application/vnd. ms-excel" %>

Same as the role of the following scriptlet.
Copy codeThe Code is as follows: <% responce. setContentType ("application/vnd. ms-excel"); %>

The first difference between the two forms is that response. setContentType uses clear Java code (this is the way some developers try to avoid using it), while page commands only use JSP syntax. The second difference is that commands are specially processed, and they do not directly become the _ jspService code at the location where they appear. This means that response. setContentType can be called conditionally, but the page command cannot. The type of content set in conditional mode is mainly used when the same content can be displayed in a variety of different forms.

Unlike conventional servlets (the default MIME type is text/plain), the default MIME type for JSP pages is text/html (the default character set is ISO-8859-1 ). Therefore, if the JSP page outputs HTML in Latin character set, you do not need to use contentType. If you want to change the content type and character set at the same time, you can use the following statement:
Copy codeThe Code is as follows: <% @ page contentType = "someMimeType; charset = someCharacterSet" %>

However, if you only want to change the character set, it is easier to use the pageEncoding attribute. For example, the Chinese JSP page can use the following statement:
Copy codeThe Code is as follows: <% @ page pageEncoding = "GBK" %>

You can use JSP to generate an Excel table in the HTTP Response Header of Java Web development (5) to understand the role of contentType.
2.3 session attributes
The session attribute controls whether the page participates in an HTTP session. When using this attribute, you can use the following two forms:

<%@ page session="true" %> <%--Default--%><%@ page session="false" %>

True (default) indicates that if an existing session exists, the session of the predefined variable (HttpSession type) should be bound to the existing session; otherwise, create a new session and bind it to the session. False indicates that the session is not automatically created. When the JSP page is converted to a servlet, access to the variable session will cause an error.
For high-traffic websites, using session = "false" can save a lot of server memory. However, note that session = "false" does not disable session tracing. It only blocks JSP pages from creating new sessions for users who do not have a session. Because the session is for the user rather than for the surface, closing session tracking on a page is of no benefit, unless session tracing is disabled for all pages that may be accessed in the same client session.
2.4 isELIgnored attributes
The isELIgnored attribute controls whether to ignore (true) JSP 2.0 Expression Language (EL) or evaluate (false) normally ). This is a newly introduced attribute of JSP 2.0. It is invalid to use this attribute on servers that only support JSP 1.2 and earlier versions. The default value of this attribute depends on the Web. xml version used by the web application. If the web. xml specifies servlet 2.3 (corresponding to JSP 1.2) or earlier versions. The default value is true (but the default value is still valid. This attribute is allowed in JSP 2.0 compatible servers, regardless of web. xml version ). If web. xml specifies servlet 2.4 (corresponding to JSP 2.0) or a later version, the default value is false. When using this attribute, you can use the following two forms:

<%@ page isELIgnored="false" %><%@ page isELIgnored="true" %>

2.5 buffer and autoFlush attributes
The buffer attribute specifies the size of the buffer used by the out variable (type: JspWriter. When using this attribute, you can use the following two forms:

<%@ page buffer="sizekb" %><%@ page buffer="none" %>

The actual buffer used by the server may be larger than the specified one, but it will not be smaller than the specified size. For example, <% @ page buffer = "32kb" %> indicates that the document content should be cached, unless at least 32 KB is accumulated, the page is completed, or the output is cleared explicitly (for example, response. flushBuffer), otherwise the document is not sent to the customer.

The default buffer size is related to the server, but at least 8 KB. If you want to disable the buffer function, you should be very careful: This requires that JSP elements that set the header or status code appear at the top of the file, before any HTML content. On the other hand, sometimes every row of the output content takes a long time to generate, so Disabling buffering or using a small buffer will be more efficient. In this way, you can immediately see each row after it is generated, instead of waiting for a longer period of time to see rows in groups.

The autoFlush attribute controls whether to automatically clear the output buffer when the buffer is full (default) or throw an exception after the Buffer Overflow (autoFlush = "false "). When using this attribute, you can use the following two forms:

<%@ page autoFlush="true" %> <%--Default--%><%@ page autoFlush="false" %>

When buffer = "none", the value of false is invalid. If the client program is a conventional Web browser, autoFlush = "false" is rarely used. However, if your application is a custom application, you may want to ensure that the application either receives the complete message or has no message at all. The false value can also be used to capture database queries that generate too much data. However, in general, it is better to put these logics in data access code rather than code.

2.6 info attributes

The info attribute defines a string that can be obtained through the getServletInfo method in the servlet. the following format is used when the info attribute is used:
Copy codeThe Code is as follows: <% @ page info = "Some Message" %>

2.7 errorPage and isErrorPage attributes

The errorPage attribute is used to specify a JSP page to handle any exceptions (Throwable objects) thrown but not captured on the current page ). Its application is as follows:
Copy codeThe Code is as follows: <% @ page errorPaqe = "Relative URL" %>

The error page can be accessed through the exception variable.
The isErrorPage attribute indicates whether the current page can be used as an error page of other JSP pages. The isErrorPage attribute can be used in the following two forms:

<%@ page isErrorPage="true" %><%@ page isErrorPage="false" %> <%--Default--%>

2.8 isThreadSafe attributes

The isThreadSafe attribute controls whether the servlet generated by the JSP page allows parallel access (default) or whether multiple requests are not allowed to access a single servlet instance at the same time (isThreadSafe = "false "). The isThreadSafe attribute can be used in the following two forms:

<%@ page isThreadSafe="true" %> <%--Default--%><%@page isThreadSafe="false" %>

Unfortunately, the standard mechanism for blocking concurrent access is to implement the SingleThreadModel interface. Although SingleThreadModel and isThreadSafe = "false" were recommended in the early days, recent experience shows that SingleThreadModel is poorly designed, making it useless. Therefore, use isThreadSafe instead of explicit synchronization measures.

2.9 extends attributes

The extends attribute specifies the servlet superclass generated by the JSP page ). It adopts the following form:
Copy codeThe Code is as follows: <% @ page extends = "package. class" %>

This property is generally reserved by developers or providers who make fundamental changes to the page's operation method (such as adding personalized features ). This attribute should be avoided unless you reference the classes provided by the server provider for this purpose.

2.10 language attributes

In a sense, the language attribute specifies the script language used by the page, as shown below:
Copy codeThe Code is as follows: <% @ page language = "java" %>

For now, Java is both a default and only legal choice, so there is no need to care about this attribute.

I hope this article will help you with JSP program design.

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.