// Copyright MageLang Institute; Version $Id: //depot/main/src/edu/modules/Servlets/magercises/FormPostingAndProcessing/solution/FormDisplayServlet.java#3 $ import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; /** * FormDisplayServlet * * This servlet will dynamically create a data editing form. * Two paramaters are required: * * structurefile: The name of a file that contains data structure information * * datafile: The name of a file that dontains the data to be displayed. * * Because this servlet is used in a Magercise, we will send error messages * to the browser. Normally this is not a good idea as the browser user should * know nothing about the workings of a servlet. * * The format of the structure file is: * |||| * * name: Name of the column of data * datatype: Type of the data in the column (use only text for now) * length: Maximum number of characters in this column of data * * The format for the data file is: * |||... * * Each piece of data is stored in a file as a string. For this reason * it is best to deal with text fields. A real version of this would * work against a database, reading the database structure from the * JDBC metadata. It would also handle datatype conversions from the * actual data type to text (which is required for display on the HTML form) */ public class FormDisplayServlet extends HttpServlet { private static final int PIXELS_PER_CHAR = 10; public void doGet ( HttpServletRequest req, HttpServletResponse res ) throws ServletException, IOException { Vector columns = new Vector( 20 ); ColumnInfo colInfo; String paramStructureFile; String paramDataFile; String inputLine = ""; int i; int l; int row; String title; FileInputStream fisStructure; DataInputStream disStructure; FileInputStream fisData; DataInputStream disData; StringTokenizer stData; // Set the Output Stream to HTML res.setContentType("text/html"); // Get a handle to the OutputStream going back to the client ServletOutputStream out = res.getOutputStream(); // Get the name of the file that contains the form information paramStructureFile = req.getParameter( "structurefile" ); // Get the name of the file that contains the form data paramDataFile = req.getParameter( "datafile" ); // Set up files for processing. If there is an error, send // information back to the browser. if ( null == paramStructureFile ) { sendError( out, "Missing parameter: structurefile" ); return; } if ( null == paramDataFile ) { sendError( out, "Missing parameter: datafile" ); return; } // Try to open and parse the Structure file // Report an error to the browser if we fail try { fisStructure = new FileInputStream( "c:\\JavaWebServer1.1\\servlets\\" + paramStructureFile ); disStructure = new DataInputStream( fisStructure ); while ( 0 != disStructure.available() ) { inputLine = disStructure.readLine(); colInfo = new ColumnInfo( inputLine ); if ( null != colInfo ) { columns.addElement( colInfo ); } } } catch ( Exception e ) { sendError( out, "Could not open and process the file " + paramStructureFile + " Got error " + e ); return; } // Try to open and read the data file // Store the data in the Vector of ColumnInfo objects. // Each ColumnInfo object contains all rows of data for that column try { fisData = new FileInputStream( "c:\\JavaWebServer1.1\\servlets\\" + paramDataFile ); disData = new DataInputStream( fisData ); row = 0; while ( 0 != disData.available() ) { inputLine = disData.readLine(); stData = new StringTokenizer( inputLine, "|" ); for ( i = 0; i < columns.size(); i++ ) { ((ColumnInfo)columns.elementAt(i)).setNextData( stData.nextToken() ); } } } catch ( Exception e ) { sendError( out, "Could not open and process the file " + paramDataFile + " Got error " + e ); return; } // Now we generate the HTML form out.println( "" ); out.println( "Data Editing Form" ); out.println( "" ); // Notice that the ACTION command sends the data to the FormProcessingServlet // It embeds the datafile and structure file information inside of hidden fields // This is done because POST command cannot accept URL embedded parameters out.println( "
" ); out.println( "" ); out.println( "" ); out.println( "" ); out.println( "" ); // Output the headers for the display table for ( i=0; i < columns.size(); i++ ) { colInfo = ((ColumnInfo)columns.elementAt( i )); l = colInfo.getLength(); title = colInfo.getName(); if ( l < title.length() ) { l = title.length(); } out.println( "" ); } out.println( "" ); // Now display the data from the data file int numberOfRows = ((ColumnInfo)columns.elementAt(0)).getNumberOfRows(); for ( row = 0; row < numberOfRows; row ++ ) { out.println( "" ); for ( i=0; i < columns.size(); i++ ) { colInfo = ((ColumnInfo)columns.elementAt( i )); out.println( "" ); } out.println( "" ); } out.println( "
" ); out.println( colInfo.getName() ); out.println( "
" ); out.println( "" ); out.println( "
" ); out.println( "
" ); out.println( "  " ); out.println( "
" ); out.println( "" ); out.println( "" ); } public String getServletInfo() { return "A servlet that sends a form to the browser"; } // Routine to send error message back to the browser // in the form of an HTML page. private void sendError( ServletOutputStream out, String message ) throws IOException { out.println( "" ); out.println( "Data Editing Form" ); out.println( "" ); out.println( "

Error in FormDisplayServlet

" ); out.println( "

" ); out.println( message ); out.println( "

" ); out.println( "" ); out.println( "" ); } }