11.1 Overview
Assumerver pages (JSP) enables us to separate static html and dynamic parts of the page. HTML can be written using any commonly used web production tool in the same way as the original one.CodePut in the special tag, most of them start with "<%" and end with "%>. For example, the following is a piece of JSP page. If we use http: // host/orderconfirmation. jsp? Title = Core + Web + programming this URL opens this page, the result shows "Thanks for ordering core web programming ".
Thanks for ordering
<I> <% = request. getparameter ("title") %> </I>
JSP page files generally use. jsp as the extension and can be installed in any place that can store common web pages. Although from the code writing perspective, JSP pages are more like common web pages than Servlets, in fact, JSP will eventually be converted into a formal servlet, static HTML is directly output to the output stream associated with the servlet service method.
The conversion process from JSP to servlet is generally performed when the first page request appears. Therefore, if you want the first user not to wait too long because the JSP page is converted into a servlet, make sure that the servlet has been correctly compiled and loaded, you can request this page after installing the JSP page.
Note that many web servers allow alias definitions, so a URL that looks to point to an HTML file may actually point to a servlet or JSP page.
In addition to common HTML code, there are three main elements embedded into the JSP page: scripting element, Directive, and action ). The script element is used to embed Java code, which will become part of the converted servlet. jsp commands are used to control the servlet structure as a whole; action is used to introduce existing components or control the behavior of the JSP Engine. To simplify the script elements, JSP defines a set of variables that can be directly used (pre-defined variables). For example, the request in the previous code snippet is one of them.
Note that this article is based on the JSP 1.0 specification. Compared with version 0.92, the new version of JSP has made many major changes. Although these changes only make JSP better, it should be noted that 1.0 of JSP pages are almost completely incompatible with the early JSP Engine.
11.2 JSP syntax summary table JSP element syntax description remarks
JSP expression <% = expression %> calculates the expression and outputs the result. The equivalent XML expression is:
<JSP: expression>
Expression
</Jsp: expression>
You can use the following predefined variables: request, response, out, session, application, config, and pagecontext. These predefined variables can also be used in JSP scriptlet.
JSP scriptlet <% code %> inserts code 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 the service method ). The equivalent XML expression is:
<JSP: Declaration>
Code
</Jsp: Declaration>
The page command <% @ page ATT = "Val" %> acts on the global commands of the servlet engine. The equivalent XML expression is
<JSP: Directive. Page ATT = "Val" \>.
Valid attributes are listed in the following table. Bold indicates 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 command <% @ include file = "url" %> when JSP is converted to servlet, it should contain the specified file on the local system. The equivalent XML expression is:
<JSP: Directive. Include
File = "url" \>.
The URL must be a relative URL.
The JSP: include action can be used to introduce files during requests (instead of converting JSP into servlet.
JSP comments <% -- Comment -- %> are ignored when JSP is converted to servlet. If you want to embed comments into the result HTML document, use the common HTML comments to mark <-- Comment -->.
JSP: include action <JSP: Include
Page = "relative URL"
Flush = "true"/> when the servlet is requested, the specified file is introduced. If you want to include a file during page conversion, use the JSP include command.
Note: On Some servers, the contained file must be an HTML file or JSP file, which is determined by the server (usually determined based on the file extension ).
JSP: usebean action <JSP: usebean ATT = Val */> or
<JSP: usebean ATT = Val *>
...
</Jsp: usebean> Find or instantiate a Java Bean. Possible attributes include:
Id = "name"
Scope = "Page | request
| Session | application"
Class = "package. Class"
Type = "package. Class"
Beanname = "package. Class"
JSP: setproperty action <JSP: setproperty ATT = Val */> to set bean attributes. You can set a definite value or specify the attribute value from the request parameter. Valid attributes include:
Name = "beanname"
Property = "propertyname | *"
Param = "parametername"
Value = "Val"
JSP: getproperty action <JSP: getproperty
Name = "propertyname"
Value = "Val"/> extract and output bean attributes.
JSP: forward action <JSP: Forward
Page = "relative URL"/> transfers the request to another page.
JSP: plugin action <JSP: plugin
Attribute = "value" *>
...
</Jsp: plugin> Generate the object or embed tag Based on the browser type to run the Java Applet through Java Plugin.
11.3 about template text (static html)
Many times, a large part of JSP pages are composed of static html, which is also known as "template text ". The template text is almost identical to that of common HTML, both comply with the same syntax rules, and the template text is directly sent to the client by servlet. In addition, the template text can also be written using any existing page creation tool.
The only exception is that if "<%" is to be output, the template text should be written as "<\ % ".