JSP Beginners must master the grammar points

Source: Internet
Author: User
Tags comments html page html tags

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

With these tips in mind, let's take a look at the JSP directives and the scripting elements, and then we'll explain JavaBeans and suppressed objects. There are five kinds of JSP directives and script elements. In JSP version 1.0, most JSPs are surrounded by a tag that starts with "<%" and ends with "%>." After the updated JSP 1.1 specification was introduced, there was an XML-compatible version.

JSP directives and script elements

Directives <%@ directive%>
Declarations <%! Declaration%>
Expressions <%= expression%>
Code Fragment/scriptlet <% code Fragment%>
Comments <%--comment--%>
 
Instructions

JSP directives are designed for the JSP engine. They don't directly produce any visible output, but just tell the engine how to handle the rest of the JSP pages. These directives are always enclosed in the "<%@?%>" tag. The two most important directives are "pagepage" and "Include". The "Taglib" directive is not in the discussion column, but it can be used to create a custom tag with JSP1.1.

"Page" instructions are seen almost at the top of all JSP pages. Although not necessary, it allows you to specify:

Where to find the supported Java categories:

<%@ page import= "Java.util.Date"%>

Where to turn on the Web surfers when there is a Java run problem:

<%@ page errorpage= "errorpage.jsp"%>

and whether you need to manage the user's session-level information, which may come from multiple pages (further in the JavaBeans section below):

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

The include directive can divide your content into more manageable elements, such as an element that includes a normal page header or footer. The included web page can be a fixed HTML page or more JSP content:

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

Statement

JSP declarations allow you to define page-level variables to hold information or to define the support methods that may be required for the remainder of the JSP page. If you find that there are too many code, it is often best to write them in a separate Java class. Declarations are generally in the "<%!?%>" tag. Be sure to end the variable declaration with a semicolon (;) because any content must be a valid Java statement:

<%! int i=0; %>.

An expression

With a JSP expression, the expression evaluation result is converted to a string and is directly included in the output page. The JSP expression is included in the "<%=?%>" tag without a semicolon unless the semicolon is used in the quoted string section.

<%= i%>
<%= "Hello"%>.

Code fragment/Script fragment

The code fragment/script fragment (Scriptlets) JSP code fragment or script fragment is embedded in the "<%?%>" tag. This Java code runs when the Web server responds to the request. There may be raw HTML or XML statements around the script fragment, where code snippets can allow you to create conditional execution code or code that uses another piece of code. For example, the following code combination uses expressions and snippets to display the string "Hello" in the H1, H2, H3, and H4 tags. Code snippets are not limited to one line of source code:

<% for (int i=1; i< =4; i++) {%>
< h<%=i%>>hello<%}%>.

Comments

The last major JSP element is an embedded annotation. Although you can always add HTML comments to a file, users will see the comments when they view the page source code. If you don't want the user to see it, you should embed it in the "<%--?--%>" tag:

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

Hide Annotations

JSP page document, but not delivered to client

JSP syntax

<%--annotation--%>
Example 1
<%@ page language= "java"%>
< html>
< head>< title> Annotation Test </titel>< body>
< h2> annotation Test <%--This section of the annotation is not visible in the original code--%>
</body>

Describe

The JSP ignores hidden text lines, and hidden annotations are often used to annotate comments that you do not want to be seen by others. The JSP does not handle any characters between <%--and--%>, and this comment is not sent to the client, but it is not found in the original code.

You can use any symbol before you use--%> to end the comment, and if you want--%> in the annotation, then you can use--%> to avoid it.

Statement

Declaring variables and methods on JSP pages

JSP syntax

<%! a statement; [A declaration;]+......%>
Example 1
<%! int i = 0; %>
<%! int A, b, C; %>
<%! Circle A = new Circle (2,0); %>

Describe

In the JSP page, if you want to use variables and methods must be declared in advance.

You can declare any number of variables and methods in a declaration element, not enough for the last few years, and you should add a semicolon at the back. The declaration must comply with the requirements of the Java programming language.

When you declare variables and methods in your JSP, you must keep in mind the following rules:

1, must end with a semicolon (the same rules apply to script writing, instead, the expression does not)

2, with the <%@ page%> introduced the pages have been declared variables and methods can be used directly, do not need to declare.

A declaration can extend a scope, that is, it can be extended to any static JSP file. Any static files contained in < jsp:include> can be used. A dynamic page is not available for annotation.

An expression

scripting language expressions in JSP pages

JSP syntax

<%= expression%>

Example 1

The map file has < font color= "Bule" ><%=map.size ()%></font>.

Good guess, but no. Try < b><%=numguess.gethint ()%></b>.

Describe

The expression is converted to a string by calculating the value and inserted into the JSP page where you want it to appear. Because the resulting value is already a string, you can control it with HTML tags in the JSP page.

When you write an expression in a JSP page, keep the following points in mind:

1, do not use a semicolon to end the expression (but, in the script in the expression is to add a semicolon; see script)

2, the expression can contain any appropriate Java language Specification expressions.

Sometimes you can use the value of an expression as a property value for a JSP element.

Script

Script segment written in scripting language on page

JSP syntax

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

Describe

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

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

2. Use an expression (see: Expression)

3, the use of fixed values and < jsp:usebean> objects

4. Use any other declarative 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.