An example of JSP three important built-in object life cycle

Source: Internet
Author: User

The three main built-in objects of JSP here are: Request,session and application. They shared the method: Setattribute,getattribute, the method name and method function are all the same, but the scope of action is different, which is related to the scope of the 3 objects. Use the following simple visitor count examples to illustrate:

<body>
<%if (Application.getattribute ("Count") ==null)
{
Application.setattribute ("Count", 1);
}
else{
String Counti=application.getattribute ("Count"). ToString ();
int Counter=integer.parseint (counti);

counter++;

Application.setattribute ("Count", integer.tostring (counter));
}

%>

You are the <%=application.getattribute ("Count")%>-bit visitor.
</body>

The SetAttribute and GetAttribute methods of the Request,session and application objects are used to represent the above example.

Experimental results and Summary:

1, Application object: As long as the server does not shut down, the count will not be zeroed, using different client (browser) Access will accumulate count.

2. Session object: Access is counted, but the access behavior of different clients is not correlated, and the number of times after closing the client (browser) is zeroed, (each session is independent).

3. Request object: Access is not counted. (The request is separate from the request, so each new request--the number of times will be cleared after the end of the response 0).

An example of JSP three important built-in object life cycle

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.