–Form 表单简介
–创建并提交表单
–使用Servlet处理表单
• 读取单个请求参数
• 读取多个表单
• 读取所有参数名称
–实例
• 注册会员
–创建并提交表单
–使用Servlet处理表单
• 读取单个请求参数
• 读取多个表单
• 读取所有参数名称
–实例
• 注册会员
###############Michael分割线#################
–Form 表单简介
• 表单是web程序和用户交互的主要途径之一,例如:搜索引擎、用户登录、注册等操作都离不开表单的使用
• 常用表单元素
–input
» text
» password
» radio
» checkbox
» hidden
» file
» button
» reset
» submit
–select
» option
–textarea
• 表单是web程序和用户交互的主要途径之一,例如:搜索引擎、用户登录、注册等操作都离不开表单的使用
• 常用表单元素
–input
» text
» password
» radio
» checkbox
» hidden
» file
» button
» reset
» submit
–select
» option
–textarea
• 创建并提交表单
–一个简单的表单(例如:登录),如下所示
–一个简单的表单(例如:登录),如下所示
• 使用Servlet处理表单
–读取单个请求参数
• String user = request.getParameter(“user”);
–读取单个请求参数
• 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;
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");
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();
}
/**
* 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
}
}
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");
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();
}
/**
* 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
}
}
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 ="submit" value ="注册" >
</body>
</html>
< 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 ="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 >
< 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 >
• String[] hobby = request.getParameterValues(“hobby”);
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 ="submit" value ="注册" >
</body>
</html>
< 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 ="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");
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(" </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
}
}
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");
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(" </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
}
}
测试一下
hobby输出到控制台了
##########Michael分割线################
附件:http://down.51cto.com/data/2353099
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/168542,如需转载请自行联系原作者