JSP: JSP declaration and JSP script <%! int count=0;%> with <% int count=0;%>

Source: Internet
Author: User

  <body> <%!int Count; %>  <%int count2=0%> Output declaration count:<%=count++%> <br> Output script count2:<%=count2++%> <br >  </body>
Each time the JSP page is refreshed the corresponding output results
Count incremented by 1 per refresh
Count2 Every time the refresh is not changed

Analysis:
Because count is a variable declared in a JSP, when the JSP is compiled into a servlet, cout is a member variable of the Servlet object, and the Servlet object is destroyed by invoking the Destroy () method based on the servlet lifecycle when the Web is closed. So each refresh will increase by 1;
and count2 the variable declared in the JSP script is equivalent to the JSP compiled into a servlet, will be compiled into the Servlet-jspservice method executable code is the method's local variables, meaning that the life cycle of the variable is only before and after the method call, so after each refresh The Count2 value is reinitialized to 0;

  

<body> <%!int Count; %> <%int count2=0; %> output declaration when count:<%=count++%> <br> output script count:<%=count2++%> <br> </body>

JSP: JSP declaration and JSP script <%! int count=0;%> with <% int count=0;%>

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.