O'Reilly logo

Head First Servlets and JSP, 2nd Edition by Bert Bates, Bryan Basham, Kathy Sierra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Making a JSP that displays how many times it’s been accessed

Pauline wants to use JSPs in her web apps—she’s really sick of writing HTML into a servlet’s PrintWriter println().

She decides to learn JSPs by making a simple dynamic page that prints the number of times the page has been requested. She understands that you can put regular old Java code in a JSP using a scriptlet—which just means Java code within a <% ... %> tag.

image with no caption

BasicCounter.jsp

image with no caption

Counter.java

package foo;

public class Counter {
   private static int count;
   public static synchronized int getCount() {
      count++;
      return count;
   }
}

Note

Plain old Java helper class.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required