Definition fields for variables in a JSP

Source: Internet
Author: User
Tags definition variables
js| variables in <% ... the variables defined by the%> are local to the JSP, and they are not visible to external functions, even if they are declared on the same page. Such as:

<%
int evilvariable = "666";
%>
...
function TestFunction () {
Inside the function, the variable evilvariable is not visible
}
Why, then? This is because variable evilvariable eventually becomes a local variable of the service () method in the result servlet, so it cannot be accessed by other methods of the result servlet.

For the <%! The variables declared in%> will be global variables. Such as:

<%!
int evilvariable = "666";
%>
...
function TestFunction () {
int x = evilvariable; Ability to use variable evilvariable
}
This is because the evilvariable variable defined in this way becomes a private member variable of the result servlet, so it can be accessed by all the methods in the servlet.

Conclusion

It is important to understand the above concepts, because in a servlet environment, only one result servlet instance is allowed to run and respond to all page requests for a JSP page. Therefore, all the member variables in the result servlet can be shared by all requests, and local variables in the service () method can be rebuilt only once per response request. So when we put the variables in the <SERVER></SERVER> tag, we should pay attention to the above rules.

Related Article

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.