JSP grammar points that beginners must master

Source: Internet
Author: User

If you are not familiar with Java programming, you may want to take a look at Sun's guide. However, web page creators do not need to perform much Java development. In addition to some method calls, you should use Java code as little as possible in your JSP webpage.

Remember the above prompts. Let's first take a look at JSP commands and script elements, and then we will explain JavaBeans and implicit objects. There are five JSP commands and script elements. In JSP 1.0, most JSPs are enclosed by a tag starting with "<%" and ending with "%>. After the updated JSP 1.1 specification is introduced, a version compatible with XML is available.

JSP commands and script elements

Directives <% @ directive %>
Declarations <%! Declaration %>
Expressions <% = expression %>
Code Fragment/Scriptlet <% code fragment %>
Comments <% -- comment -- %>
 
Command

JSP commands are designed for the JSP Engine. Instead of directly generating any visible output, they just tell the engine how to process other JSP pages. These commands are always included in "<% @? %> "Marked. The two most important commands are "pagePage" and "Include ". The "Taglib" command is not discussed, but can be used to create a custom tag using JSP1.1.

The "Page" command is displayed on the top of almost all JSP pages. Although not required, it allows you to specify:

Where to find the supported Java categories:

<% @ Page import = "java. util. Date" %>,

Where will the Internet surfers be directed when a Java running problem occurs:

<% @ Page errorPage = "errorPage. jsp" %>,

Whether you need to manage the user's session-level information, which may come from multiple webpages (this will be further described in the following JavaBeans Section:

<% @ Page session = "true" %>.

The "Include" command can divide your content into more manageable elements, such as an element that includes a common page header or footer. The contained webpage can be a fixed HTML page or more JSP content:

<% @ Include file = "filename. jsp" %>

Statement

The JSP Declaration allows you to define page-level variables to save information or define the support methods that may be required for the rest of the JSP page. If you find that there are too many codes, you 'd better write them into an independent Java class. The statement is generally in the "<%! ? %> "Marked. You must end the variable declaration with a semicolon (;), because any content must be a valid Java statement:

<%! Int I = 0; %>.

Expression

With a JSP expression, the expression evaluation result is converted into a string and directly included in the output page. The JSP expression is contained in "<% =? "%>" Indicates that there is no semicolon, unless you use a semicolon in the string section with quotation marks.

<% = I %>
<% = "Hello" %>.

Code snippets/script snippets

Code snippets/Scriptlets JSP code snippets or script snippets are embedded in "<%? '%>. This Java code runs when the Web server responds to the request. The script fragment may be surrounded by original HTML or XML statements. In these cases, the code fragment allows you to create conditional Execution Code or use code of another piece of code. For example, the following code combines expressions and code snippets to display the string "Hello" in the H1, H2, H3, and H4 tags ". Code snippets are not limited to a single line of source code:

<% For (int I = 1; I <= 4; I ++) {%>
<H <% = I %> Hello </H <% = I %>
<% }%>.

Note

The last major JSP element is embedded comments. Although you can always add HTML comments to the file, the comments are displayed when you view the page source code. If you don't want users to see it, you should embed it into "<% --? -- %> "Marked:

<% -- Comment for server side only -- %>.

Hide comments

JSP page documentation, but not sent to the client

JSP syntax

<% -- Comment -- %>
Example 1
<% @ Page language = "java" %>
<Html>
<Head> <title> comment test </titel> <Body>
<H2> annotation test <% -- This comment cannot be seen in the original code -- %>
</Body>
</Html>

Description

JSP ignores hidden text lines. Hidden comments are usually used to comment comments you are not willing to see. JSP does not process any characters between <% -- and -- %>, and this comment is not sent to the client. Of course, you cannot view the original code.

You can use any symbol before using -- %> end annotation. If you want to use -- %> In annotation, you can use -- %> to avoid it.

Statement

Declare variables and methods on the JSP page

JSP syntax

<%! Declaration; [Declaration;] + ...... %>
Example 1
<%! Int I = 0; %>
<%! Int a, B, c; %>
<%! Circle a = new circle (2, 0); %>

Description

On the JSP page, if you want to use variables and methods, you must declare them in advance.

You can declare any number of variables and methods in a declaration element. If it is not long enough, you must add a semicolon to the end. The statement must comply with the JAVA programming language.

When declaring variables and methods in JSP, you must remember the following rules:

1. It must end with a semicolon (the same rule applies to script writing. On the contrary, expressions are not needed)

2. Variables and methods already declared on the page introduced by <% @ page %> can be directly used without further declaration.

The declaration can be extended, that is, it can be extended to any static JSP file. Any static file contained in <jsp: include> can be used. Dynamic Pages cannot be recorded.

Expression

Use a scripting expression on the JSP page

JSP syntax

<% = Expression %>

Example 1

There are <font color = "bule"> <% = map. size () %> </font> map files.

I guess it's good, but it's not. Try <B> <% = numguess. gethint () %> </B>.

Description

The expression calculates the value, converts it to a string, and inserts it to the desired place on the JSP page. Because the obtained value is a string, you can use HTML tags to control it on the JSP page.

When you write expressions on the JSP page, remember the following points:

1. Do not end the expression with a semicolon (however, the expression in the script requires a plus sign; refer to the script)

2. expressions can contain any regular expressions suitable for Java.

Sometimes you can use the expression value as the attribute value of the JSP element.

Script

Script segments written in script language on the page

JSP syntax

<% = Code segment %>
Example 1
<%
String name = null;
If (request. getParameter ("name") = null {
%>
<% @ Include file = "error.html" %>
<%} Else {
Foo. setName (request. getParmeter ("name "));
If (foo. getname (). inclusignorecase ("integra "))
Name = "acura ";
If (name. inclusignorecase ("acura ")){
%>

Description

A piece of code can include any long code, declare any number of variables and methods, or include any number of expressions. When writing code, you can do the following:

1. Declare variables and methods for future needs (see Declaration)

2. Use expressions (see expressions)

3. Use fixed values and objects in <jsp: useBean>

4. Use any other declared value on the page (if you are using the JAVA language, you must follow the JAVA rules) text, HTML tags, and JSP elements must be outside the script. Script Execution takes time.

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.