JSP page Instruction usage detailed _jsp programming

Source: Internet
Author: User
Tags sessions java web server memory

This example describes the use of page directives in JSP. Share to everyone for your reference. Specifically as follows:

First, JSP directives

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

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

In JSP, there are 3 main types of instructions: Page,include and taglib. The page directive allows you to control the structure of the servlet through the import of classes, the customization of servlet superclass, the setting of content types, and the like. Page directives can be placed anywhere in the document. The second instruction, include allowing a file to be inserted into the JSP page when the JSP file is converted to a servlet. The include directive should be placed in the document where you want to insert the file. The third instruction, taglib, defines a custom tag label.

Second, JSP page instruction

The page directive can define the following case sensitive attributes (listed roughly according to 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 Property

Use the Import property of the page directive to specify the package that the servlet that the JSP page converts into should be entered. In the JSP, the package is absolutely required. The reason is that if a package is not used, the system considers the referenced class to be in the same package as the current class. For example, suppose a JSP page contains the following scriptlet:

Copy Code code 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 if the page does not explicitly import the package to which test belongs, the system will assume that test is in the package where the auto-generated servlet resides. But the problem is that the automatically generated servlet's package is unknown! When a server creates a servlet, it often determines its package based on the directory in which the JSP page resides. Other servers may use a different approach. Therefore, you cannot expect classes that do not use packages to work correctly. The same is true for beans, because the bean is simply a class that follows some simple naming conventions and struct conventions.

By default, the servlet imports java.lang.*, javax.servlet.*, javax.servlet.jsp.*, javax.servlet.http.*, and perhaps some server-specific packages. When writing JSP code, never rely on any server-specific classes that are automatically imported. Doing so will make the code not portable.

When using the Import property, you can use the following two forms:

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

For example, the following instruction indicates that all classes in the Java.util and Cn.foololdfat packages do not need to give explicit package identifiers when they are used:

Copy Code code as follows:
<%@ page import= "java.uti1.*, cn.foololdfat.*"%>

Import is the only property in the page that allows multiple occurrences in the same document. Although page directives can appear anywhere in the document, you typically do not place the import statement near the top of the document, or before the corresponding package is first used.

2.2 ContentType and Pageencoding properties

The ContentType property sets the Content-type response header, indicating the MIME type of the document that is about to be sent to the client program. For more information about MIME types, see the Java WEB Development (5) HTTP response header.

When using the ContentType property, you can use the following two forms:

<%@ page contenttype= "Mime-type"%>
<%@ page contenttype= "Mime-type; Charset=character-set"%>

For example, the directive

Copy Code code as follows:
<%@ page contenttype= "application/vnd.ms-excel"%>

The same as the following scriptlet.

Copy Code code as follows:
<% Responce.setcontenttype ("application/vnd.ms-excel"); %>

The 1th difference between the two forms is that Response.setcontenttype uses explicit Java code (a way some developers are trying to avoid), and the page instruction uses only JSP syntax. The 2nd difference is that the instructions are handled specifically, and they are not directly _jspservice code where they appear. This means that the Response.setcontenttype can be invoked conditionally, and the page instruction cannot. The types of content that are conditionally set are primarily used in situations where the same content can be displayed in many different forms.

Unlike a regular servlet (the default MIME type is Text/plain), the default MIME type of the JSP page is text/html (the default character set is Iso-8859-1). Therefore, if the JSP page is exporting HTML in the Latin character set, you do not need to use contentType at all, and if you want to change both the type and character set of the content, you can use the following statement:

Copy Code code as follows:
<%@ page contenttype= "Somemimetype; Charset=somecharacterset "%>

However, if you only want to change the character set, it is simpler to use the Pageencoding property. For example, the Chinese JSP page can use the following statement:

Copy Code code as follows:
<%@ page pageencoding= "GBK"%>

Consider the example of generating Excel tables in the Java WEB Development (5) HTTP response header using JSP to realize the contentType effect.
2.3 Session Property
the Session property controls whether the page participates in HTTP sessions. When using this property, you can use the following two forms:

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

A value of True (default) indicates that the predefined variable session (type HttpSession) should be bound to an existing session if there are existing sessions, otherwise, create a new session and bind it to the sessions. A value of FALSE indicates that the session is not created automatically, and access to the variable sessions can result in an error when the JSP page is converted to a servlet.
For high-traffic Web sites, using session= "false" can save a lot of server memory. Note, however, that session= "false" does not disable session tracing, it simply blocks JSP pages from creating new sessions for those users who do not already have a session. Because sessions are for users and not for Jeong-faceted, session tracking that closes a page has no benefit unless session tracking is turned off by related pages that are likely to be accessed in the same customer session.
2.4 iselignored Properties
the Iselignored property controls whether to ignore (true) the JSP 2.0 expression language (EL) or to perform a normal evaluation (false). This is the newly introduced attribute of JSP 2.0, and it is not legal to use this property in a server that supports only JSP 1.2 and earlier versions. The default value for this property depends on the version of the Web.xml used by the WEB application. If web.xml specifies servlet 2.3 (corresponding to JSP 1.2) or earlier, the default value is True (but the change defaults are still legal, and this property is allowed on the JSP 2.0 compliant server, regardless of the web.xml version). If web.xml specifies the servlet 2.4 (corresponding to JSP 2.0) or later, the default value is False. When using this property, you can use the following two forms:

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

2.5 Buffer and AutoFlush properties
The Buffer property specifies the size of the buffer used by the out variable (type JspWriter). When using this property, you can use the following two forms:

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

The server actually uses a buffer that may be larger than specified, but will not be less than the specified size. For example, the <%@ page buffer= "32KB"%> indicates that the contents of the document should be cached unless the accumulation is at least 32KB, the page completes, or the output is explicitly emptied (for example, using response. Flushbuffer), otherwise the document will not be sent to the customer.

The default buffer size is associated with the server, but at least 8KB. If you want to turn the buffering off, be careful: This requires that the JSP element that sets the header or status code appear at the top of the file, before any HTML content. On the other hand, sometimes a long build time is required for each row of the output, so it is more efficient to disable buffering or use small buffers, so that users can see them immediately after each row is built, rather than wait longer to see the group rows.

The AutoFlush property controls whether the output buffer (the default) should be emptied automatically after the buffer is filled, or an exception (autoflush= "false") is thrown after the buffer overflows. When using this property, you can use the following two forms:

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

The value of false is not valid when buffer= "none". If the client program is a regular Web browser, the use of autoflush= "false" is extremely rare. However, if the client is a custom application, you may want to ensure that the application either receives the full message or does not have a message at all. The value of false can also be used to capture database queries that generate too much data, but generally it is better to put these logic in the data access code rather than the representation code.

2.6 Info Property

The info attribute defines a string that can be obtained by the Getservletinfo method in the servlet, using the Info property in the following form:

Copy Code code as follows:
<%@ page info= "Some message"%>

2.7 ErrorPage and Iserrorpage properties

The ErrorPage property is used to specify a JSP page that handles any exceptions that are thrown but not captured in the current page (that is, objects of type Throwable). It is applied in the following way:

Copy Code code as follows:
<%@ page errorpaqe= "Relative URL"%>

The specified error page can access the thrown exception through the exception variable.
The Iserrorpage property indicates whether the current page can be used as an error page for other JSP pages. When using the Iserrorpage property, you can use the following two forms:

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

2.8 IsThreadSafe Properties

The IsThreadSafe property controls whether the servlet generated by the JSP page allows parallel access (the default), or that multiple requests are not allowed to access a single servlet instance (isthreadsafe= "false") at the same time. When using the IsThreadSafe property, you can use 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 the use of Singlethreadmodel and isthreadsafe= "false" was recommended in the early days, recent experience has shown that Singlethreadmodel's design is poor, making its basic soil useless. Therefore, the use of isthreadsafe should be avoided and the explicit synchronization measures should be used instead.

2.9 Extends properties

The extends property specifies the superclass (superclass) of the servlet generated by the JSP page. It takes the following form:

Copy Code code as follows:
<%@ page extends= "Package.class"%>

This property is typically reserved for developers or providers who make fundamental changes to how the page works (such as adding personalization features). You should avoid using this property unless you refer to a class that is specifically provided by the server provider for that purpose.

2.10 Language Properties

In some ways, the role of the Language property is to specify the scripting language used by the page, as follows:

Copy Code code as follows:
<%@ page language= "java"%>

For now, because Java is both the default and the only legitimate choice, there is no need to care about this attribute.

I hope this article will help you with the 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.