How do I get the "Posting and Processing HTML Forms" example at the Java Developer Connection to work under Servlet API version 2.2 (and Tomcat 3.1)?

Avi Kak

The Java Developer Connection tutorial on servlets contains a very instructive example that shows how servlet can be used for processing HTML forms. This example was written for JSDK 2.1.

To get this example to work with Tomcat 3.1, which uses the Servlets 2.2 API, you need to make the following changes:

The most significant change you have to make concerns the use of DataInputStream.readLine() method. The readLine() method of the DataInputStream class has been deprecated because, as mentioned in the Java 2 SDK API, it "does not properly convert bytes to characters." Java 2 SDK API further states that "as of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method." Therefore, in the servlet class files FormDisplayServlet.java and FormProcessingServlet.java, replace the invocation

      DataInputStream disStructure = new DataInputStream( fisStructure );


      BufferedReader disStructure = new BufferedReader( new InputStreamReader( fisStructure ));

and, in the servlet class file FormDisplayServlet.java, replace the invocation

      DataInputStream disData = new DataInputStream( fisData );


      BufferedReader disData = new BufferedReader( new InputStreamReader( fisData ) ); 

Changing over to BufferedReader for reading the structure and the data files also makes it necessary to change the test condition in the while loop for reading the files. For illustration, the test condition in the while loop for reading the structure file struct.dat needs to be changed from

      while ( 0 != disStructure.available() ) {


      while ( ( inputLine = disStructure.readLine() ) != null ) {

since the method available() is not defined for BufferedReader.

The other changes you need to make depend on where you store the the files for this example in the Tomcat 3.1 directory structure. In my case, in the TOMCAT_HOME/webapps directory, I created a new webapp for this example and called it form-processing. I deposited the servlet files in the


directory, and I deposited the files struct.dat and data.dat in the directory


This placement of the files dictated the following additional changes to the example code:
  1. In the FormDisplayServlet.java file, change the statement
        fisData = new FileInputStream( "c:JavaWebServer1.1servlets" + paramDataFile );
        fisData = new FileInputStream( "C:/tomcat/webapps/form-processing/servlets/" + paramDataFile );
  2. In the FormDisplayServlet.java file, change the statement
        out.println( "<form method=POST ACTION="http://localhost:8080/servlet/FormProcessingServlet">" );  
        out.println( "<form method=POST ACTION="http://localhost:8080/form-processing/servlet/FormProcessingServlet">" );  
  3. In the FormProcessingServlet.java file, change the statement
       isStructure = context.getResourceAsStream( paramStructureFile[0]);
       isStructure = context.getResourceAsStream( "/servlets/" + paramStructureFile[0]);
0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact