JSP Servlets   «Prev  Next»

Reading Servlet Response - Exercise

An Applet and Servlet that work together

Objective: Build an applet and servlet that process a form.

Exercise scoring

This exercise is worth 10 points. To receive credit, paste your completed code in the text box provided below, and click the Submit button to send your code.

Background/overview

It is now time for you to put the code in this lesson into action.
You are going to write an applet and a servlet that work together.

Download files

You need to use the following files from the Exercise download: getter.html (the HTML that loads the applet), Getter-skeleton.java (the result of the exercise earlier in this module).

Instructions

  1. First, copy Getter-skeleton.java to getter.java and add code to build the applet. Remember, it should:
    1. React to the button click: implement ActionListener, add yourself as a listener to the button, add code for actionPerformed()
    2. Get the text from the input field using input.getText()
    3. Build the URL for the GET request, including the parameter
    4. Send the GET to the servlet by opening the URL connection
    5. Build a BufferedReader from the connection and read the response
    6. Change the label using output.setText()
  2. Compile your applet and fix any errors. Load it into a browser using the HTML provided and make sure you see the user interface you expect.
  3. Next, write the servlet, naming it Greeter.java. It only needs a doGet() method, and it returns the same greeting we used in earlier versions of the servlet. If a name, such as Bill, is provided, it’s used in the greeting:
    Hello, Bill

    If the name is completely blank, the greeting is:
    Hello, whoever you are

    This servlet sends only the string – no HTML.
  4. Compile your servlet.
  5. Copy the applet’s class file; Getter.class; and the HTML, getter.html, to the directory where the Java Web Server expects to find Web pages. On my system, that’s C:\JWS\public_html.
  6. Copy the servlet’s class file, Greeter.class, to the directory where the Java Web Server expects to find servlets. On my system, that’s C:\JWS\servlets.
  7. Start the Java Web Server. Launch a browser, and enter this URL:
    http://localhost:8080/getter.html
    
  8. Test your applet by entering a name in the input field and clicking Submit. Do you see the greeting you expect? Clear the name and click Submit again. Do you see the “Whoever you are” greeting?

Hints

  1. Remember to add implements ActionListener to the line that defines your Getter applet class.
  2. Remember to add a line at the end of init() that registers your applet as listening to the button:
    button.addActionListener(this);
    
  3. Remember that the actionPerformed() method looks like this:

public void actionPerformed(ActionEvent event){
//deal with the event
}
  1. Remember that the code in actionPerformed() to send the GET looks like this:
  2. URL url = new URL(getCodeBase(),"/servlet/Greeter?name=" + name);
    URLConnection conn = url.openConnection();
    conn.setUseCaches(false);
    

    (You will need the name variable set before this point.)
  3. To read the servlet response:
InputStream is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));String reply = reader.readLine();    is.close();

Paste your code into the text area below once it's all working smoothly.