JSP syntax (1)--html Annotation hide Annotation Declaration Expression scriptlet page instruction taglib instruction __html

Source: Internet
Author: User
Tags rfc

HTML annotation

Displays a comment on the client.

JSP syntax

<!--comment [<%= 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 comments: Written in a 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 <%--and--%>, it will not be displayed in the customer's browser, nor will it be seen in the source code between the <%----%>, you can write any annotation statements, but you cannot use "--%>", Please use "--%/>" if you have to use it.


Declaration: Declaring valid variables and methods in a 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:

The statement must be ";" The end (Scriptlet has the same rule, but the expression is different).
You can directly use the declared variables and methods that are included in the <% @ Page%>, and do not need to declare them again.

A declaration is 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.

Expression: Contains an expression that conforms to the JSP syntax

JSP syntax

<%= expression%>

Example

<font color= "Blue" ><%= map.size ()%></font>
<b><%.g= numguessethint ()%></b>.

Describe

An expression element represents an expression that is defined in a scripting language, automatically converted to a string after it is run, and then inserted into the location of the JSP file to display the expression. 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:

You cannot use a semicolon (";") as the terminator of an 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.

Sometimes an expression can also be a property value of another JSP element. An expression can become complex, and it may consist of one or more expressions, the order of which is left to right.

Scriptlet: Contains a valid program segment.

JSP syntax

<% 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, expressions

Because of scriptlet, we can do the following things:

Declare the variable or method (reference declaration) that will be used.
Write a JSP expression (a reference expression).
Use any suppressed objects and any objects declared with <jsp:useBean>
Write JSP statements (if you are using the Java language, these statements must conform to the Java Language specification).
Any text, HTML tags, 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 directive: 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 multiple <% @ Page%> instructions on one page, but the attributes can only be used once, but there is one exception, which 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 uri= "Http://java.sun.com/jsp/jstl/core" prefix= "C"%>
<% @taglib uri= "http://java.sun.com/jsp/jstl/fmt" prefix= "FMT"%>
<% @taglib uri= "http://java.sun.com/jsp/jstl/functions" prefix= "FN"%>

TAGLIB directive: Defines a tag library with its own custom label prefix.

JSP syntax

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

Example

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

Describe

<% @ 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. Elements 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, and can also have <jsp:params> and <jsp:fallback> Elements.

You must use the <% @ taglib%> directive 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, 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"
Prefixes before custom labels, such as public in <public:loop>, if this is not public, then this is illegal. Please do not use JSP, Jspx, Java, javax, servlet, Sun, and sunw as your prefix

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.