JSP basic Syntax 1, JSP template elements
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.
<%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
2. JSP expression
JSP script expressions (expression) are used to output program data to the client
syntax:<%= variable or expression%>
Example: Output current system time: <%= new java.util.Date () %>
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.
3. JSP Script Fragment
A JSP script fragment 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
<% intsum=0;//declaring Variables/*Writing Statements*/ for (intI=1; I<= -; I++) {sum+=i; } out.println (""+sum+"");%>
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:
<% intx= Ten; OUT.PRINTLN (x);%><P>This is the JSP page text</P><% inty= -; Out.println (y);%>
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:
<% for (intI=1; I<5; I++) {%> <H1>http://localhost:8080/JavaWeb_Jsp_Study_20140603/</H1><% }%>
4. 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:
<%!static {System.out.println ("Loading servlet!"); }Private intGlobalvar= 0; Publicvoid Jspinit () {System.out.println ("Initializing jsp!");}%><%! Publicvoid Jspdestroy () {System.out.println ("destroying jsp!");}%>
5. JSP annotations
In JSPs, there are two main categories of annotations: explicit comments and implicit annotations
The differences between the three types of annotations:
<!--show comments, This note can see --> <% // java /* multiline comment in Java */ %> <% -- jsp own comment -- %>
JSP Learning (i) JSP basic syntax