JSP page <%!%> And <%> and <% =%> Comments

First, we must understand the operating principle jsp. JSP is the essence of a Servlet, Tomcat server will first be translated into JSP .java files before running, and then compile the .java text

As .class files, and when we visit jsp, the translation process is the class of the request.

  1. <%%>
Called a script fragment, which will be written by content translation method in the Service Servlet, it is clear we can define local variables in the Service or method call other methods, but can not

Redefining the Service in other ways, that is, we can define local variables in <%%> or call a method, but it can not be defined method. Jsp pages can have multiple script fragments, but more than

Between script fragments to ensure structural integrity.


 
 2. <%!%> Called the statement, which writes the contents of the future will have a direct translation in the Servlet class, because we can define class methods and properties, and global variables, so we can in <%!%> Acoustic

Ming methods, properties, global variables.

  3. <% =%> Referred jsp expression, which has been declared for the variable or expression is output to the top page.


  4. written directly jsp pages <body> </ body> code called template elements, the method will in the future Servlet Service in out.write ( "___"), as the output.


  
Below, we give two examples:


Example 1:
 

1 <body>
2     <% for (int i=0;i<3;i++) {%>
3     out.print(i*2);
4     <%} %>
5 </body>
 

    Page Content:

 Example 2:

1 <body>
2     <%! int sum=1; %>
3     <% int sum=8; %>
4     <h3>----<% out.print(sum++); %></h3>
5     <h3>----<%= this.sum %></h3>
6 </body>

    Page Content:

Guess you like

Origin www.cnblogs.com/mark5/p/11596435.html