/** * Responds to HTTP GET requests. * * @param request the HTTPServletRequest object * @param response the HttpServletResponse object * * @throws ServletException * @throws IOException */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //The query string sent by the client tells which button was clicked // on the Web page. String query = request.getQueryString(); // Read the current state of the LEDs (0=on, 1=off). // and toggle the LED named in the query string. boolean led1On; if ("button1".equals(query)) { System.out.println("Button 1 was clicked"); // Toggle the LED. led1On = toggle(led1); } else { // Don't toggle the LED. // Read the LED's state and set led1On true if the state is 0 // (on) and false if the state is 1 (off). led1On = (led1.readLatch() == 0); } boolean led2On; if ("button2".equals(query)) { System.out.println("Button 2 was clicked"); // Toggle the LED. // A logic low turns the LED on. led2On = toggle(led2); } else { // Don't toggle the LED. // Read the LED's state and set led2On true if the state is 0 // (on) and false if the state is 1 (off). led2On = (led2.readLatch() == 0); } // Set the images and text to match the current LEDs' states // If led1State (led2State) is true, LED1 (LED2) is off. // If led2State (led2State) is false, LED1 (LED2) is on. String led1Image; String led1State; if (led1On) { led1Image= "/ledon.gif"; led1State = "on"; } else { led1Image = "/ledoff.gif"; led1State = "off"; } String led2Image; String led2State; if (led2On) { led2Image= "/ledon.gif"; led2State = "on"; } else { led2Image = "/ledoff.gif"; led2State = "off"; } // Return the Web page to the client. SendWebPage (response, led1Image, led2Image, led1State, led2State); } //end doGet Listing 2. The servlet’s doGet() method services HTTP requests for the Web page.