& Lt; %! % & Gt; and & lt; % =%& gt;, % in jsp page

Source: Internet
Author: User

<%! %> 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:

 


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.