import java.io.PrintWriter; import java.io.IOException; import javax.servlet.http.*; public class GuessGameServlet extends HttpServlet { private GuessGameBean theBean; public void init() { theBean = new GuessGameBean(); } public void doGet( HttpServletRequest req, HttpServletResponse resp ) throws javax.servlet.ServletException, IOException { theBean.setGuess( req.getParameter( "theInput" ) ); PrintWriter out = resp.getWriter(); out.print( "GuessGame Servlet" ); out.print( "
" ); out.print( "The minimum is " ); out.print( theBean.getMin() ); out.print( "

" ); out.print( "Enter a guess: " ); out.print( "" ); out.print( "

" ); out.print( "The maximum is " ); out.print( theBean.getMax() ); out.print( "

" ); out.print( theBean.getResults() ); out.print( "

" ); out.close(); } public void doPost( HttpServletRequest req, HttpServletResponse res ) throws javax.servlet.ServletException, IOException { doGet( req, res ); } } public class GuessGameBean { private int theNumber, min, max; private void init() { min = 1; max = 100; theNumber = new java.util.Random().nextInt( 100 ) + 1; } public GuessGameBean() { init(); } public int getMin() { return min; } public int getMax() { return max; } public void setGuess( String inString ) { System.out.println( "setGuess: min.ans.max-in " + min + "." + theNumber + "." + max + "-" + inString ); if (inString == null) return; int input = Integer.parseInt( inString ); if (input < theNumber) { if (input > min) min = input; } else if (input > theNumber) { if (input < max) max = input; } else min = max = input; } public String getResults() { if (min == max) { init(); return "Sirens!! Confetti!! Fireworks!!

Go again"; } else return ""; } }