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)?
Created May 4, 2012
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 );by
BufferedReader disStructure = new BufferedReader( new InputStreamReader( fisStructure ));and, in the servlet class file FormDisplayServlet.java, replace the invocation
DataInputStream disData = new DataInputStream( fisData );by
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() ) { //.... }to
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
TOMCAT_HOME/webapps/form-processing/WEB-INF/classesdirectory, and I deposited the files struct.dat and data.dat in the directory
TOMCAT_HOME/webapps/form-processing/servlets/This placement of the files dictated the following additional changes to the example code:
-
In the FormDisplayServlet.java file, change the statement
fisData = new FileInputStream( "c:JavaWebServer1.1servlets" + paramDataFile );
tofisData = new FileInputStream( "C:/tomcat/webapps/form-processing/servlets/" + paramDataFile );
-
In the FormDisplayServlet.java file, change the statement
out.println( "<form method=POST ACTION="http://localhost:8080/servlet/FormProcessingServlet">" );
toout.println( "<form method=POST ACTION="http://localhost:8080/form-processing/servlet/FormProcessingServlet">" );
-
In the FormProcessingServlet.java file, change the statement
isStructure = context.getResourceAsStream( paramStructureFile[0]);
toisStructure = context.getResourceAsStream( "/servlets/" + paramStructureFile[0]);