Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Using Applications

A session enables you to track one user at a time—an application enables you to track all JSPs in the same site, no matter how many users are using them. To access the current application, you can use the built-in JSP application object. Like the session object, the application object is based on the javax.servlet.http.HttpSession interface. In the previous example, you saw how to create a session attribute named counter, which stores the number of times the user has visited the page in the current session. In the same way, you can create an application attribute named applicationCounter that holds the total number of times anyone in the same application has viewed a JSP page:

Integer applicationCounter = (Integer)application.getAttribute("applicationCounter");
if (applicationCounter == null) {
  applicationCounter = new Integer(1);
} else {
  applicationCounter = new Integer(applicationCounter.intValue() + 1);
}

application.setAttribute("applicationCounter", applicationCounter);

You can see this at work in Listing 7.6, where the code supports a counter for the number of visits in the current session, and in the current application.

Listing 7.6 Using the Application Object (ch07_06.jsp)

<HTML>
  <HEAD>
    <TITLE>Using the Application Object</TITLE>
  </HEAD>

  <BODY>
    <H1>Using the Application Object</H1>
    <%
    Integer counter = (Integer)session.getAttribute("counter");
    String heading = null;
    if (counter == null) {
      counter = new Integer(1);
    } else {
      counter = new Integer(counter.intValue() + 1);
    }

    session.setAttribute("counter", counter);

    Integer applicationCounter = (Integer)application.getAttribute("applicationCounter");
    if (applicationCounter == null) {
      applicationCounter = new Integer(1);
    } else {
      applicationCounter = new Integer(applicationCounter.intValue() + 1);
    }

    application.setAttribute("applicationCounter", applicationCounter);
    %>

    You have visited this page <%=counter%> times.
    <BR>
    This page has been visited by all users <%=applicationCounter%> times.
  </BODY>
</HTML>

In this example, the session counter keeps track of the number of times a user has visited the page, and the application counter keeps track of the number of times all users have accessed the page. This isn't particularly easy to see on the same computer, because Tomcat will count you as the same user. However, you can get around that if you happen to have different browsers. For example, you can open it in Internet Explorer and reload a few times, as you see in Figure 7.7. If you then open it in another browser, such as Netscape Navigator, you'll see that the application counter is indeed keeping track of users across sessions, as shown in Figure 7.8.

Figure 7.7Figure 7.7 Using the application object.

Figure 7.8Figure 7.8 Another user using the application object.

  • + Share This
  • 🔖 Save To Your Account