FileDocCategorySizeDatePackage
BuzzInServlet.javaAPI DocExample6157Thu Mar 25 21:30:22 GMT 2004buzzin

BuzzInServlet

public class BuzzInServlet extends HttpServlet
A quiz-show "buzzer" servlet: the first respondant wins the chance to answer the skill-testing question.

Previous versions of this code used shared static variables, but this is not reliable, since most web engines now use custom class loaders that may load a servlet class more than once. The "right" way is to synchronize on an object stored in the Servlet Application Context.

Fields Summary
protected static final String
WINNER
The attribute name used throughout.
Constructors Summary
Methods Summary
public voiddoGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
doGet is called from the contestants web page. Uses a synchronized code block to ensure that only one contestant can change the state of "buzzed".


	                         	 
	     
	  
	
		ServletContext application = getServletContext();

		boolean iWon = false;
		String user = request.getRemoteHost() + '@" + request.getRemoteAddr();

		// Do the synchronized stuff first, and all in one place.
		synchronized(application) {
			if (application.getAttribute(WINNER) == null) {
				application.setAttribute(WINNER, user);
				application.log("BuzzInServlet: WINNER " + user);
				iWon = true;
			}
	 	}

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();

		out.println("<html><head><title>Thanks for playing</title></head>");
		out.println("<body bgcolor=\"white\">");

		if (iWon) {
			out.println("<b>YOU GOT IT</b>");
			// TODO - output HTML to play a sound file :-)
		} else {
				out.println("Thanks for playing, " + request.getRemoteAddr());
				out.println(", but " + application.getAttribute(WINNER) + 
					" buzzed in first");
		}
		out.println("</body></html>");
	
public voiddoPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
The Post method is used from an Administrator page (which should only be installed in the instructor/host's localweb directory). Post is used for administrative functions: 1) to display the winner; 2) to reset the buzzer for the next question.

		ServletContext application = getServletContext();

		response.setContentType("text/html");
		HttpSession session = request.getSession();

		PrintWriter out = response.getWriter();

		if (request.isUserInRole("host")) {
			out.println("<html><head><title>Welcome back, " +
				request.getUserPrincipal().getName() + "</title><head>");
			out.println("<body bgcolor=\"white\">");
			String command = request.getParameter("command");
			if (command.equals("reset")) {

				// Synchronize what you need, no more, no less.
				synchronized(application) {
					application.setAttribute(WINNER, null);
				}
				session.setAttribute("buzzin.message", "RESET");
			} else if (command.equals("show")) {
				String winner = null;
				synchronized(application) {
					winner = (String)application.getAttribute(WINNER);
				}
				if (winner == null) {
					session.setAttribute("buzzin.message",
						"<b>No winner yet!</b>");
				} else {
					session.setAttribute("buzzin.message",
						"<b>Winner is: </b>" + winner);
				}
			}
			else {
				session.setAttribute("buzzin.message",
					"ERROR: Command " + command + " invalid.");
			}
			RequestDispatcher rd = application.getRequestDispatcher(
				"/hosts/index.jsp");
			rd.forward(request, response);
		} else {
			out.println("<html><head><title>Nice try, but... </title><head>");
			out.println("<body bgcolor=\"white\">");
			out.println(
				"I'm sorry, Dave, but you know I can't allow you to do that.");
			out.println("Even if you are " + request.getUserPrincipal());
		}
		out.println("</body></html>");