[Reserved] Java Web Learning (xv) ---- JSP basic grammar

Reprinted from: https://www.cnblogs.com/xdp-gacl/p/3776512.html

Any language has its own grammar, there JAVA, JSP, although an application in JAVA, but still has its own expansion of grammar, but in JSP, JAVA all the statements can be used.

A, JSP template elements

  HTML content in a JSP page called JSP template elements. 
  The JSP template element defines the basic skeleton of the page, i.e. define the structure and appearance of the page.

Two, JSP expressions

  JSP script expression (expression) for outputting the data to the client program
      syntax: <% = variable or expression%>
      Example: Output current system time:

1 <%= new java.util.Date() %> 

  JSP engine in the translation script expression, the program will turn the data into a string, and then use out.print in the corresponding position (...) the data lost to the client.
  JSP script tag or the expression expressions can not have a semicolon (;) .

Three, JSP script fragment

  JSP script fragment (scriptlet) used to write multiple lines of Java code in JSP pages. Syntax:
    <% 
            plurality of lines of code java 
    %>

  Variables can be defined in <%>, write statements, the method can not be defined.

Example: In Scriptlet define variables, write statements

Copy the code
. 1 <% 
2 int SUM = 0; // declare variables 
. 3 
4 / * write statements * / 
. 5 for (int I =. 1; I <= 100; I ++) { 
. 6 + SUM = I; 
. 7} 
. 8 Out.println ( " <h1 of> = the Sum "+ SUM +" </ h1 of> "); 
. 9%>
Copy the code

  Precautions:

  • JSP script java code snippet can appear only other template elements can not appear, JSP engine in translation JSP page, JSP script will fragment the Java code will be placed intact _jspService method of Servlet.
  • JSP script snippet of Java code must strictly follow the Java syntax, for example, each statement must be executed later by a semicolon (;) end .
  • In a JSP page can have multiple script fragment, between two or more pieces of scripts can be embedded text, HTML tags and other JSP elements.

    For example:

Copy the code
. 1 <% 
2 X = int 10; 
. 3 Out.println (X); 
. 4%> 
. 5 <P> This is a JSP page text </ P> 
. 6 <% 
. 7 int Y = 20 is; 
. 8 Out.println (Y); 
9%>
Copy the code

  Multiple scripts snippet of code can access each other, as if all the code in the case of one pair of <%%> among. Such as: out.println (x);
  a single script snippet of Java statements may not be complete, however, the result of a combination of multiple scripts pieces must be complete Java statements, such as:

Copy the code
1 <%
2     for (int i=1; i<5; i++) 
3     {
4 %>
5     <H1>http://localhost:8080/JavaWeb_Jsp_Study_20140603/</H1>
6 <%
7     }
8 %>
Copy the code

Four, JSP statement

  All of the code written in JSP pages, the default will translate to the servlet's service method, but Jsp statement java code is translated to the outside _jspService methods. Syntax:
    <%! 
        java code
    %>
  Therefore, JSP statement can be used to define the static code block is converted into Servlet JSP page program, member variables and methods  . 
  A plurality of static code blocks, variables and functions can be defined in a JSP statement may be defined individually in the plurality of JSP statement.
  JSP implicit objects scope is limited to the Servlet _jspService method can not be used in these implicit objects JSP statement.

  JSP declarations Case:

Copy the code
 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!");
10 }
11 %>
12 
13 <%!
14 public void jspDestroy(){
15     System.out.println("destroying jsp!");
16 }
17 %>
Copy the code

Five, JSP comments

In JSP, there are two types of comments:

   Explicit Note: The direct use of HTML style comments: <- -!  Footnotes - ->

   Implicit Note: The direct use of JAVA comment: // , / * ...... * /

 JSP own comments: <% - -  Notes content - -%>

The difference between these three comments

Copy the code
1 <! - this annotation can be seen -> 
 2 
 . 3 <% 
 . 4 in the JAVA // single-line comment 
 . 5 
 . 6 / * 
 . 7 the JAVA multi-line comments 
 . 8 * / 
 . 9%> 
10 
. 11 <% - the JSP own comment -%>
Copy the code

  HTML comment view source in the browser can be seen in time, and when JAVA and JSP comments See the comments in the source file in the browser can not see the content of the comment, which is the difference between these three comments.

Guess you like

Origin www.cnblogs.com/clarino/p/11708971.html