On JSP Grammar Encyclopedia and Example analysis

Source: Internet
Author: User
Tags end expression flush variables string version variable valid
js| Syntax html annotation

Displays a comment on the client.

JSP syntax

spacing= "0" cellpadding= "2" bordercolorlight = "BLACK" Bordercolordark = "#FFFFFF" align= "center" > <!--comment [&l t;%= expression%>]-->

Example 1

<!--This file displays the user login screen-->
Generate the same data as above in the client's HTML source code:
<!--This file displays the user login screen-->

Example 2

<!--This page is loaded on <%= (new Java.util.Date ()). toLocaleString ()%>-->

Displayed in the client's HTML source code as:

<!--This page is loaded on January 1,-->

Describe

This annotation is similar to HTML in that it can be seen in "viewing source code."

The only difference is that you can use an expression (shown in Example 2) in this comment. This expression is variable, different from the page, you can use a variety of expressions, as long as it is legitimate on the line.

Hide Annotations

Write in the JSP program, but not to the customer.

JSP syntax

<%--Comment--%>

Example:

<%@ page language= "java"%>
<body>
<%--This comment'll is visible in the page source--%>
</body>

Describe

Characters marked with hidden annotations are ignored when the JSP is compiled. This annotation is useful when you want to hide or annotate your JSP program.

The JSP compiler does not compile the statements between the%--and--%. It will not be displayed in the customer's browser, and will not be seen in the source code in the%----%, you can write any comment statements, but you cannot use "--%>", if you have to use "--%\>".

Statement

Declare valid variables and methods in the JSP program.

JSP syntax

<%! Declaration; [Declaration;] + ...%>

Example

<%! int i = 0; %>
<%! int A, b, C; %>
<%! Circle a = new Circle (2.0); %>

Describe

Declare the variables and methods that you will use in your JSP program. You must do the same, or you will make mistakes.

You can declare multiple variables and methods at once, as long as the ";" At the end of the line, of course, these statements in Java if legitimate.

When you declare a method or variable, be aware of some of the following rules:

1 The statement must be with ";" The end (Scriptlet has the same rule, but the expression is different).

2 You can directly use the declared variables and methods that are included in% @ Page, and you do not need to declare them again.

31 declarations are valid only in one page. If you want to use a few statements for each page, it's best to write them in a separate file and include them with%@ include% or jsp:include elements.

An expression

Contains an expression that conforms to the JSP syntax.

JSP syntax

<%= expression%>

Example

<font color= "Blue" ><%= map.size ()%></font>
<b><%= numguess.gethint ()%></b>.

Describe

An expression element represents an expression defined in a scripting language that is automatically converted to a string after it is run, and then inserted into this expression at the location of the JSP file. Because the value of this expression has been converted to a string, you can insert the expression in one line of text (in the same way as an ASP).

When you use an expression in your JSP, keep the following points in mind:

1 You cannot use a semicolon (";" ) as the terminator of the expression. But the same expression used in scriptlet needs to end with a semicolon! View Scriptlet This expression element can include any expression that is valid in the Java Language specification.

2 Sometimes an expression can also be used as a property value for other JSP elements. An expression can become complex, and it may consist of one or more expressions that are ordered from left to right.

Scriptlet

Contains a valid program segment.

JSP syntax

Width= "border=" "1" cellspacing= "0" cellpadding= "2" bordercolorlight = "BLACK" Bordercolordark = "#FFFFFF" align= " Center "> <% Code fragment%>

Example

