Java/Servlets/Form — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 06:11, 1 июня 2010

Basic Form processor

/*
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.ru/xml/ns/j2ee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://java.sun.ru/xml/ns/j2ee
         http://java.sun.ru/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  <servlet>
    <servlet-name>BasicFormProcessor</servlet-name>
    <servlet-class>BasicFormProcessor</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>BasicFormProcessor</servlet-name>
    <url-pattern>/formProcessor</url-pattern>
  </servlet-mapping>
</web-app>
*/
import java.io.IOException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BasicFormProcessor extends HttpServlet {
  public void doPost(HttpServletRequest req, HttpServletResponse res)
      throws IOException {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out
        .println("<html><head><title>Basic Form Processor Output</title></head>");
    out.println("<body>");
    out.println("<h1>Here is your Form Data</h1>");
    //extract the form data here
    String title = req.getParameter("title");
    String name = req.getParameter("name");
    String city = req.getParameter("city");
    String country = req.getParameter("country");
    String tel = req.getParameter("tel");
    String age = req.getParameter("age");
    // extracting data from the checkbox field
    String[] interests = req.getParameterValues("interests");
    //output the data into a web page
    out.println("Your title is " + title);
    out.println("<br>Your name is " + name);
    out.println("<br>Your city is " + city);
    out.println("<br>Your country is " + country);
    out.println("<br>Your tel is " + tel);
    out.println("<br>Your interests include<ul> ");
    for (int i = 0; i < interests.length; i++) {
      out.println("<li>" + interests[i]);
    }
    out.println("</ul>");
    out.println("<br>Your age is " + age);
    out.println("</body></html>");
  }
}





feedback Form

       
<HTML><HEAD><TITLE> feedback Form </TITLE></HEAD>
<BODY><H1> Feedback Form </H1>
<HR>
<BR>
<FORM NAME="ParameterPost" ACTION="/servlets-examples/PrintFormParams" METHOD="POST">
<TABLE BGCOLOR="BLANCHEDALMOND" ALIGN="CENTER" BORDER="0">
    <TR>
    <TD ALIGN="CENTER"><B>Name:</B></TD> 
    <TD ALIGN="CENTER"> <INPUT TYPE="TEXT" SIZE="25" NAME="Person"></TD>
    </TR>
   
    <TR>
    <TD ALIGN="CENTER"><B>Email:</B></TD> 
    <TD ALIGN="CENTER"> <INPUT TYPE="TEXT" SIZE="25" MAXLENGTH="40" NAME="emailaddress"></TD>
    </TR>
   
   
    <TR>
    <TD ALIGN="CENTER"><B>How did you find this site?</B></TD> 
    <TD ALIGN="CENTER"> 
        <SELECT NAME="from" SIZE="1">
        <OPTION VALUE = "Website" SELECTED>Another Website</OPTION>
        <OPTION VALUE = "search engine">A search engine</OPTION>
        <OPTION VALUE = "friend">A friend told you</OPTION>
        <OPTION VALUE = "email">From an email</OPTION>
        <OPTION VALUE = "unlisted">Another way not listed here</OPTION>
        </SELECT>
    </TD>    
    </TR>
   
    <TR>
    <TD ALIGN="CENTER"><B>How would you rate my website:</B></TD> 
    <TD ALIGN="CENTER"> 
    <INPUT TYPE="radio" NAME = "rating" VALUE = "Excellent"> Excellent 
    <INPUT TYPE="radio" NAME = "rating" VALUE = "Good"> Good 
    <INPUT TYPE="radio" NAME = "rating" VALUE = "Average" CHECKED> Average 
    <INPUT TYPE="radio" NAME = "rating" VALUE = "Poor"> Poor 
    <INPUT TYPE="radio" NAME = "rating" VALUE = "Overhaul"> Needs an Overhaul
    </TD>
    </TR>
   
    <TR>
    <TD ALIGN="CENTER"><B>Comments or Suggestions:</B></TD> 
    <TD ALIGN="CENTER"> 
    <TEXTAREA ROWS="6" COLS="40" WRAP="PHYSICAL" Name="suggestions">
             Enter any comments or suggestions you have here.</TEXTAREA>
    </TD>
    </TR>
    
    <TR>
    <TD ALIGN="CENTER"><B>Do you think this form looks nice?</B></TD> 
    <TD ALIGN="CENTER"> 
    <INPUT TYPE="CHECKBOX" NAME="formrating" VALUE="yes">
    </TD>
    </TR>
   
    <TR>
    
    <TD ALIGN="LEFT">
    
    <INPUT TYPE="SUBMIT" VALUE="Send Comments" ALIGN="MIDDLE">  &nbsp; &nbsp;
    <INPUT TYPE="RESET" VALUE="Clear Form" ALIGN="MIDDLE"> 
    
    </TD>
    </TR>
   
</FORM>
</BODY>
</html>
<!-- web.xml -->
    <servlet>
        <servlet-name>PrintFormParams</servlet-name>
        <servlet-class>PrintFormParams</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PrintFormParams</servlet-name>
        <url-pattern>/PrintFormParams</url-pattern>
    </servlet-mapping>
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PrintFormParams extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    PrintParams(request, response);
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    PrintParams(request, response);
  }
  public void PrintParams(HttpServletRequest request,
      HttpServletResponse response) throws IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String htmlHeader = "<HTML><HEAD><TITLE> Printed Form Parameters </TITLE></HEAD><BODY>";
    String htmlFooter = "</BODY></HTML>";
    out.println(htmlHeader);
    out.println("<TABLE ALIGN=CENTER BORDER=1>");
    out.println("<tr><th> Input Name </th><th> Value </th>");
    Enumeration enum = request.getParameterNames();
    while (enum.hasMoreElements()) {
      String inputName = (String) enum.nextElement();
      String value = request.getParameter(inputName);
      if (value.length() != 0) {
        out.println("<tr><td align=center>" + inputName + "</td>");
        out.println("<td align=center>" + value + "</td></tr>");
      } else {
        out.println("<tr><td align=center>" + inputName + "</td>");
        out.println("<td align=center><i>Null</i></td></tr>");
      }
    }
    out.println("</TABLE><BR>");
    out.println(htmlFooter);
  }
}





HTML Forms with Servlets

React to user form input

import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FirstServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //set the MIME type of the response to "text/html"
    response.setContentType("text/html");
    //use a PrintWriter send text data to the client 
    java.io.PrintWriter out = response.getWriter();
    //output the HTML content
    out.println("<html><head>");
    out.println("<title>Help Page</title></head><body>");
    out.println("<h2>Please submit your information</h2>");
    //make sure method="post" so that the servlet service method
    //calls doPost in the response to this form submit
    out.println("<form method=\"post\" action =\""
        + request.getContextPath() + "/FirstServlet\" >");
    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println("Your first name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"firstname\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your last name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"lastname\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your email: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"email\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
    out.println("</table></form>");
    out.println("</body></html>");
  } 
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, java.io.IOException {
    //display the parameter names and values
    Enumeration paramNames = request.getParameterNames();
    String parName;
    boolean emptyEnum = false;
    if (!paramNames.hasMoreElements())
      emptyEnum = true;
    //set the MIME type of the response to "text/html"
    response.setContentType("text/html");
    //use a PrintWriter send text data to the client
    java.io.PrintWriter out = response.getWriter();
    //Begin assembling the HTML content
    out.println("<html><head>");
    out.println("<title>Submitted Parameters</title></head><body>");
    if (emptyEnum) {
      out.println("<h2>No parameters</h2>");
    } else {
      out.println("<h2>Here are the submitted parameter values</h2>");
    }
    while (paramNames.hasMoreElements()) {
      parName = (String) paramNames.nextElement();
      out.println("<strong>" + parName + "</strong> : "
          + request.getParameter(parName));
      out.println("<br />");
    }
    out.println("</body></html>");
  }
}





Servlets Form TextField