package examples.servlets; import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import weblogic.html.*; /** * This is an example of a simple FormServlet. It * also demonstrates the use of htmlKona tables. * * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved. * @author Copyright (c) 1999-2000 by BEA Systems, Inc. All Rights Reserved. */ public class SimpleFormServlet extends HttpServlet{ ServletPage sp; // Set up some arrays to make passing information easier. final static int B_EMAIL = 0; final static int B_NAME = 1; final static int B_COMPANY = 2; final static int B_STREET = 3; final static int B_CITY = 4; final static int B_STATE = 5; final static int B_ZIPCODE = 6; final static int B_COUNTRY = 7; final static int B_PHONE = 8; final static int B_MAX = 9; static String label[] = new String[B_MAX]; static { label[B_EMAIL] = "Email"; label[B_NAME] = "Name"; label[B_COMPANY] = "Company"; label[B_STREET] = "Street"; label[B_CITY] = "City"; label[B_STATE] = "State"; label[B_ZIPCODE] = "Zipcode"; label[B_COUNTRY] = "Country"; label[B_PHONE] = "Phone"; } String value[] = new String[B_MAX]; static int maxlens[] = new int[B_MAX]; static { maxlens[B_EMAIL] = 40; maxlens[B_NAME] = 30; maxlens[B_COMPANY] = 30; maxlens[B_STREET] = 50; maxlens[B_CITY] = 30; maxlens[B_STATE] = 10; maxlens[B_ZIPCODE] = 10; maxlens[B_COUNTRY] = 15; maxlens[B_PHONE] = 15; } /** * Generates an htmlKona FormElement with the form fields already added */ private static FormElement getForm(String[] label, String[] value, int[] maxlens) { TableElement tab = new TableElement(); for (int i = 0; i < B_MAX; i++) { tab.addElement(new TableRowElement() .addElement(new BoldElement(label[i]) + ":") .addElement(new InputElement(label[i]) .setSize(30) .setMaxlen(maxlens[i]) .setValue(value[i]))); } tab.addElement(new TableRowElement() .addElement(new TableDataElement(new InputElement("SUBMIT", FieldType.submit) .setValue("Register")) .setColSpan(2) .setAlign(AlignType.center))); FormElement form = new FormElement("simpleFormServlet", "POST"); form.addElement(tab); return form; } /** * The doGet method handles the initial invokation of the servlet. It responds * with a form, that will use the "POST" method to submit data. */ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { try { res.setContentType("text/html"); res.setHeader("Pragma", "no-cache"); ServletPage sp = new ServletPage(); sp = new ServletPage(); FormElement fe = getForm(label, value, maxlens); sp.getBody() .addElement(fe.asCenteredElement()) .addElement(MarkupElement.HorizontalLine); sp.output(res.getOutputStream()); } catch (Throwable th) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); try { PrintWriter ps = new PrintWriter(buf); th.printStackTrace(ps); ps.flush(); } catch (Exception e) {} } } /** * Responds to the "POST" query from the original form supplied by the goGet() * method. */ public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); res.setHeader("Pragma", "no-cache"); PrintWriter pw = new PrintWriter(res.getOutputStream(),true); pw.println("Thank you!"); pw.println("Thank you for filling out our form. The information you have submitted is as follows:"); pw.println("

"); pw.println("

"); String ParamString; Enumeration ParamNames = req.getParameterNames(); while(ParamNames.hasMoreElements()){ ParamString = (String)ParamNames.nextElement(); if (!ParamString.equals("SUBMIT")) { pw.println("" + ParamString + ": " + req.getParameterValues(ParamString)[0]); pw.println("

"); } } pw.println(""); return; } /** * Simple info about the servlet. */ public String getServletInfo() { return "A simple form servlet"; } }