1. JSP annotations
2. JSP Declaration
3. JSP expression
4. JSP Script
JSP Comments:
Comment Format:
<%--Comment Content--%>
Note that the comments of the JSP are not exported to HTML.
JSP declaration:
JSP declarations are used to declare variables and methods. Declaring a method in a JSP declaration looks special, and it seems that you can define a method without defining a class, and that the method seems to exist independently from the class.
In fact, the JSP declaration will be converted into a member variable or method of the corresponding Servlet.
Declaration syntax: (using the <%! ...%> "This format, the first percent sign after a"! ")
<%!//declares an integer variable public int count;//declares a method public String info () { return "Browse Times:" + count;} %>
JSP expression:
The output expression cannot be followed by a semicolon <%= expression%><%=count++%><%=info ()%>
Open multiple browsers, we will find that the value of the count variable is continuous because the JSP page is compiled into a Servlet instance,
Each Servlet has only one instance in the container; the variable declared in the JSP is a member variable, and the member variable is initialized only when the instance is created.
The value of the variable is persisted until the instance is destroyed.
JSP script:
In general, all executable Java code can be embedded in an HTML page through a JSP script.
For example, output a list
<ul> <% for (int i = 0; i < 3; i++) { %> <li><%=i%></li> <% } %></ul>
We can look at the compiled Java code:
for (int i = 0; i < 3; i++) { out.write ("\ n"); Out.write (" <li>"); Out.print (i); Out.write ("</li>\n"); Out.write (" ");}
The above code fragment is located in the Servlet's _jspservice method.
That is, any Java code that we write in the JSP script tag will appear as-is in the Servlet's _jspservice method.
Also, because the Java code within the JSP scripting Syntax (<%.%>) tag will appear inside the method, it means that we cannot use keywords such as public, and if I want to declare it, use the declarative syntax.
Because JSP scripts can place any executable statement, you can take advantage of the Java language's capabilities, such as connecting to a database.
4 basic syntax for JSP