3 Compiler directives for JSP

Source: Internet
Author: User

The JSP compiler directive is a message informing the JSP engine that it does not generate output directly.

There are three common compiler directives:

1. Page: This directive is for the current page

2. Include: Used to specify that another page is included

3. Taglib: for specifying and accessing custom labels

The syntax format for using compile directives is as follows:

<%@ compiler directive Name Property name = "Property value" ...%>

  

Page directive:

The page directive is usually at the top of the JSP page, and a JSP page can use multiple page directives. The syntax format for the page directive is as follows:

<%@ page[language= "java"][extends= "Package.class"][import= "Package.class | package.*, ..."][session= "true | false "][buffer=" none | 8KB | size Kb "][autoflush=" true | false "][isthreadsafe=" true | false "][info=" text "][errorpage=" Relativeurl "][contenttype=" Mimetype[;charset=characterset] "| "Text/html;charset=utf-8"][pageencoding= "UTF-8"][iserrorpage= "true | false"]%>

  

The meanings of each attribute are described in detail below:

Language: Declares the type of scripting language used by the current page, all Java, without setting

Extends: Specifies the parent class inherited by the Java class that is generated by the JSP page compilation, or the implemented interface

Import: for importing packages. Several packages are automatically imported, java.lang.*, javax.servlet.*, javax.servlet.jsp.*, javax.servlet.http.*

Session: Set whether the page requires an HTTP session

Buffer: Specifies the output buffer size.

AutoFlush: Whether you need to force the contents of the output buffer when the output buffer is about to overflow

Info: Set the information for this JSP, which can be obtained through the Servlet.getservletinfo () method

ErrorPage: Specify error Handling page

Iserrorpage: Set this JSP page as an error handler

ContentType: Used to set the file format and coded character set of the generated Web page

Pageencoding: Specifies the coded character set of the generated Web page

Example:

<%@ page contenttype= "text/html; Charset=utf-8 "language=" java "%>

  

Info using:

<%@ page contenttype= "text/html; Charset=utf-8 "language=" java "info=" This is page info "%>
<%=getservletinfo ()%>

  

ErrorPage attribute is the exception handling mechanism of JSP, JSP script does not require forcing to handle exception.

When we do not process, the server will return the error message to the client.

Include directive:

With the include directive, you can embed an external file into the current JSP file and parse the JSP statements in the page.

This is a static include statement that will include other compilation directives for the target page, but dynamic include does not.

1. Static include

<% @include file= "scriptlet.jsp"%>

2. Dynamic include

<jsp:include page= "scriptlet.jsp"/>

  

The difference between static include and dynamic include:

(1), static import (include directives) specify the contained file via the file property, and the file property does not support any expressions; Dynamic import (include) actions specify the contained files through the page property, and the page property supports JSP expressions

(2), when using static import (include directive), the contents of the contained file are inserted into the containing page intact, and then the JSP compiler will eventually compile the synthesized file into a Java file, when using the dynamic import (include action) to include the file, when the identity is executed, The program forwards the request to the contained page (that is, a servlet), prints the execution results to the browser, and then returns the code that contains the page to continue execution. Because the server executes multiple files, the JSP compiler compiles the files separately

(3), when using the include directive contains a file, because the contained file will eventually generate a file, so in the contained, contains the file cannot have the name of the variable or method, while the include action contains the file, because each file is compiled separately, So the variables and methods that have the same name in the included file and the containing file do not conflict.

(4), static import is to be imported page code fully integrated, two pages into a whole Servlet, so the imported page does not even need to be a full page, and dynamic import uses the Include method in the Servlet to introduce the content of the imported page

3 Compiler directives for JSP

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.