Fundamentals of JavaTM Servlets: Magercises
Help: Posting and Processing HTML Forms

By MageLang Institute

[Help | Solution | API Docs | Expected Output | Course Notes | Magercises | Module Intro]

Help is available for each task, or you can go straight to the solution source code.

Task 1

Change the skeleton code for FormProcessingServlet to accept data sent from the client to the server. The client sends an HTTP POST request with a complete set of data from the HTML form.

The client sends information to the client via the HTTP POST method. To process this information you need to override the doPost() method. In this method, you will need to open the InputStream sent from the client. All information sent by a POST is read from this source.

The InputStream contains a value for the parameters structurefile and datafile. You must open the file referenced by structurefile and read its contents to know how to parse the input stream sent by the client.

The heart of the content, the information from the editable form, also needs to be read from the InputStream. The class HttpUtils contains useful methods for parsing information coming from a client.

Task 2

Write code in FormProcessingServlet to echo the accepted data back to the client in the form of an HTML table. This makes it appear to the user of the browser that they are seeing a non-editable version of the updated information

This part of the code has been completed for you. If you use the proper variable names earlier in the code you can use this code unmodified.

Task 3

Finally, as an advanced part of this Magercise, write the code to store the updated data to the disk. This is not a trivial exercise as you have to think about the problem of multiple people updating data at the same time.

The problem of coordinating updates to shared information by multiple clients is solved by most database systems. The problem with HTML based clients is that the HTTP protocol does not maintain a connection between clients and server over multiple HTTP method calls. In other words, the HTTP protocol does not maintain state. There are many ways to solve this, including using hidden fields in the HTML form to store date/time values.

This part of the Magercise is left unsolved because there really is no one right way to solve this problem. You need to think about the system you are building (frequency of updates, ratio of reads to updates, etc.) before you decide on a strategy to address this problem.

Task 4

Request the first form to be displayed by sending the URL http://localhost:8080/servlet/FormDisplayServlet?
structurefile=struct.dat&datafile=data.dat

Copyright © 1998 MageLang Institute. All Rights Reserved.