JSP (i) JSP elements

Source: Internet
Author: User
Tags getcolor

(1) instruction element: used to send a message from the JSP to the container, such as setting global variables, text encoding, introducing packets, etc.

1. Page

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

Common properties in the page directive:

Language: Defines the scripting language to use, currently only "Java", that is, language= "Java".

Import: The same as the General Java import meaning, used to introduce the class to use, just a comma "," separate the package or class list. Omitted by default, that is, no other classes or packages are introduced. For example, import= "java.io.*,java.util.hashtable".

Session: Specifies whether the page is participating in an HTTP session. The default value is True,session= "true".

Buffer: Specifies the buffering mode to the customer output stream. If none is not buffered, if a value is specified, then the output is buffered with a buffer that is not less than this value. Used in conjunction with Autoflash. The default is not less than 8KB and can be set according to different servers. For example, buffer= "64KB".

Autoflash: If the full buffer is true, the output to the client is flushed, and if the buffer is full, a run exception indicates a buffer overflow. The default is true, for example autoflash= "true".

Info: For information about JSP pages, define a string that can be obtained using Servlet.getservletinfo (). omitted by default. For example, info= "test page".  

Iserrorpage: Indicates whether the current page is a errorpage destination for another page. If set to True, the exception object can be used. Conversely, if set to False, the exception object cannot be used. The default is False, for example, iserrorpage= "true".

ErrorPage: Defines the page that is called when an exception occurs on this page. Ignored by default, such as errorpage= "error.jsp".

IsThreadSafe: Used to set whether the JSP file can be used in multiple threads. If set to true, a JSP can handle requests from multiple users at the same time, whereas if set to false, a JSP can only process one request at a time. For example, isthreadsafe= "true".

ContentType: Defines the MIME type for JSP character encoding and page response. Type=mime Type;charset=charset. The default is type=text/html,charset=iso8859-1. For example contenttype= "text/html;charset=gb2312".

Pageencoding:jsp the character encoding of the page, the default value is Pageencoding= "Iso-8859-1", for example pageencoding= "gb2312".

Iselignored: Specifies whether an El (expression language) is ignored. If true, the container ignores the evaluation of the expression "${}". The default value is determined by the version of the Web. Xml description file, which is ignored by the previous version of servlet2.3. For example iselignored= "true".

2. Include

1. Two ways to use

<%@ include file= ""%>

<jsp:include page= "" flush= "true"/> (action Element)

2. Usage differences

(1) Difference in execution time

<%@ include file= "relativeuri"%> is executed during the translation phase

<jsp:include page= "Relativeuri" flush= "true"/> is executed during the request processing phase.

(2) The different content of the introduction

<%@ include file= ""%>

The include directive element of the JSP reads the contents of the specified page. and combine the content with the original page. (This process is done during the translation phase: that is, the JSP is transformed into a servlet.)

<jsp:include page= "Relativeuri" flush= "true"/> Introduces the answer text that is innate by the execution page or servlet.

3, tablib Custom Label

(2) Script elements

JSP declaration


declarations (declaration) are used to declare variables and define methods in JSP pages. A declaration is a label that begins with <%! and ends with a%>, which can contain any number of legitimate Java declaration statements. Here is an example of a JSP declaration:

<%! int count = 0; %>

The above code declares a variable named count and initializes it to 0. Declared variables are initialized only once by the first time the page is loaded, and are persisted in subsequent requests after initialization.

The following code declares a variable and a method in a label:

<%!

String color[] = {"Red", "green", "Blue"};

String getColor (int i) {

return color[i];

}

%>

You can also write the above two Java declaration statements in two JSP declaration tags, such as:

<%! String color[] = {"Red", "green", "Blue"}; %>

<%!

String getColor (int i) {

return color[i];

}

%>


JSP Small Script


A small script (scriptles) is a Java code snippet embedded in a JSP page. This is a label that begins with <% and ends with a%>. For example <% count++; %>

The small script is executed every time the page is accessed, so the count variable is increased by 1 on each request. Because a small script can contain any Java code, it is often used to embed computational logic on a JSP page. You can also print HTML template text using small scripts.


JSP expressions


An expression is a label that begins with <%= and ends with a%>, which acts as a placeholder for a Java language expression. such as <%= count%>

Contains an expression that conforms to the Java syntax. The element of an expression is automatically converted to a string after it is run, and then inserted into the location of the JSP file of the expression. Because the value of this expression has been converted to a string, the expression can be inserted in a line of text. The expression is a simplified out.println statement.

The expression is evaluated each time the page is accessed, and its value is embedded in the output of the HTML.

Unlike a variable declaration, an expression cannot end with a semicolon, so the following code is illegal:

<%= count; %>

You can use an expression to output the value of any object or any base data type to the output stream, or you can print any arithmetic expression, Boolean expression, or value returned by a method call.

There can be no spaces between the percent sign and the equal signs of the JSP expression.

(3) Action elements

<jsp:forword page= "realurl.jsp"/>

(4) JSP annotations

<%--This is a comment--%>

<!--This is a comment--

JSP (i) JSP element

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.