Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理(2)

本文介绍了一个基于HTML和Servlet的简单用户注册系统的实现过程,包括表单元素的使用、如何通过Servlet接收并处理表单数据,以及如何读取所有参数名称等关键步骤。
–Form 表单简介
–创建并提交表单
–使用Servlet处理表单
• 读取单个请求参数
• 读取多个表单
• 读取所有参数名称

–实例
• 注册会员
###############Michael分割线#################
看下radio选项
register.html
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >    
< html >    
     < head >    
         < title >register.html </title>    
         < meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" >    
         < meta http-equiv ="description" content ="this is my page" >    
         < meta http-equiv ="content-type" content ="text/html; charset=UTF-8" >    
         < ! --<link rel="stylesheet" type="text/css" href="./styles.css">-->    
     </head>    
     < body >    
         < form action ="/Servlet_Form/servlet/RegisterServlet" method ="post" >    
        用户名称: < input type ="text" name ="username" > < br >    
        用户密码: < input type ="password" name ="password" > < br >    
        用户爱好: < input type ="checkbox" name ="hobby" value ="1" >游泳    
                         < input type ="checkbox" name ="hobby" value ="2" >足球 < br >    
        用户性别: < input type ="radio" name ="gender" value ="male" >男    
                         < input type ="radio" name ="gender" value ="female" >女    
                         < input type ="radio" name ="gender" value ="secret" >保密 < br >    
         < input type ="submit" value ="注册" >    
     </body>    
</html>
RegisterServlet.java
package com.michael.servletform;    
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 RegisterServlet extends HttpServlet {    
         /**    
         * Constructor of the object.    
         */
    
         public RegisterServlet() {    
                 super();    
        }    
         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    
         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                String username = request.getParameter( "username");    
                String password = request.getParameter( "password");    
                String[] hobby = request.getParameterValues( "hobby");    
                String gender = request.getParameter( "gender");    
                 if(hobby!= null&&hobby.length>0){    
                         for( int i=0;i<hobby.length;i++){    
                                System.out.println(hobby[i]);    
                        }    
                }    
                response.setContentType( "text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.println("username:"+username);    
                out.println("password:"+password);    
                out.println("gender:"+gender);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
        }    
        /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
        public void doPost(HttpServletRequest request, HttpServletResponse response)    
                        throws ServletException, IOException {    
                doGet(request,response);    
        }    
        /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
        public void init() throws ServletException {    
                // Put your code here    
        }    
}
测试一下
image
image
–读取所有参数名称
Enumeration names = request.getParameterNames();
RegisterServlet.java
package com.michael.servletform;    
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 RegisterServlet extends HttpServlet {    
         /**    
         * Constructor of the object.    
         */
    
         public RegisterServlet() {    
                 super();    
        }    
         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    
         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                 /*    
                String username = request.getParameter("username");    
                String password = request.getParameter("password");    
                String[] hobby = request.getParameterValues("hobby");    
                String gender = request.getParameter("gender");    
                if(hobby!=null&&hobby.length>0){    
                        for(int i=0;i<hobby.length;i++){    
                                System.out.println(hobby[i]);    
                        }    
                }    
                */
    
                Enumeration enu = request.getParameterNames();    
                 while(enu.hasMoreElements()){    
                        String name = (String) enu.nextElement();    
                         if(name.equals( "hobby")){    
                                String[] hobby = request.getParameterValues( "hobby");    
                                 if(hobby!= null&&hobby.length>0){    
                                         for( int i=0;i<hobby.length;i++){    
                                                System.out.println(hobby[i]);    
                                        }    
                                }    
                        }    
                        String value = request.getParameter(name);    
                        System.out.println(name+ ":"+value);    
                }    
                 /*    
                response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.println("username:"+username);    
                out.println("password:"+password);    
                out.println("gender:"+gender);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
                */
    
        }    
         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                doGet(request,response);    
        }    
         /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    
}
image
测试下
image
image
image
• 实例
–注册会员
register.html
< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >    
< html >    
     < head >    
         < title >register.html </title>    
         < meta http-equiv ="keywords" content ="keyword1,keyword2,keyword3" >    
         < meta http-equiv ="description" content ="this is my page" >    
         < meta http-equiv ="content-type" content ="text/html; charset=UTF-8" >    
         < ! --<link rel="stylesheet" type="text/css" href="./styles.css">-->    
     </head>    
     < body >    
         < form action ="/Servlet_Form/servlet/RegisterServlet" method ="post" >    
        用户名称: < input type ="text" name ="username" > < br >    
        用户密码: < input type ="password" name ="password" > < br >    
        用户爱好: < input type ="checkbox" name ="hobby" value ="1" >游泳    
                         < input type ="checkbox" name ="hobby" value ="2" >足球 < br >    
        用户性别: < input type ="radio" name ="gender" value ="male" >男    
                         < input type ="radio" name ="gender" value ="female" >女    
                         < input type ="radio" name ="gender" value ="secret" >保密 < br >    
        用户职位: < select name ="position" >    
                         < option value ="CEO" >CEO </option>    
                         < option value ="CFO" >CFO </option>    
                         < option value ="CTO" >CTO </option>    
                         </select> < br >    
        用户简历: < textarea rows ="5" cols ="20" name ="resume" > </textarea>    
         < input type ="submit" value ="注册" >    
     </body>    
</html>
web.xml
<? xml version ="1.0" encoding ="UTF-8" ?>    
< web-app version ="2.4"    
         xmlns ="http://java.sun.com/xml/ns/j2ee"    
         xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"    
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee    
http://java.sun.com/xml/ns/j2ee/web-app_2_4.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 >RegisterServlet </ servlet-name >    
         < servlet-class >com.michael.servletform.RegisterServlet </ servlet-class >    
     </ servlet >    
     < servlet-mapping >    
         < servlet-name >RegisterServlet </ servlet-name >    
         < url-pattern >/servlet/RegisterServlet </ url-pattern >    
     </ servlet-mapping >    
</ web-app >
RegisterServlet.java
package com.michael.servletform;    
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 RegisterServlet extends HttpServlet {    
         /**    
         * Constructor of the object.    
         */
    
         public RegisterServlet() {    
                 super();    
        }    
         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    
         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                 /*    
                String username = request.getParameter("username");    
                String password = request.getParameter("password");    
                String[] hobby = request.getParameterValues("hobby");    
                String gender = request.getParameter("gender");    
                if(hobby!=null&&hobby.length>0){    
                        for(int i=0;i<hobby.length;i++){    
                                System.out.println(hobby[i]);    
                        }    
                }    
                */
    
                Enumeration enu = request.getParameterNames();    
                 while(enu.hasMoreElements()){    
                        String name = (String) enu.nextElement();    
                         if(name.equals( "hobby")){    
                                String[] hobby = request.getParameterValues( "hobby");    
                                 if(hobby!= null&&hobby.length>0){    
                                         for( int i=0;i<hobby.length;i++){    
                                                System.out.println(hobby[i]);    
                                        }    
                                }    
                        }    
                        String value = request.getParameter(name);    
                        System.out.println(name+ ":"+value);    
                }    
                 /*    
                response.setContentType("text/html");    
                PrintWriter out = response.getWriter();    
                out    
                                .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");    
                out.println("<HTML>");    
                out.println("    <HEAD><TITLE>A Servlet</TITLE></HEAD>");    
                out.println("    <BODY>");    
                out.println("username:"+username);    
                out.println("password:"+password);    
                out.println("gender:"+gender);    
                out.println("    </BODY>");    
                out.println("</HTML>");    
                out.flush();    
                out.close();    
                */
    
        }    
         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                doGet(request,response);    
        }    
         /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occure    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    
}
image
测试一下
image
image
看下效果
image
###############Michael分割线#################
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值