home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1999 November / PCONLINE_11_99.ISO / filesbbs / OS2 / APCHSSL2.ZIP / OS2HTTPD / servlets / FormDisplayServlet.java < prev    next >
Encoding:
Java Source  |  1999-07-25  |  3.3 KB  |  95 lines

  1. /* -----------------------------------------------------------------
  2. ** Copyright 1997-98 IBM Corporation.  All rights reserved.
  3. **
  4. ** -----------------------------------------------------------------
  5. */
  6. /*
  7.  * This is a demonstration of how to read in HTML form data and
  8.  * write that same form data back to the browser page. No form
  9.  * data processing is done in this servlet. See FormProcessingServlet
  10.  * for an example of HTML form data processing and custom output.
  11.  */
  12.  
  13. import javax.servlet.*;
  14. import javax.servlet.http.*;
  15. import java.io.*;
  16. import java.util.*;
  17. import java.text.*;
  18.  
  19. /**
  20.   * Example FormDisplayServlet-- 
  21.   * This servlet demonstrates HTML form data handling and 
  22.   * formatting output to the browser page as HTML.
  23.   */
  24.  
  25. public class FormDisplayServlet extends HttpServlet
  26. {
  27.     /** 
  28.       * @param req Instance of class HttpServletRequest.
  29.       * @param res Instance of class HttpServletResponse.
  30.       * @exception IOException When inputting form data or writing
  31.       * the form data back to the browser.
  32.       */
  33.  
  34.         public final String BASENAME = "samples";
  35.         ResourceBundle rb = null;
  36.  
  37.         // ****************************************************************
  38.         // * Initialize servlet when it is first loaded                   *
  39.         // ****************************************************************
  40.         public void init(ServletConfig config)
  41.         throws ServletException
  42.         {
  43.            super.init(config);
  44.  
  45.            /* get the ResourceBundle for localization */
  46.            try
  47.            {
  48.                rb = ResourceBundle.getBundle(BASENAME);
  49.            }
  50.            catch(Exception e)
  51.            {
  52.                System.out.println(e.getMessage());
  53.            }
  54.         }
  55.  
  56.         public void doPost(HttpServletRequest req, HttpServletResponse res)
  57.                 throws IOException
  58.         {
  59.                 Enumeration params;
  60.                 String name;
  61.                 String[] values;
  62.                 MessageFormat mf = null;
  63.                 ServletOutputStream os;
  64.                 PrintWriter pw;
  65.                 String[] vars = {"", ""};
  66.  
  67.                 res.setContentType("text/html;charset=iso-8859-1");
  68.                 pw = res.getWriter();
  69.                 
  70.                 pw.println("<HEAD>");
  71.                 pw.println("<TITLE>" + rb.getString("s1") + "</TITLE>");
  72.                 pw.println("<BODY><H2>" + rb.getString("s2") + "</H2>");
  73.                 pw.println("<P>" + rb.getString("s3") +"<BR><BR>");
  74.  
  75.  
  76.                 params = req.getParameterNames();
  77.                 while(params.hasMoreElements())
  78.                 {
  79.                     name = (String)params.nextElement();
  80.                     values = req.getParameterValues(name);
  81.                     if (values != null) {
  82.                       vars[0] = name;
  83.                       vars[1] = values[0];
  84.                       pw.println(mf.format(rb.getString("s4"), vars) + "<BR>");
  85.                       pw.println(mf.format(rb.getString("s5"), vars) + "<BR>");
  86.                     }
  87.                 }
  88.                 pw.println("</BODY>");
  89.  
  90.                 // flush and close the print writer
  91.                 pw.flush();
  92.  
  93.         } // end of doGet() 
  94. } // end of class FormDisplayServlet
  95.