The application Object counts the number of times that all users access a webpage.

Source: Internet
Author: User

Because the application object is used to complete the accumulative function, when

(1) Redeploy the current Wen Application
(2) restart the Tomcat server

The counter starts counting again.

The jsp code is as follows:
Copy codeThe Code is as follows:
<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> Insert title here </title>
</Head>
<Body>
<%
// Check whether the parameter count is saved in the application object.
// If NO, add a parameter named count in the application object.
If (application. getAttribute ("count") = null ){
Application. setAttribute ("count", new Integer (0 ));
}
Integer count = (Integer) application. getAttribute ("count ");
// Use the application object to read the value of the count parameter, and then accumulate 1 based on the original value
Application. setAttribute ("count", new Integer (count. intValue () + 1 ));
%>
<H2>
<! -- Output the value corresponding to the accumulated count parameter -->
Welcome to visit. This page has been accessed <font color = "# ff0000"> <% = application. getAttribute ("count") %> </font> times ....
</H2>
</Body>
</Html>

Enter http: // localhost: 8888/WebDemo/count. jsp in the browser.
 
Include the count. jsp file in the jsp file to be counted.
Copy codeThe Code is as follows:
<% @ Include file = "count. jsp" %>

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.