隨筆 - 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@ 閱讀(1160) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 津市市| 尼勒克县| 团风县| 上饶县| 凤城市| 开封县| 阿鲁科尔沁旗| 汝阳县| 和平县| 宝丰县| 成武县| 益阳市| 虹口区| 故城县| 西乌珠穆沁旗| 平武县| 呼玛县| 涞水县| 织金县| 阳新县| 灵武市| 湖北省| 阿合奇县| 双江| 洪泽县| 西充县| 平遥县| 武义县| 绥江县| 永仁县| 门头沟区| 汤原县| 江津市| 诸城市| 滨州市| 海阳市| 瓦房店市| 兴海县| 德阳市| 建宁县| 呼玛县|