JSP overview and Page Technology

Source: Internet
Author: User
Tags file url

 

JSP is a more advanced extension of Servlet. JSP knowledge mainly includes JSP page composition, execution process, character translation, implicit object and other technologies. Knowledge points include:

1. JSP Overview 2. JSP page technology 3. JSP Execution Process

4. JSP Exception Handling 5. JSP implicit object 6. JSP design mode.

Today, we will mainly explain the overview of JSP and the composition of pages. First, let's explain the overview of JSP:

Speaking of JSP, we have to talk about Servlet first, so let's talk about it below.

Servlet-based methods have some drawbacks:

1. Developers and maintenance application members must have a full understanding of Java programming knowledge, because processing code and processing html elements are mixed together.

2. If you want to change the appearance of the application or add support for the new customer type, you need to update and recompile the Servlet code.

3. It is difficult to make full use of Web page development tools when designing application interfaces. If this tool is used to develop Web page la S, the generated html must be manually embedded into the Servlet code. This process is time-consuming and error-prone.

In the face of the above problems, Sun Microsystems advocates the development of dynamic web pages based on the functions provided by Servlet specifications using Java as the scripting language, java Server Pages (JSP) used to display dynamic content on webpages ). The webpage consists of a Java segment or JSP tag. The webpage is suffixed with ". JSP ".

JSP technology can be run in any environment that complies with the Java syntax structure once it is written. Sun's open-source code allows many companies to participate in the establishment of JSP application interfaces (APIS) even if the standards are met, without a doubt, and will continue to improve as Java organizations continue to expand and expand.

The success of JSP depends on its own advantages, including:

1. Simplicity and effectiveness 2. Program independence 3. Program compatibility

4. reusability of the program

After finishing the overview of JSP, I believe you have a simple understanding of JSP. Next we will begin to explain the composition of JSP pages:

A JSP page is a common Web page with JSP elements. It consists of static content and dynamic content. Static content mainly refers to HTML elements, while dynamic content mainly refers to JSP elements.

Among them, JSP elements mainly include command elements, script elements, Action elements, watching and so on. Today, we only want to talk about command elements and script elements.

Command element, which is mainly used to provide JSP page information for the conversion phase. The command does not generate any output to the current output stream. It specifies information about the page itself, the information remains unchanged during the request. In addition, all JSP instruction elements are used on the current JSP page. That is to say, the command element only affects the JSP page where it is located, and does not affect other JSP pages.

The syntax of JSP commands is as follows:

<% @ Directive {attr = "value"} * %>

No space is allowed between <and %, and between % and @, and between % and>.

JSP Commands include page, include, and taglib.

Page command: this command is used to define page-related attributes on the entire page. It is the contact person for JSP pages and containers. This command can appear anywhere on the JSP page, but wherever it is, it always works on the whole page, so we usually place it at the beginning. Syntax: <% @ page attribute name 1 = "value 1" attribute name 2 = "value 2"... %>.

Note: You can use multiple page commands, but each page can only declare a given attribute once, except the import attribute. The attribute name is case sensitive.

There are 13 page attributes, and the most common attributes are:

Import = "package list": indicates the class or interface to be used for introducing the current JSP file. Multiple classes and package names are separated by commas. For example, % @ page import = "java. util. *, java. i0. *" % or % @ page import = "java. util. *" %

% @ Page import = "java. io. *" %

The import attribute is the only property that can appear multiple times on the same page. lang. *, javax. servlet. *, javax. servlet. jsp. *, javax. servlet. http. *. They are all implicitly introduced. Classes and interfaces in the package can be used without being introduced.

Language = "scriptingLangeuage": Specifies the programming language used by the program code in the current JSP file. Currently, the attribute value can only be java.

ContentType = "ctinfo": sets the MIME type and character set of the content output from the current JSP page to the client browser. The client browser displays the Servlet output content based on the MIME type and character set code you specified in contentType. (The MIME type is the type used by an application to open a file with a certain extension. When a file with the extension is accessed, the browser automatically specifies the application to open it ). Example: <% @ page contentType = "text/html; charset = UTF-8" %>

PageEncoding = "peingo": sets the character encoding used on the current JSP page. If this property is set, the character encoding of the JSP page is the character set it specifies, if not, use the value of the contentType property, if none, the page defaults to the ISO-8859-1.

Session = "true | false": indicates whether the session implicit object in the current JSP file is valid. If it is true, the session object is valid and can be used. Otherwise, the session object is invalid and cannot be used.

Buffer = "none | 8kb | sizekb": Specifies the buffer size used by the out object in the JSP file.

AutoFlush = "true | false": sets whether to automatically flush when the output buffer is full. Note that if the buffer is set to none, this attribute cannot be set to false. Otherwise, an exception is thrown during JSP file running.

ErrorPage = "relativeURL": sets the page for exception handling when the code in the current JSP page is abnormal.

IsErrorPage = "true | false": sets whether the current JSP page is specially designed to handle exceptions. If it is true, the page can use the built-in exception object, handle exceptions transferred from other JSP pages.

Include command: it is used to Include a file in a JSP page, which can be an HTML webpage, a text file or a piece of Java code. It can simplify the Page code and improve the importance of the Code. Its Syntax: <% @ include file = "relative to the current file URL" %>. The URL is the relative path of the included file. If it starts, if the root directory does not start with "/", it is relative to the current JSP page. (It is recommended that you do not use tags such as

Taglib command: it allows the page to use user-defined labels. The syntax is as follows:

<% @ Taglib (uri = "tag descriptor address with unique identifier and prefix" prefix = "prefix") %>

The following describes the script elements:

JSP script elements can be used to embed Java code into JSP pages. These Java code will appear in the Servlet generated by the current JSP page, so that JSP can separate static content from dynamic content.

Expression: The expression tag is calculated in the JSP request processing phase. The result is converted to a string and combined with static tag data. The position of the expression on the page is the position where the expression is displayed. Syntax: <% = expression %>.

(The semicolon (;) cannot be used as the expression Terminator. The expression must be a valid Java expression. The expression must have a return value and be converted to a string.

).

Script: it is the Java code embedded in <%>. The Java code here is no different from the general Java code. Therefore, each statement must end with a semicolon, this is different from the expression. Syntax: <% code %>.

Declaration: it defines the variables and methods used on the current JSP page, and the face changes defined in it are the global variables of the JSP page. After JSP is converted to Servlet, the Defined variables are converted to class variables, and the methods are converted to methods in Servlet.

Syntax: <%! String str = "hello"; %>

<%! Public static final String DEFAULT_NAME = "World"; %>

<%! Public String getName (HttpServletRequest request ){

Return request. getParameter ("name ");

%>

Note: the Declaration of variables must end with;. This declaration is only valid on one page. If you want to use some declarations on each page, you 'd better write them into a separate file, then, use the <% @ include %> or <jsp: include> element.

From XueLang Subtotal

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.