Xi. Overview of JSP and grammar

Source: Internet
Author: User
Tags expression include variables relative version
js| syntax 11.1 Overview

JavaServer pages (JSP) allows us to separate the static HTML and dynamic parts of the page. HTML can be written in any commonly used Web authoring tool and written in the same way as the original, where the code for the dynamic part is placed within a special tag, most of it begins with "<%" and ends with "%>". For example, the following is a fragment of a JSP page, and if we open the page with http://host/OrderConfirmation.jsp?title=Core+Web+Programming this URL, the result shows "the Ordering Core Web Programming ".
For ordering
<I> <%= request.getparameter ("title")%> </I>

JSP paging files are usually extended with. JSP, and can be installed anywhere that you can keep a regular web page. While the JSP page is more like a normal web page than a servlet in code writing, in fact, the JSP is eventually converted into a regular servlet, and static HTML is exported directly to the output stream associated with the Servlet service method.

The conversion process for JSP to servlet typically occurs when the first page request appears. So, if you want the first user to not wait too long for the JSP page to be converted to a servlet, and want to make sure that the servlet compiles and loads correctly, you can ask for the page yourself after you install the JSP page.

Also note that many Web servers allow you to define aliases, so a URL that appears to point to an HTML file might actually point to a servlet or JSP page.

In addition to ordinary HTML code, there are three other components of embedded JSP pages: script elements (scripting Element), instructions (Directive), actions (action). Script elements are used to embed Java code, which will be part of the converted servlet; the JSP instruction is used to control the structure of the servlet as a whole; actions are used to introduce existing components or control the behavior of the JSP engine. To simplify scripting elements, the JSP defines a set of variables that can be used directly (predefined variables), such as the one in the previous code snippet.

Note This article is based on the JSP 1.0 specification. Compared with version 0.92, the new version of JSP made a number of significant changes. While these changes will only make the JSP better, it should be noted that 1.0 of the JSP pages are almost completely incompatible with the early JSP engine.

   11.2 JSP Syntax overview table JSP element Syntax description notes
JSP expression
<%= expression %>
Evaluates an expression and outputs the result. The equivalent XML expression is:
<jsp:expression>
expression
</jsp:expression>

The predefined variables you can use include: Request,response,out,session,application,config,pagecontext. These predefined variables can also be used in JSP scriptlet.

JSP Scriptlet <% code %>
The code to insert into the service method. The equivalent XML expression is:
<jsp:scriptlet>
code
</jsp:scriptlet>

JSP declaration
<%! code %>
The code is inserted into the servlet class (outside of the service method). The equivalent XML expression is:
<jsp:declaration>
code
</jsp:declaration>

Page directives
<%@ page att="val" %>
Global directives that act on the servlet engine. The equivalent XML expression is
<jsp:directive.page att="val"\>。

The valid properties are the following table, where bold represents the default value:

import="package.class"
contentType="MIME-Type"
isThreadSafe="true|false"
session="true|false"
buffer="size kb|none"
autoflush="true|false"
extends="package.class"
info="message"
errorPage="url"
isErrorPage="true|false"
language="java"

Include directives
<%@ include file="url" %>
When a JSP is converted to a servlet, it should contain the specified file on the local system. The equivalent XML expression is:

<jsp:directive.include
file="url"\>.

Where the URL must be a relative URL.

The Jsp:include action can be used to introduce files at the time of the request, rather than when the JSP is converted to a servlet.

JSP comments
<%-- comment --%>
Note; The JSP is ignored when it is converted into a servlet. If you want to embed the annotation in the result HTML document, use the normal HTML annotation tag <--comment-->.
Jsp:include Action <jsp:include
Page= "Relative URL"
Flush= "True"/> the specified file is introduced when the servlet is requested. If you want to include a file when the page is converted, use the JSP include directive.
Note: On some servers, the included file must be an HTML file or a JSP file, which is determined by the server (usually judged by the file name extension).

Jsp:usebean Action <jsp:usebean att=val*/> or
<jsp:useBean att=val*>
...
</jsp:useBean> 寻找或实例化一个Java Bean。 可能的属性包括:
id="name"
<>

Jsp:setproperty Action
<jsp:setProperty att=val*/>
Set the properties of the bean. You can either set a certain value or specify that the property value comes from the request parameter. The legitimate properties include:
name="beanName"
property="propertyName|*"
param="parameterName"
value="val"

Jsp:getproperty Action
<jsp:getProperty
name="propertyName"

Value= "Val"/> extracts and outputs the properties of the bean.
jsp:forward动作 <jsp:forward
page="relative URL"/> 把请求转到另外一个页面。  
jsp:plugin动作 <jsp:plugin
attribute="value"*>
...
The </jsp:plugin> generates an object or embed tag based on the browser type to run the Java applet through the Java plugin.

   11.3 About template text (static HTML)

Many times, a large part of the JSP page is composed of static HTML, which is also known as "template text." Template text is almost identical to normal HTML, they all follow the same syntax rules, and template text is sent directly to the client by the servlet. In addition, template text can be written with any existing page authoring tool.

The only exception is that if you want to output "<%", the template text should be written "<\%".


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.