First, we want to understand how JSP works. The essence of JSP is that a servlet,jsp is translated into a. java file before being run by the Tomcat server and then compiling the. Java text
is a. class file, and when we access the JSP, the translated class is the one that handles the request.
1.<%%> is called a script fragment, where the written content is translated into the servlet's service method, obviously we can define local variables in the service method or call other methods, but cannot
Other methods are defined in the service, that is, we can define local variables or Invoke methods in <%%>, but we cannot define methods. There can be multiple script fragments on a JSP page, but many
To ensure a complete structure between the script fragments.
2.<%!%> is called a declaration in which content written in the future will be translated directly into the servlet class, because we can define methods and properties and global variables in the class, so we can make a sound in <%!%>
Methods, properties, global variables.
3.<%=%> , called a JSP expression, is used to output a declared variable or expression to a Web page.
4. the code written directly on the JSP page <body></body> is called a template element and will in the future be used as output in Out.write ("___") in the Servlet's service method.
Below, we give two examples:
Example 1:
1 <Body>2 <% for (intI=0; I<3; I++) {%>3 Out.print (i*2);4 <%} %>5 </Body>
Page content:
Example 2:
1 <Body>2 <%! intsum=1; %>3 <% intsum=8; %>4 <H3>----<%out.print (Sum++); %></H3>5 <H3>----<%=This.sum%></H3>6 </Body>
Page content:
JSP pages <%!%> and <%%> and <%=%>