<%! %> And <% = %>, % in jsp page
First, we need to understand the operating principles of jsp. JSP is essentially a Servlet. Before Running JSP, it is first translated into a. java file by the Tomcat server, and then compiled in. java text.
Is A. class file. When we access jsp, the translated class is used to process the request.
1. <%>It is called a script fragment, and the content written in it will be translated into the Servlet Service method. Obviously, we can define local variables in the Service method or call other methods, but it cannot
Define other methods in the Service, that is, we can define local variables or call methods in <%>, but not methods. The jsp page can contain multiple script fragments, but multiple
Ensure the structure is complete between script fragments.
2. <%! %>The content written in the Declaration will be directly translated into the Servlet class in the future. Because we can define methods, attributes, and global variables in the class, we can <%! %> Moderate voice
Methods, attributes, and global variables.
3. <% = %>A jsp expression is used to output declared variables or expressions to a webpage.
4.The code written directly on the jsp page <body> </body> is called a template element. In the future, the out. write ("___") in the Servlet Service method will be used as the output content.
Here are 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
Page content: