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:
- The declaration begins with "<%!" and ends with "%>", and "%!" must be immediately
- The naming rules for variables and methods should be the same as the Java naming conventions
- You can directly use declared variables and methods that are included in < @page%>, and do not need to be re-declared
- 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:
- There is no semicolon in the expression of the JSP!
- 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