Servlets.com

Home

What's New?

com.oreilly.servlet

Servlet Polls

Mailing Lists

Servlet Engines

Servlet ISPs

Servlet Tools

Documentation

Online Articles

The Soapbox

"Java Servlet
Programming,
Second Edition"

"Java Enterprise
Best Practices"

Speaking & Slides

About Jason

XQuery Affiliate

Advertising Info

The Book
Chapter 3 Examples from Java Servlet Programming, 2nd Ed

  • 3-1: A simple counter
  • 3-2: A more holistic counter
  • 3-3: A counter that reads init parameters
  • 3-4: A fully persistent counter
  • 3-7: On the hunt for primes
  • 3-10: A Guestbook using CacheHttpServlet

Examples from other chapters:

Example 3.1: A simple counter
This servlet counts and displays the number of times it has been accessed since the last server reboot.

Example 3.2: A more holistic counter
This servlet counts the times it has been accessed, the number of instances created by the server, and the total times all of them have been accessed.

Example 3.3: A counter that reads init parameters
This servlet counts and displays the number of times it has been accessed, and reads an init parameter to know what at what number to begin counting.

Example 3.4: A fully persistent counter
This servlet counts and displays the number of times it has been accessed, and saves the count to a file in its destroy() method to make the count persistent.

Example 3.7: On the hunt for primes
This servlet searches for prime numbers above one quadrillion. The algorithm it uses couldn't be simpler: it selects odd-numbered candidates and attempts to divide them by every odd integer between 3 and their square root. If none of the integers evenly divides the candidate, it is declared prime. It's disabled to let the server's CPU handle important tasks.
    • Try it (disabled for efficiency)
    • Source

Example 3.10: A Guestbook using CacheHttpServlet
This servlet shows how to take advantage of com.oreilly.servlet.CacheHttpServlet . It’s a guestbook servlet that displays user-submitted comments. The servlet stores the user comments in memory as a Vector of GuestbookEntry objects. There's another version of this servlet running off a database in Chapter 9, Database Connectivity. For now, to simulate reading from a slow database, the display loop has a half-second delay per entry. As the entry list gets longer, the rendering of the page gets slower. However, because the servlet extends CacheHttpServlet , the rendering only has to occur during the first GET request after a new comment is added. All later GET requests send the cached response.

 


Home   com.oreilly.servlet   Polls   Lists   
Engines   ISPs   Tools   Docs   Articles   Soapbox   Book

Copyright © 1999-2005 Jason Hunter
Privacy Policy

webmaster@servlets.com
Last updated: March 1, 2009