Any language has its own syntax, in Java, JSP, although it is a Java application, but still has its own expanded syntax, and in the JSP, all the Java statements can be used.
First, the JSP template element
The HTML content in a JSP page is called a JSP template element.
The JSP template element defines the basic skeleton of a Web page, which defines the structure and appearance of the page.
Second, the JSP expression
JSP script expressions (expression) are used to output program data to the client
Syntax:<%= variable or expression%>
Example: Output current system time:
When the JSP engine translates the script expression, it turns the program data into a string and then loses the data to the client at the appropriate location with Out.print (...).
You cannot have a semicolon (;)after a variable or expression in a JSP script expression.
Third, JSP script fragment
The JSP script fragment (scriptlet) is used to write multiple lines of Java code in a JSP page. Grammar:
<%
Multi-line Java code
%>
You can define variables, write statements, and not define methods in <%%>.
Example: Defining variables in scriptlet, writing statements
1 <%2 int sum=0;//declaration Variable 3 4 /* Write statement */5 for (int i=1;i<=100;i++) {6 sum+=i;7 }8 Out.println ("
Precautions:
- The JSP script fragment can only appear Java code, cannot appear other template elements, JSP engine in the translation JSP page, will be in the JSP script fragment Java code will be placed in the servlet _jspservice method.
- The Java code in the JSP script fragment must strictly follow the Java syntax, for example, each execution statement must end with a semicolon (;).
- You can have multiple script fragments in a JSP page that can embed text, HTML tags, and other JSP elements between two or more script fragments.
Example:
1 <%2 int x = 10;3 out.println (x); 4%>5 <p> This is the JSP page text </p>6 <%7 int y = 20;8 Out.println (y); 9%>
The code in multiple script fragments can be accessed from one another as if all the code were placed in a pair of <%%>. such as: Out.println (x);
The Java statements in a single script fragment can be incomplete, but the result of a combination of multiple script fragments must be a complete Java statement, for example:
1 <%2 for (int i=1; i<5; i++) 3 {4%>5
Iv. JSP Declaration all code written in the JSP page is translated by default into the Servlet's service method, and the Java code in the JSP declaration is translated outside the _jspservice method. Syntax:
<%!
Java code
%>
Therefore,JSP declarations can be used to define the static code blocks, member variables, and methods of the servlet program that the JSP page transforms into .
Multiple static code blocks, variables, and functions can be defined in a JSP declaration or individually in multiple JSP declarations.
JSP implicit objects are scoped only to the _jspservice method of the servlet, so these implicit objects cannot be used in JSP declarations.
JSP declaration case:
1 <%! 2 Static { 3 System.out.println ("Loading servlet!"); 4} 5 6 private int globalvar = 0; 7 8 public void Jspinit () {9 System.out.println ("Initializing jsp!"); }11%>12 <%!14 public void Jspdestroy () {System.out.println ("Destroying jsp!"); }17%>
V. JSP annotationsIn JSPs, there are two main categories of annotations:
Explicit comments: Use HTML - style annotations directly:<!-- Comment content
Implicit comments: Comments using JAVA directly://,/*... */
JSP Own comments:<%-- Comment content --%>
The difference between the three types of annotations
1 <!--This note can be seen in 2 3 <% 4 //java single line comment 5 6/ * 7 Multi-line comments in Java 8 */ 9%>10 < %--jsp own Comments--%>
HTML annotations can be seen when viewing source files in a browser, while Java comments and JSP annotations do not see the contents of the comments in the browser when viewing the source file, which is the difference between the three types of annotations.
Javaweb Learning Summary (15)--jsp Basic Grammar (RPM)