JSP pages <%!%> and <%%> and <%=%>

Source: Internet
Author: User
Tags define local

  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 <%=%>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.