随笔 - 15, 文章 - 0, 评论 - 5, 引用 - 0
数据加载中……

一个servlet处理表单的简单例子

  实例名称:    潜在用户网络调查表


HTML页面代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

 <head>
  <title>潜在用户网络调查</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
 </head>

 <body>
  <h1>
   潜在用户网络调查
  </h1>
  <br>
  <form method="post" action="/webproject1/servlet/loginform">
   <table border="0">
    <tr>
     <td align="right">
      姓名:
     </td>
     <td colspan="2" align="left">
      <input type="text" name="name" size="40">
     </td>
    </tr>
    <tr>
     <td align="right">
      EMAIL:
     </td>
     <td colspan="2" align="left">
      <input type="text" name="email" size="40">
     </td>
    </tr>
    <tr>
     <td align="right">
      年纪:
     </td>
     <td align="left">
      <input type="radio" name="age" value="18">
      小于18
      <input type="radio" name="age" value="18-25">
      18 - 25
      <input type="radio" name="age" value="26-40">
      26-40
      <input type="radio" name="age" value=">40">
      大于 40
     </td>
    </tr>
    <tr>
     <td align="right">
      编程时间:
     </td>
     <td align="left">
      <select name="codetime" size=1>
       <option value="never">
        不编程
       <option value="6">
        小于6个月
       <option value="6-12">
        6 - 12 月
       <option value="12-24">
        1 - 2年
       <option value=">24">
        2年以上
      </select>
     </td>
    </tr>
    <tr>
     <td align="right">
      使用的操作系统
     </td>
     <td align="left">
      <select name="os" size="6" multiple>
       <option value="WinXP">
        Win XP
       </option>
       <option value="Win2000/2003">
        Win 2000/2003
       </option>
       <option value="Linux">
        Linux
       </option>
       <option value="FreeBSD">
        FreeBSD
       </option>
       <option value="MacOS">
        Mac OS
       </option>
       <option value="other">
        other
       </option>
      </select>
     </td>
    </tr>
    <tr>
     <td>
      使用的编程语言
     </td>
     <td>
      <input type="checkbox" name="language" value="C">
      C
      <input type="checkbox" name="language" value="C++">
      C++
      <input type="checkbox" name="language" value="C#">
      C#
      <input type="checkbox" name="language" value="Python">
      Python
      <input type="checkbox" name="language" value="Java">
      Java
      <input type="checkbox" name="language" value="VB">
      VB
      <input type="checkbox" name="language" value="Dephi">
      Dephi
     </td>
    </tr>
    <tr>
     <td align="right">
      建议:
     </td>
     <td colspan="2" align="left">
      <textarea name="comment" cols="40" rows="4"></textarea>
     </td>
    </tr>
    <tr>
     <td></td>
     <td>
      <input type="reset" value="reset">
      <input type="submit" value="submit">
     </td>
    </tr>
   </table>
  </form>
 </body>
</html>


servlet中的代码

package com.v503.zhouzhou;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginForm extends HttpServlet {


 private static final long serialVersionUID = 1560239073696880062L;


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

        doPost(request, response);
 }

 
 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  request.setCharacterEncoding("utf-8");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.println("姓名:"+filterHtml(request.getParameter("name"))+"<br>");
  out.println("email:"+filterHtml(request.getParameter("email"))+"<br>");
  out.println("年龄:"+filterHtml(request.getParameter("age"))+"<br>");
  out.println("编程时间:"+request.getParameter("codetime")+"<br>");
  out.println("使用的操作系统:");
  @SuppressWarnings("unused")
  String os[]=request.getParameterValues("os");
  for(int i = 0;i<os.length;i++)
  {   out.println(os[i]+"<br>");} 
  out.println("使用的编程语言");
  @SuppressWarnings("unused")
  String language[]=request.getParameterValues("language");
  for(int i = 0;i<language.length;i++)
  {out.println(language[i]+"<br>");}
  out.println("建议:"+filterHtml(request.getParameter("comment"))+"<br>");
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }
// 过滤的方法
 public String filterHtml(String value){
  value=value.replaceAll("&","&amp");
  value=value.replaceAll("<", "&lt;");
  value=value.replaceAll(">", "&gt;");
  value=value.replaceAll(" ","&nbsp;");
  value=value.replaceAll("'","value39;");
  value=value.replaceAll("\"","value;");
  value=value.replaceAll("\n", "value");
  return value;
 }

}


web.xml文件代码

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginForm</servlet-name>
    <servlet-class>com.v503.zhouzhou.LoginForm</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>LoginForm</servlet-name>
    <url-pattern>/servlet/loginform</url-pattern>
  </servlet-mapping>
 
</web-app>




 

posted on 2008-07-29 20:41 zhouzhou@ 阅读(1152) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: