Xi. JSP and grammar summary
- 2020-05-05 11:42:41
- OfStack
11.1 overview
JavaServer Pages (JSP) enables us to separate the static HTML and dynamic parts of the page. HTML can be written with any of the commonly used Web authoring tools in the same way as the original; The dynamic part of the code is put into special tags, most of which start with "< %" and end with "% >". For example, the following is a JSP fragment of the page, if we use http: / / host OrderConfirmation jsp? title=Core+Web+Programming the URL opens the page and the result shows "Thanks for ordering Core Web Programming".
Thanks for ordering
< I > < %= request.getParameter(" title & quot;) % > < /I >
JSP page files typically have the.jsp extension and can be installed anywhere you can store normal Web pages. While the JSP page looks more like a normal Web page than an Servlet page from a coding point of view, JSP is actually eventually converted to regular Servlet, with static HTML directly output to the output stream associated with the Servlet service method.
The conversion from JSP to Servlet typically occurs when the first page request appears. So, if you want the first user not to wait too long for the JSP page to be converted to Servlet and want to make sure Servlet is properly compiled and loaded, you can request the JSP page yourself after installing it.
Also note that many Web servers allow you to define aliases, so an URL that appears to point to an HTML file might actually point to an Servlet or JSP page.
In addition to the normal HTML code, there are three main components embedded in the JSP page: script elements (Scripting Element), instructions (Directive), and actions (Action). Script elements are used to embed Java code, which will become part of the converted Servlet. The JSP directive is used to control the Servlet structure as a whole; Actions are used to introduce existing components or to control the behavior of the JSP engine. To simplify script elements, JSP defines a set of variables (predefined variables) that can be used directly, such as request in the previous code snippet.
Note that this article is based on the JSP 1.0 specification. The new version of JSP has many major changes from version 0.92. While these changes will only make JSP better, it should be noted that the JSP page in 1.0 is almost completely incompatible with the earlier JSP engine.
11.2 JSP syntax summary JSP element syntax
The JSP expression < %= expression % > evaluates the expression and outputs the result. The equivalent XML expression for is:
< jsp: expression >
expression
< / jsp: expression >
Predefined variables available include: request, response, out, session, application, config, pageContext. These predefined variables can also be used in JSP Scriptlet.
JSP Scriptlet < % code % > inserts the code into the service method. The equivalent XML expression for is
< jsp: scriptlet >
code
< / jsp: scriptlet >
JSP declares < %! The code % > code is inserted into the Servlet class (outside the service method). The equivalent XML expression for is
< jsp: declaration >
code
< / jsp: declaration >
page directive < % @page att=" val & quot; % > global instruction on Servlet engine. The equivalent XML expression of is
< jsp: directive page att = & quot; val & quot; \ >.
Valid attributes are shown in the following table, where the default value is shown in bold:
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 directive < % @include file=" url & quot; When JSP is converted to Servlet, the specified file on the local system should be included. The equivalent XML expression for is
< jsp: directive include
file = & quot; url & quot; \ >
Where URL must be relative to URL.
With the jsp:include action, files can be introduced when requested (rather than when JSP is converted to Servlet).
JSP annotation < %-- comment --% > annotation < %-- comment --% > annotation; JSP is ignored when converted to Servlet. If you want to embed comments in the resulting HTML document, use the normal HTML comment tag < -- comment -- >.
jsp:include action < jsp:include
page="relative URL"
flush = & quot; true & quot; When Servlet is requested, the specified file is introduced. if you want to include a file in your page conversion, use the JSP include directive.
Note: on some servers, the included files must be HTML or JSP, depending on the server (usually based on the file extension).
jsp:useBean action < jsp:useBean att=val*/ > or
< jsp:useBean att=val* >
...
< /jsp:useBean > find or instantiate an Java Bean. Possible attributes of include:
id="name"
scope="page|request
|session|application"
class="package.class"
type="package.class"
beanName="package.class"
jsp:setProperty action < jsp:setProperty att=val*/ > sets the properties of Bean. You can either set an exact value or specify that the property value comes from the request parameter. Legal attributes of include:
name="beanName"
property="propertyName|*"
param="parameterName"
value="val"
jsp:getProperty < jsp:getProperty
name="propertyName"
value = & quot; val & quot; / > extracts and outputs the properties of Bean.
jsp:forward action < jsp:forward
page = & quot; relative URL & quot; / > directs the request to another page.
jsp:plugin action < jsp:plugin
attribute = & quot; value & quot; * >
...
< /jsp:plugin > generates either OBJECT or EMBED tags based on the browser type in order to run Java Applet through Java Plugin.
11.3 about template text (static HTML)
Many times, a large portion of an JSP page consists of static HTML, also known as "template text." The template text is almost identical to regular HTML in that they follow the same syntax rules, and the template text is also sent directly to the client by Servlet. In addition, template text can be written with any existing page-making tool.
The only exception is that if you want to print "< %", you should write "< \ %" in the template text.
JavaServer Pages (JSP) enables us to separate the static HTML and dynamic parts of the page. HTML can be written with any of the commonly used Web authoring tools in the same way as the original; The dynamic part of the code is put into special tags, most of which start with "< %" and end with "% >". For example, the following is a JSP fragment of the page, if we use http: / / host OrderConfirmation jsp? title=Core+Web+Programming the URL opens the page and the result shows "Thanks for ordering Core Web Programming".
Thanks for ordering
< I > < %= request.getParameter(" title & quot;) % > < /I >
JSP page files typically have the.jsp extension and can be installed anywhere you can store normal Web pages. While the JSP page looks more like a normal Web page than an Servlet page from a coding point of view, JSP is actually eventually converted to regular Servlet, with static HTML directly output to the output stream associated with the Servlet service method.
The conversion from JSP to Servlet typically occurs when the first page request appears. So, if you want the first user not to wait too long for the JSP page to be converted to Servlet and want to make sure Servlet is properly compiled and loaded, you can request the JSP page yourself after installing it.
Also note that many Web servers allow you to define aliases, so an URL that appears to point to an HTML file might actually point to an Servlet or JSP page.
In addition to the normal HTML code, there are three main components embedded in the JSP page: script elements (Scripting Element), instructions (Directive), and actions (Action). Script elements are used to embed Java code, which will become part of the converted Servlet. The JSP directive is used to control the Servlet structure as a whole; Actions are used to introduce existing components or to control the behavior of the JSP engine. To simplify script elements, JSP defines a set of variables (predefined variables) that can be used directly, such as request in the previous code snippet.
Note that this article is based on the JSP 1.0 specification. The new version of JSP has many major changes from version 0.92. While these changes will only make JSP better, it should be noted that the JSP page in 1.0 is almost completely incompatible with the earlier JSP engine.
11.2 JSP syntax summary JSP element syntax
The JSP expression < %= expression % > evaluates the expression and outputs the result. The equivalent XML expression for is:
< jsp: expression >
expression
< / jsp: expression >
Predefined variables available include: request, response, out, session, application, config, pageContext. These predefined variables can also be used in JSP Scriptlet.
JSP Scriptlet < % code % > inserts the code into the service method. The equivalent XML expression for is
< jsp: scriptlet >
code
< / jsp: scriptlet >
JSP declares < %! The code % > code is inserted into the Servlet class (outside the service method). The equivalent XML expression for is
< jsp: declaration >
code
< / jsp: declaration >
page directive < % @page att=" val & quot; % > global instruction on Servlet engine. The equivalent XML expression of is
< jsp: directive page att = & quot; val & quot; \ >.
Valid attributes are shown in the following table, where the default value is shown in bold:
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 directive < % @include file=" url & quot; When JSP is converted to Servlet, the specified file on the local system should be included. The equivalent XML expression for is
< jsp: directive include
file = & quot; url & quot; \ >
Where URL must be relative to URL.
With the jsp:include action, files can be introduced when requested (rather than when JSP is converted to Servlet).
JSP annotation < %-- comment --% > annotation < %-- comment --% > annotation; JSP is ignored when converted to Servlet. If you want to embed comments in the resulting HTML document, use the normal HTML comment tag < -- comment -- >.
jsp:include action < jsp:include
page="relative URL"
flush = & quot; true & quot; When Servlet is requested, the specified file is introduced. if you want to include a file in your page conversion, use the JSP include directive.
Note: on some servers, the included files must be HTML or JSP, depending on the server (usually based on the file extension).
jsp:useBean action < jsp:useBean att=val*/ > or
< jsp:useBean att=val* >
...
< /jsp:useBean > find or instantiate an Java Bean. Possible attributes of include:
id="name"
scope="page|request
|session|application"
class="package.class"
type="package.class"
beanName="package.class"
jsp:setProperty action < jsp:setProperty att=val*/ > sets the properties of Bean. You can either set an exact value or specify that the property value comes from the request parameter. Legal attributes of include:
name="beanName"
property="propertyName|*"
param="parameterName"
value="val"
jsp:getProperty < jsp:getProperty
name="propertyName"
value = & quot; val & quot; / > extracts and outputs the properties of Bean.
jsp:forward action < jsp:forward
page = & quot; relative URL & quot; / > directs the request to another page.
jsp:plugin action < jsp:plugin
attribute = & quot; value & quot; * >
...
< /jsp:plugin > generates either OBJECT or EMBED tags based on the browser type in order to run Java Applet through Java Plugin.
11.3 about template text (static HTML)
Many times, a large portion of an JSP page consists of static HTML, also known as "template text." The template text is almost identical to regular HTML in that they follow the same syntax rules, and the template text is also sent directly to the client by Servlet. In addition, template text can be written with any existing page-making tool.
The only exception is that if you want to print "< %", you should write "< \ %" in the template text.