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
The 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%> inserts into the service method. The equivalent XML expression is:
<jsp:scriptlet>
Code
</jsp:scriptlet>
JSP Declaration <%! Code%> codes are inserted into the servlet class (outside of the service method). The equivalent XML expression is:
<jsp:declaration>
Code
</jsp:declaration>
The Page directive <%@ 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:
Include directives <%@ include file= "url"%> when the JSP is converted to a servlet, the specified file on the local system should be included. 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.
The JSP annotation <%--comment--%> annotation, which is ignored when the JSP is converted to 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*>
...
The </jsp:useBean> finds or instantiates a Java Bean. Possible properties include the following:
Id= "Name"
Scope= "Page|request
|session|application "
Class= "Package.class"
Type= "Package.class"
Beanname= "Package.class"
Jsp:setproperty Action <jsp:setproperty att=val*/> set the Bean's properties. 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 Action <jsp:forward
Page= "Relative URL"/> transfer the request to another page.
Jsp:plugin Action <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 "<\%".
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.