JSP----Declaration, expression, script, comment

Source: Internet
Author: User

Statement

In a JSP page, you can declare one or more valid variables and methods, and the declared variables and methods can be used anywhere in this JSP page, and will be initialized when the JSP page initializes.

The syntax format is as follows:

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

Be aware of the declaration:

    1. The declaration begins with "<%!" and ends with "%>", and "%!" must be immediately
    2. The naming rules for variables and methods should be the same as the Java naming conventions
    3. You can directly use declared variables and methods that are included in < @page%>, and do not need to be re-declared
    4. A declaration is valid only on one page, and if you want to use some declarations for each page, you can write them as a separate file, with the "<%@ include%>" directive or the "<jsp:include>" action included

For example:

<%! intI= 0;%><%! intx, y, z%><%! StringStr="Great , my brother.";%><%! Date Date = New Date();%>
An expression

Expressions in JSPs can convert data into a string to output directly on a Web page, or generate a dynamic link address, or dynamically specify a from form processing page

The syntax format is as follows:

<%=expression%>

Need to note:

    1. There is no semicolon in the expression of the JSP!
    2. There can be no spaces between "%="

For example:

<%! StringPath="http://www.cnblogs.com/yuanmiemie/";%>    <ahref= "<%=path%>">Click to jump</a>    <formAction= "<%=path%>">        <inputtype= "Submit"value= "Click to jump"/>    </form>
Script

The script is scriptlet, the code part of the JSP, a piece of Java code that can be used almost any Java syntax, which is executed at the request time, using variables, methods, expressions, or JavaBean defined by the JSP page. The variables and methods defined by the script are valid throughout the current page, but are not shared by other threads, and the user's role in the variable does not affect other users, and the variable is destroyed when the page on which it is located is closed.

The syntax format is as follows:

<%scriptlet%>

For example:

<%! StringStr="reading for the rise of China";%><%    if(true){%>        <%=Str%><%    }%>
Comments

Comments for JSP programs have hidden comments, HTML comments, Java language annotations

Hidden annotations are standard annotations for JSPs, which are completely ignored when publishing a Web page and are not sent to the client when writing a JSP program

The syntax format is as follows:

<%--Comment--%> Comment for annotated content

HTML annotations can see HTML comments in the browser file window when publishing a Web page, in which case the JSP expression can also be used

The syntax format is as follows:

<!--comment [<%=expression%>]--> comment for annotated content

Java annotations are similar to hidden annotations, and note content is not visible in the browser's source file window

The syntax format is as follows:

<%//single-line comment%>

<%/* Multi-line Comment */%>

<%/** Document Comment */%>

For example:

<!--HTML Comment: This line is displayed in the source file -<%--Hide Comments: This line does not appear in the source file--%><% //Java Single-line comment: This line is not displayed in the source file%><% /*Java Multiline Comment: These lines are not displayed in the source file*/%><% /**Java Documentation Comments: These lines are not displayed in the source file*/%>

JSP----Declaration, expression, script, comment

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.