Listing 7 - Listing 7.1 ApplesAndOranges.java package...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Listing 7.1 ApplesAndOranges.java package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Servlet that creates Excel spreadsheet comparing * apples and oranges. */ public class ApplesAndOranges extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/vnd.ms-excel"); PrintWriter out = response.getWriter(); out.println("\tQ1\tQ2\tQ3\tQ4\tTotal"); out.println("Apples\t78\t87\t92\t29\t=SUM(B2:E2)"); out.println("Oranges\t77\t86\t93\t30\t=SUM(B3:E3)"); } } Listing 7.2 PrimeNumberServlet.java package coreservlets; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*; /** Servlet that processes a request to generate n * prime numbers, each with at least m digits. * It performs the calculations in a low-priority background * thread, returning only the results it has found so far. * If these results are not complete, it sends a Refresh * header instructing the browser to ask for new results a * little while later. It also maintains a list of a * small number of previously calculated prime lists * to return immediately to anyone who supplies the * same n and m as a recently completed computation. */
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
public class PrimeNumberServlet extends HttpServlet { private ArrayList primeListCollection = new ArrayList(); private int maxPrimeLists = 30; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int numPrimes = ServletUtilities.getIntParameter(request, "numPrimes", 50); int numDigits = ServletUtilities.getIntParameter(request, "numDigits", 120); PrimeList primeList = findPrimeList(primeListCollection, numPrimes, numDigits); if (primeList == null) { primeList = new PrimeList(numPrimes, numDigits, true); // Multiple servlet request threads share the instance // variables (fields) of PrimeNumbers. So // synchronize all access to servlet fields. synchronized(primeListCollection) { if (primeListCollection.size() >= maxPrimeLists) primeListCollection.remove(0); primeListCollection.add(primeList); } } ArrayList currentPrimes = primeList.getPrimes(); int numCurrentPrimes = currentPrimes.size(); int numPrimesRemaining = (numPrimes - numCurrentPrimes); boolean isLastResult = (numPrimesRemaining == 0); if (!isLastResult) { response.setIntHeader("Refresh", 5); } response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "Some " + numDigits + "-Digit Prime Numbers"; out.println(ServletUtilities.headWithTitle(title) + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<H2 ALIGN=CENTER>" + title + "</H2>\n" + "<H3>Primes found with " + numDigits + " or more digits: " + numCurrentPrimes + ".</H3>");
Background image of page 2
if (isLastResult) out.println("<B>Done searching.</B>"); else out.println("<B>Still looking for " + numPrimesRemaining + " more<BLINK>. ..</BLINK></B>"); out.println("<OL>"); for(int i=0; i<numCurrentPrimes; i++) {
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 14

Listing 7 - Listing 7.1 ApplesAndOranges.java package...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online