|
源代码网推荐
来源:LoveJSP.site 在Web程序设计中,处理表单提交的数据是获取Web数据的主要方法,今天,我们来看一看Servlet中是怎样处理来自表单的数据的。
表单数据的提交方法有两种Post方法和Get方法,当使用Post方法时,数据由标准的输入设备读入,当使用Get方法时,数据由CGI变量QUERY_STRING传递给表单数据处理程序。
Servlet会自动将以上两种方法得到的数据进行处理,从而使用户只要简单的调用HttpServletRequest的getParameter方法,给出变量名称即可取得该变量的值。需要注意的是,变量的名称是大小写敏感的。对于Post方法或Get方法提交的数据,Servlet的处理方法是一样的。当请求的变量不存在时,将会返回一个空字符串。如果变量有多个值,你应该调用getParameterValues,这个方法将会返回一个字符串数组。使用getParameterNames可以取得所有变量的名称,该方法返回一个Emumeration方法。
下面让我们来看一个简单的例子,下面这个Servlet读取表单中指定名称的五个字段的值。下载这个例子
//Html file----> postdata.htm 下载本文件 <html> <head> <title>getFormData Servlet Example form LoveJSP.com</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> </head>
<body bgcolor="#FFFFFF"> <h1 align="center"> <i><b>Demo Page</b></i></h1> <form action="/try/servlet/lovejsp.getFormData"> <p> </p> <table width="41%" border="2" align="center"> <tr bgcolor="#6633CC" align="center"> <td colspan="2" align="center"><font color="white">getFormData Servlet Demo Page</font></td> </tr> <tr bgcolor="#FFFFCC"> <td align="center" width="43%"> <div align="right">username:</div> </td> <td width="57%"> <div align="left"> <input type="text" name="username"> </div> </td> </tr> <tr bgcolor="#CCFF99"> <td align="center" width="43%"> <div align="right">password:</div> </td> <td width="57%"> <div align="left"> <input type="password" name="password"> </div> </td> </tr> <tr bgcolor="#FFFFCC"> <td align="center" width="43%"> <div align="right">Email:</div> </td> <td width="57%"> <div align="left"> <input type="text" name="email"> </div> </td> </tr> <tr bgcolor="#CCFF99"> <td align="center" width="43%"> <div align="right">Homepage:</div> </td> <td width="57%"> <div align="left"> <input type="text" name="Homepage"> </div> </td> </tr> </table> <p align="center"> <input type="reset" name="Reset" value="clear"> <input type="submit" name="Submit2" value="Let"s Go"> </p> </form> </body> </html>
//Servlet File getFormData.java 下载本文件
package lovejsp;
import java.io.*; import javax.servlet.*; import javax.servlet.http.*;
/** Servlet getParameter Ex from Lovejsp.site(http://www.lovejsp.com) */
public class getFormData extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "读取表单数据"; out.println(LovejspTools.headTitle(title) + //a tools method to show the html code with title "<BODY BGCOLOR="#FDF5E6">
" + "<H1 ALIGN=CENTER>" + title + "</H1>
" + "<UL>
" + " <LI><B>username</B>: " + request.getParameter("username") + "
" + " <LI><B>password</B>: " + request.getParameter("password") + "
" + " <LI><B>Email</B>: " + request.getParameter("Email") + "
" + " <LI><B>Homepage</B>: " + request.getParameter("Homepage") + "
" + "</UL>
" + "</BODY></HTML>"); } }
这个程序的运行结果如下图所示:
大家可能注意到了,上面的结果中Email的值是null,这是因为在表单中没有这个字段,表单的email的名称是email。
下面这个程序是getParameterNames的示例,也是调试程序的很好的工具,它的功能是显示所有的表单数据。
//Servlet File ShowAllFormData.java 下载本文件
package lovejsp;
import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import java.util.*;
public class ShowAllFormData extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String title = "显示所有的Form变量的值"; out.println("<HTML><HEAD><TITLE>"+title+"</TITLE></HEAD>"+ "<BODY BGCOLOR="#FDF5E6">
" + "<H1 ALIGN=CENTER>" + title + "</H1>
" + "<TABLE BORDER=1 ALIGN=CENTER>
" + "<TR BGCOLOR="#FFAD00">
" + "<TH>变量名称<TH>变量值"); Enumeration paramNames = request.getParameterNames(); while(paramNames.hasMoreElements()) { String paramName = (String)paramNames.nextElement(); out.println("<TR><TD>" + paramName + "
<TD>"); String[] paramValues = request.getParameterValues(paramName); if (paramValues.length == 1) { String paramValue = paramValues[0]; if (paramValue.length() == 0) out.print("<I>No Value</I>"); else out.print(paramValue); } else { out.println("<UL>"); for(int i=0; i<paramValues.length; i++) { out.println("<LI>" + paramValues[i]); } out.println("</UL>"); } } out.println("</TABLE>
</BODY></HTML>"); }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
运行结果:
好了,that"s all for today,thank you all. see you next time
源代码网推荐
源代码网供稿. |