⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simpleformservlet.java

📁 java的一系列产品中包括jsme,jmse,j2ee,本文件提供j2ee实现的源代码.
💻 JAVA
字号:
package examples.servlets;

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
//import weblogic.html.*;

import examples.utils.common.ExampleUtils;

/**
 * 
 * This example demonstrates using HTML forms in servlets and using
 * htmlKona tables to display the output.
 *
 * <p><h3>Build the Example</h3>  
 * <ol>
 * <li> Open a new command shell. 
 * <p><li>Set up this development shell as described in 
 * <a href=../examples.html#environment>Setting up Your Environment for 
 * Building and Running the Examples</a>.
 * <p>
 * <li>Compile the servlet using the following command line:
 * <pre>  prompt&gt;<b> javac -d %EX_WEBAPP_CLASSES% SimpleFormServlet.java</b></pre>
 * 
 * 
 * <p> <li>Start WebLogic Server with the <a
 * href=../examples.html>examples configuration</a>.  <p> 
 *
 * </ol>
 * <p><h3>Configure the Server</h3>
 * <dl>
 * <dd>Make sure that the <font face="Courier New" size=-1>examplesWebApp</font> is <a href=../examples.html#webApp>deployed on your server</a>.
 * </dl>
 * <p><h3>Run the Example</h3>
 * <dl>
 * <dd>Use a web browser to load the following URL: 
 * 
 * <pre><b>http://localhost:7001/examplesWebApp/SimpleFormServlet</b></pre>
 *
 * </dl>
 * <h3>There's More...</h3>
 *
 * For more information on servlets, see  <a
 * href="http://e-docs.bea.com/wls/docs70/servlet/index.html">Programming WebLogic HTTP Servlets</a>. 
 * <p>
 * @author Copyright (c) 1996-98 by WebLogic, Inc. All Rights Reserved.
 * @author Copyright (c) 1999-2002 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 String getForm(String[] label, String[] value, int[] maxlens) {
    
      String form = 
	"<CENTER><FONT SIZE='6'>Simple FFForm Servlet</FONT></CENTER>" +
	"<IMG ALIGN=right SRC='images/BEA_Button_Final_web.gif'>" +
	"<CENTER><FORM ACTION='SimpleFormServlet' METHOD='POST'><TABLE>";

    for (int i = 0; i < B_MAX; i++) {
	form += "<TR><TD><B>" + label[i] + "</B>:</TD>" +
		    "<TD><INPUT TYPE=TEXT " +
			    " NAME='"	    + label[i] + "' " +
			    " VALUE='"	    + ( null == value[i] ? "" : value[i] ) + "' " +
			    " MAXLENGTH="   + maxlens[i] +
			    " SIZE=30>" +
		    "</TD></TR>";
    }

    form += "<TR><TD COLSPAN=2 ALIGN=center>" +
	    "<INPUT type='submit' name='SUBMIT' VALUE='Register'>" +
	    "</TD></TR></TABLE></FORM>";

    
    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");
      
      PrintWriter out = res.getWriter();

      out.println( 
	    "<html><head><title>Simple Form Servlet</title></head><body>" );
      out.println( getForm(label, value, maxlens) );
      out.println( "<HR>" );
      out.println( "</body></html>" );
    }
    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 out = new PrintWriter(res.getOutputStream(),true);
    out.println( "<html><head><title>Thank You!</title></head><body>" );
    out.println("Thank you for filling out our form. The information you have submitted is as follows:");
    out.println("<P>");
    out.println("<P>");
    String ParamString;
    Enumeration ParamNames = req.getParameterNames();
    while(ParamNames.hasMoreElements()){
      ParamString = (String)ParamNames.nextElement();
      if (!ParamString.equals("SUBMIT")) {
        out.println("<b>" + ParamString + ":</b> " + 
                   req.getParameterValues(ParamString)[0]);
        out.println("<P>");
      }
    }
    out.println( "</body></html>" );
    return;
  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -