Java EE WEB工程师培训-JDBC+Servlet+JSP整合开发之13.Form表单处理
–Form 表单简介
–创建并提交表单
–使用Servlet处理表单
• 读取单个请求参数
• 读取多个表单
• 读取所有参数名称–实例
• 注册会员###############Michael分割线#################
–Form 表单简介• 表单是web程序和用户交互的主要途径之一,例如:搜索引擎、用户登录、注册等操作都离不开表单的使用
• 常用表单元素
–input
» text
» password
» radio
» checkbox
» hidden
» file
» button
» reset
» submit
–select
» option
–textarea
• 创建并提交表单
–一个简单的表单(例如:登录),如下所示
–当单击“登录”按钮时表单被提交
• 使用Servlet处理表单
–读取单个请求参数
•
String user = request.getParameter(“user”);
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;
publicclass RegisterServlet extends HttpServlet {
public RegisterServlet() {
super();
}
publicvoid destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
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("username:"+password);
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
publicvoid init() throws ServletException {
// Put your code here
}
}
register.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="this is my page">
<metahttp-equiv="content-type"content="text/html; charset=UTF-8">
<!--<link rel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<body>
<formaction="/Servlet_Form/servlet/RegisterServlet"method="post">
用户名称:<inputtype="text"name="username"><br>
用户密码:<inputtype="password"name="password"><br>
<inputtype="submit"value="注册">
</body>
</html>
web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="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>
测试
–读取多个表单
• String[] hobby = request.getParameterValues(“hobby”);register.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="this is my page">
<metahttp-equiv="content-type"content="text/html; charset=UTF-8">
<!--<link rel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<body>
<formaction="/Servlet_Form/servlet/RegisterServlet"method="post">
用户名称:<inputtype="text"name="username"><br>
用户密码:<inputtype="password"name="password"><br>
用户爱好:<inputtype="checkbox"name="hobby"value="1">游泳
<inputtype="checkbox"name="hobby"value="2">足球<br>
<inputtype="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;
publicclass RegisterServlet extends HttpServlet {
public RegisterServlet() {
super();
}
publicvoid destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
String[] hobby = request.getParameterValues("hobby");
if(hobby!=null&&hobby.length>0){
for(int i=0;i<hobby.length;i++){
System.out.println(hobby);
}
}
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(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
publicvoid init() throws ServletException {
// Put your code here
}
}
测试一下
hobby输出到控制台了
看下radio选项
register.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="this is my page">
<metahttp-equiv="content-type"content="text/html; charset=UTF-8">
<!--<link rel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<body>
<formaction="/Servlet_Form/servlet/RegisterServlet"method="post">
用户名称:<inputtype="text"name="username"><br>
用户密码:<inputtype="password"name="password"><br>
用户爱好:<inputtype="checkbox"name="hobby"value="1">游泳
<inputtype="checkbox"name="hobby"value="2">足球<br>
用户性别:<inputtype="radio"name="gender"value="male">男
<inputtype="radio"name="gender"value="female">女
<inputtype="radio"name="gender"value="secret">保密<br>
<inputtype="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;
publicclass RegisterServlet extends HttpServlet {
public RegisterServlet() {
super();
}
publicvoid destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
publicvoid 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);
}
}
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();
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
publicvoid init() throws ServletException {
// Put your code here
}
}
测试一下
–读取所有参数名称
• 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;
publicclass RegisterServlet extends HttpServlet {
public RegisterServlet() {
super();
}
publicvoid destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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);
}
}
}
String value = request.getParameter(name);
System.out.println(name+":"+value);
}
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
publicvoid init() throws ServletException {
// Put your code here
}
}
测试下
• 实例
–注册会员
register.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>register.html</title>
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="this is my page">
<metahttp-equiv="content-type"content="text/html; charset=UTF-8">
<!--<link rel="stylesheet"type="text/css"href="./styles.css">-->
</head>
<body>
<formaction="/Servlet_Form/servlet/RegisterServlet"method="post">
用户名称:<inputtype="text"name="username"><br>
用���密码:<inputtype="password"name="password"><br>
用户爱好:<inputtype="checkbox"name="hobby"value="1">游泳
<inputtype="checkbox"name="hobby"value="2">足球<br>
用户性别:<inputtype="radio"name="gender"value="male">男
<inputtype="radio"name="gender"value="female">女
<inputtype="radio"name="gender"value="secret">保密<br>
用户职位:<selectname="position">
<optionvalue="CEO">CEO</option>
<optionvalue="CFO">CFO</option>
<optionvalue="CTO">CTO</option>
</select><br>
用户简历:<textarearows="5"cols="20"name="resume"></textarea>
<inputtype="submit"value="注册">
</body>
</html>
web.xml
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="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;
publicclass RegisterServlet extends HttpServlet {
public RegisterServlet() {
super();
}
publicvoid destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
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);
}
}
}
String value = request.getParameter(name);
System.out.println(name+":"+value);
}
}
publicvoid doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
publicvoid init() throws ServletException {
// Put your code here
}
}
测试一下
看下效果
###############Michael分割线#################
点击下载��资源>>
免费分享IT技术资料,就在51CTO下载中心:Down.51cto.com
该贴被barry编辑于2012-2-15 14:33:25