Difference instance of application session request in Jsp

Source: Internet
Author: User

Put. jsp

[Html]
<% @ Page contentType = "text/html; charset = GBK" language = "java"
ErrorPage = "" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> application test </title>
<Meta name = "website" content = "http://www.crazyit.org"/>
</Head>
<Body>
<! -- JSP declaration -->
<%! Int I; %>
<! -- Add the I value to the application variable -->
<%
Application. setAttribute ("counter", String. valueOf (++ I ));
%>
<! -- Output I value -->
<% = I %>
</Body>
</Html>
[Html]
 
Get. jsp
[Html]
<% @ Page contentType = "text/html; charset = GBK" language = "java"
ErrorPage = "" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> application test </title>
<Meta name = "website" content = "http://www.crazyit.org"/>
</Head>
<Body>
<! -- Directly output the application variable value -->
<% = Application. getAttribute ("counter") %>
</Body>
</Html>
Refresh put in different browser processes. jsp page, no matter in which browser process access get. the jsp page must get a consistent value, because the counter scope is under the application, and if you close the browser, the value obtained after re-opening the put or get page is the value of the last access because the application has not been destroyed. At the same time, note that if you access the put page in different browsers (regardless of whether the browser is closed again or not), the value is consistent (the last accessed "I" + 1 ), in this example, jsp is converted to servlet (and only converted once), and there is only one instance object in the backend of the server. Therefore, the "I" value accessed by everyone is the same, when the server destroys the servlet, or the server restarts to redeploy the server application, the "I" is reset to 0.

 

If the application in the Code is changed to session (a connection between the server and the browser exists only when the browser is not closed), if the browser process is not closed, access get in that browser process. on the jsp page, we get the session in that browser. setAttribute ("counter", String. valueOf (++ I); because the counter scope is under the session, different browser processes have different sessions. If the browser is closed, the session is destroyed. The value of the saved counter variable becomes null.

 

If the application in the Code is changed to request, when and where to access the get page, the result must be null. Because when the get page is accessed again, the request is different. getAttribute ("counter") must be empty.

[Java]
 

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.