<%
String name = NULL;
if (Request.getparameter ("name") = = null) {
%>
<%@ include file= "error.html"%>
<%
} else {
Foo.setname (Request.getparameter ("name"));
if (Foo.getname (). Equalsignorecase ("Integra"))
Name = "Acura";
if (Name.equalsignorecase ("Acura")) {
%>

Describe

A scriptlet can contain multiple JSP statements, methods, variables, and expressions.

Because of the scriptlet, we can do the following things:

1 Declare the variables or methods that will be used (the reference declaration).

2 Write the JSP expression (reference expression).

3 Use any suppressed objects and any objects declared with Jsp:usebean.

4 Write JSP statements (if you are using the Java language, these statements must conform to the Java Language specification).

Any text, HTML tags, and JSP elements must be outside the scriptlet.

When the JSP receives the customer's request, the scriptlet is executed, and if Scriptlet has the displayed content, the displayed content is present in the Out object.

Page directives

Defines global properties in a JSP file.

JSP syntax

<%@ page
[language= "Java"]
[extends= "Package.class"]
[import= "{package.class | package.*}, ..."]
[Session= ' true | false ']
[buffer= "none | 8kb | sizekb"]
[Autoflush= ' true | false ']
[Isthreadsafe= ' true | false ']
[info= "Text"]
[errorpage= "Relativeurl"]
[contenttype= "mimetype [; Charset=characterset]" | "Text/html; Charset=iso-8859-1 "]
[Iserrorpage= ' true | false ']
%>

Example

<%@ page import= "java.util.*, java.lang.*"%>
<%@ page buffer= "5kb" autoflush= "false"%>
<%@ page errorpage= "error.jsp"%>

Describe

The%@ page% instruction acts on the entire JSP page, including the static include file. However, the% @ Page% directive does not work on dynamic include files, such as Jsp:include.

You can use more than one% @ Page instruction in one page, but the attributes can only be used once, but there is an exception, that is the import attribute. Because the Import property is similar to the import statement in Java (referencing the Java Language), you can use this attribute more than once.

Regardless of where you put the% @ Page% instruction in the JSP file, it is scoped to the entire JSP page. However, for the readability of JSP programs, as well as good programming habits, it is best to put it in the top of the JSP file.

Taglib directives

Defines a tag library and its prefix for a custom label.

JSP syntax

<%@ taglib uri= "uritotaglibrary" prefix= "TagPrefix"%>

Example

<%@ taglib uri= "http://www.jspcentral.com/tags" prefix= "public"%>
<public:loop>
.
.
</public:loop>

Describe

The% @ taglib% directive declares that the JSP file uses a custom label, references the tag library, and also specifies the prefix of their tags.

The custom label here contains the label and the element. Because JSP files can be translated into XML, it is important to understand the links between tags and elements. The label is simply a markup that is raised in a sense and is part of the JSP element. The JSP element is part of the JSP syntax and has the same start and end tags as XML. An element can also contain other text, tags, and elements. For example, a jsp:plugin element has a jsp:plugin start tag and a/jsp:plugin end tag, as well as jsp:params and jsp:fallback elements.

You must use the% @ taglib% instruction before using a custom label, and you can use it more than once in a page, but the prefix can only be used once.

Property

Uri= "Uritotaglibrary"

The Uniform Resource Identifier (URI) uniquely names a custom label based on the prefix of the label, which can be the following:

Uniform Resource Locator (URL), defined by RFC 2396, to view http://www.hut.fi/u/jkorpela/rfc/2396/full.html.

Uniform Resource Name (URN), defined by RFC 2396.

A relative or absolute path

prefix= "TagPrefix"

The prefix before the custom label, for example, public in, if not public here, then this is illegal. Do not use JSP, Jspx, Java, javax, servlet, Sun, and sunw to do your prefix.

Jsp:forward

redirect an HTML file, JSP file, or a program segment.

JSP syntax

<jsp:forward page={"Relativeurl" | "<%= expression%>"}/>
Or
<jsp:forward page={"Relativeurl" | "<%= expression%>"} >
<jsp:param name= "ParameterName"
Value= "{parametervalue | <%= expression%>}"/>+
</jsp:forward>

Example

<jsp:forward page= "/servlet/login"/>
<jsp:forward page= "/servlet/login" >
<jsp:param name= "username" value= "jsmith"/>
</jsp:forward>

Describe

The Jsp:forward tag passes a Request object containing the user's requests from a JSP file to another file. The code below the Jsp:forward tag will not be executed.

You can transfer parameters and values to the target file, in this case we pass the parameter named Username, the value is Scott, if you use the Jsp:param tag, the target file must be a dynamic file to handle the parameters.

If you use a non buffered output, be careful when using Jsp:forward. If the JSP file already has data before you use Jsp:forward, then the file execution will go wrong.

Property

Page= "{relativeurl | <%= expression%>}"

Here is an expression or a string that describes the file or URL you are going to orient. This file can be a JSP, a program segment, or any other file that can handle the request object (such as ASP, CGI, PHP).

<jsp:param name= "ParameterName value=" {parametervalue | <%= expression%>} "/>+

Send one or more parameters to a dynamic file, which must be a dynamic file.

If you want to pass multiple parameters, you can use multiple Jsp:param in a JSP file. NAME specifies the parameter name, and value specifies the parameter value.

Jsp:getproperty

Gets the value of the Bean's property for display in the page.

JSP syntax

d_nobr> <jsp:getproperty name= "Beaninstancename" property= "PropertyName"/>

Example

<jsp:usebean id= "Calendar" scope= "page" "class=" employee. Calendar "/>
Calendar of <jsp:getproperty name= "Calendar" property= "username"/>

Describe

This jsp:getproperty element will get the Bean's property value and can be used or displayed in the JSP page. Before you use Jsp:getproperty, you must create it with Jsp:usebean.

The Jsp:getproperty element has some limitations:

1 You can't use Jsp:getproperty to retrieve a property that has been indexed.

2 You can use Jsp:getproperty with the JavaBeans component, but not with the enterprise bean.

Property

Name= "Beaninstancename"

The name of the bean, specified by Jsp:usebean.

Property= "PropertyName"

The name of the property of the specified bean.

Skills:

As mentioned in Sun's JSP reference, if you use Jsp:getproperty to retrieve a value that is null, then NullPointerException will appear, and if you use a program segment or an expression to retrieve its value, then a null (NULL) appears on the browser.

Jsp:include

Contains a static or dynamic file.

JSP syntax

<jsp:include page= "{relativeurl | <%= expression%>}" flush= "true"/>
Or
<jsp:include page= "{relativeurl | <%= expression%>}" flush= "true" >
<jsp:param name= "ParameterName"
Value= "{parametervalue | <%= expression%>}"/>+
</jsp:include>

Example

<jsp:include page= "scripts/login.jsp"/>
<jsp:include page= "copyright.html"/>
<jsp:include page= "/index.html"/>
<jsp:include page= "scripts/login.jsp" >
<jsp:param name= "username" value= "jsmith"/>
</jsp:include>

Describe

The Jsp:include element allows you to include dynamic files and static, and the results of the two include files are different. If the file is just a static file, the inclusion is simply adding the contents of the containing file to the JSP file, and if the file is dynamic, the included file is also executed by the JSP compiler (which is similar to the ASP).

You can't judge whether a file is dynamic or static from the filename, for example, aspcn.asp may only contain some information, but it doesn't need to be executed. Jsp:include can handle both files at the same time, so you don't need to include them and decide whether the file is dynamic or static.

If the inclusion file is dynamic, you can also pass the parameter name and parameter values with Jsp:param.

Property

Page= "{relativeurl |}"

The parameter is a relative path, or an expression that represents a relative path.

Flush= "true"

Here you must use Flush= "true", you cannot use the value of false. The default value is False.

<jsp:param name= "ParameterName" value= "{parametervalue | <%= expression%>}"/>+

Jsp:param clause allows you to pass one or more arguments to a dynamic file, and you can use multiple Jsp:param in one page to pass multiple arguments.

Jsp:plugin

Execute an applet or bean and, if possible, download a Java plug-in to execute it.

JSP syntax

<jsp:plugin
Type= "Bean | Applet
Code= "Classfilename"
Codebase= "Classfiledirectoryname"
[Name= "InstanceName"]
[archive= "uritoarchive, ..."]
[align= "bottom | top | middle |
[height= "Displaypixels"]
[width= "Displaypixels"]
[hspace= "Leftrightpixels"]
[vspace= "Topbottompixels"]
[jreversion= "Jreversionnumber | 1.1"]
[nspluginurl= "Urltoplugin"]
[iepluginurl= "Urltoplugin"] >
[<jsp:params>
[<jsp:param name= "ParameterName"
Value= "{parametervalue | <%= expression%>}"/>]+
</jsp:params>]
[<jsp:fallback> text message for user </jsp:fallback>]
</jsp:plugin>

Example

<jsp:plugin type=applet code= "Molecule.class" codebase= "/html" >
<jsp:params>
<jsp:param name= "molecule" value= "Molecules/benzene.mol"/>
</jsp:params>
<jsp:fallback>
<p>unable to load applet</p>
</jsp:fallback>
</jsp:plugin>

Describe

The Jsp:plugin element is used to play or display an object in the browser (typically applets and beans), and this display needs to be in the browser's Java plug-in.

When a JSP file is compiled and sent to the browser, the Jsp:plugin element is replaced with an object or EMBED element according to the version of the browser. Note that object is used for HTML 4.0,embed for HTML 3.2.

In general, the Jsp:plugin element specifies whether the object is an applet or a bean, and also specifies the name of the class, as well as the location, and also specifies where the Java plug-in will be downloaded. Specifically as follows:

Property

Type= "Bean | Applet

The type of plug-in object that will be executed, you must specify whether this is a bean or an applet, because this property has no default value.

Code= "Classfilename"

The name of the Java class that will be executed by the Java plug-in, which must end with. class. This file must exist in the directory specified by the CODEBASE property.

Codebase= "Classfiledirectoryname"

The directory (or path) of the Java class file that will be executed, and if you do not provide this attribute, the directory using the Jsp:plugin JSP file will be used.

Name= "InstanceName"

The name of this bean or applet instance, which will be invoked elsewhere in the JSP.

Archive= "uritoarchive, ..."

Some path names separated by commas that are used to preinstall some of the class that will be used, which improves the performance of the applet.

align= "Bottom | Top | Middle | Left | Right

The position of the graphic, object, applet, with the following values:

Bottom

Top

Middle

Left

Right

height= "Displaypixels" width= "Displaypixels"

The long, wide value that the applet or bean will display, which is a number, in pixels.

Hspace= "Leftrightpixels" vspace= "Topbottompixels"

The amount of space, in pixels, required to be left on or off the screen when the applet or bean is displayed.

jreversion= "Jreversionnumber | 1.1 "

The version of the Java Runtime Environment (JRE) required to run the applet or the bean.

The default value is 1.1.

Nspluginurl= "Urltoplugin"

Netscape Navigator user can use the JRE download address, this value is a standard URL, such as http://www.aspcn.com/jsp

Iepluginurl= "Urltoplugin"

IE users can use the JRE download address, this value is a standard URL, such as http://www.aspcn.com/jsp

<jsp:params> [<jsp:param name= "ParameterName"
Value= "{parametervalue | <%= expression%>}"/>]+ </jsp:params>

The parameter or parameter value you need to pass to the applet or bean.

<jsp:fallback> text message for user </jsp:fallback>

A piece of text for the Java plug-in can not start when displayed to the user, if the plug-in can start and the applet or bean can not, then the browser will have an error message pop-up.

Jsp:usebean

Create a bean instance and specify its name and scope.

JSP syntax

<jsp:usebean
Id= "Beaninstancename"
Scope= "page | Request | Session | Application "
{
class= "Package.class" |
Type= "Package.class" |
class= "Package.class" type= "Package.class" |
Beanname= "{package.class | <%= expression%>}" type= "Package.class"
}
{
/> |
> Other elements </jsp:useBean>
}

Example

<jsp:usebean id= "cart" "scope=" Session "class=" session. Carts "/>
<jsp:setproperty name= "cart" property= "*"/>
<jsp:usebean id= "Checking" scope= "session" class= "bank. Checking ">
<jsp:setproperty name= "Checking" property= "balance" value= "0.0"/>
</jsp:useBean>

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.