在本章中,我们将讨论jsp中的表单处理。当需要从浏览器将一些传递到web服务器的信息处理并最终传递到后端程序时,可能会遇到一些情况。浏览器使用两种方法将此信息传递到web服务器。 这些方法是分别是:get方法和post方法。
表格数据处理方法
现在我们来介绍表单处理中的方法。
get方法
get方法将附加的用户数据信息编码并发送到请求的页面。页面和编码信息被分隔符 - ?字符分隔开,如下 -
http://www.51frw.cn/hello?key1=value1&key2=value2
get方法是将信息从浏览器传递到web服务器的默认方法,它生成一个长字符串,出现在浏览器的地址栏框中。如果有密码或其他敏感信息传递到服务器,建议最好不要使用get方法。
get方法具有大小限制:请求字符串中最多只能有1024个字符。
该信息使用query_string标头传递,并且可以通过query_string环境变量进行访问,该变量可以使用getquerystring()和getparameter()方法来处理请求对象。
post方法
通常更可靠的将信息传递给后端程序是使用post方法。
post方法与get方法将信息打包的方式完全相同,而不是将使用?作为分隔符组成文本字符串并在url中发送。 此消息以标准输入的形式发送到后端程序,可以解析并用于处理。
jsp使用getparameter()方法处理这种类型的请求,以读取简单参数的值,而getinputstream()方法来读取客户端的二进制数据流。
使用jsp读取表单数据
jsp根据情况使用以下方法自动处理表单数据 -
- getparameter() - 调用request.getparameter()方法来获取表单参数的值。
- getparametervalues() - 如果参数出现多次并返回多个值(例如复选框),则调用此方法。
- getparameternames() - 如果想要当前请求中的所有参数的完整列表,则调用此方法。
- getinputstream() - 调用此方法读取客户端的二进制数据流。
get方法使用url示例
为了方便演示,打开eclipse创建一个项目:formprocessing 。其完整的目录结构如下所示 -
以下url将使用get方法将两个值传递给helloform程序。
http://localhost:8080/formprocessing/main.jsp?username=maxsu&email=maxsu@51frw.com
以下是jsp程序(main.jsp )处理由web浏览器请求给出的输入。这里使用getparameter()方法,这样很容易访问传递的信息 -
文件: main.jsp -
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>使用get方法读取url请求数据</title>
</head>
<body>
<h1>使用get方法读取url请求数据</h1>
<ul>
<li><p>
<b>username:</b>
<%=request.getparameter("username")%>
</p></li>
<li><p>
<b>email:</b>
<%=request.getparameter("email")%>
</p></li>
</ul>
</body>
</html> 现在打开浏览器,在地址栏中输入:http://localhost:8080/formprocessing/main.jsp?username=maxsu&email=maxsu@51frw.com 。 这将产生以下结果 -
get方法处理表单示例
以下是使用html form和提交按钮传递两个值的示例。这里使用hello.html来处理这个输入。
文件:hello.html -
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户表单处理</title>
</head>
<body>
<div style="margin: auto; width: 80%;">
<form action="main.jsp" method="get">
用户名: <input type="text" name="username"> email: <input
type="text" name="email" /> <input type="submit" value="提交" />
</form>
</div>
</body>
</html> 现在打开浏览器,在地址栏中输入:http://localhost:8080/formprocessing/hello.html 。 这将产生以下结果 -
填入信息,提交表单,看到以下结果 -
post方法处理表单示例
在上面的jsp中进行一些修改来处理get和post方法。以下是使用get或post方法处理由web浏览器给出的输入jsp程序:post.jsp。
因为上述jsp代码实现没有变化,但是传递参数的方法需要改变为post,也不是将二进制数据被传递给jsp程序。文件处理相关概念将在单独的章节中进行说明,我们需要读取二进制数据流。
文件:post.jsp -
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>使用post方读取请求数据</title>
</head>
<body>
<h1>使用post方读取表单数据</h1>
<ul>
<li><p>
<b>username:</b>
<%=request.getparameter("username")%>
</p></li>
<li><p>
<b>email:</b>
<%=request.getparameter("email")%>
</p></li>
</ul>
</body>
</html> 以下是post.html文件的内容 -
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>用户表单处理</title>
</head>
<body>
<div style="margin: auto; width: 80%;">
<form action="post.jsp" method="post">
用户名: <input type="text" name="username"> email: <input
type="text" name="email" /> <input type="submit" value="提交" />
</form>
</div>
</body>
</html> 部署运行以上项目,然后打开浏览器访问url: http://localhost:8080/formprocessing/post.html ,看到结果如下 -
分别填写用户名和email,然后提交表单,结果如下所示 -
jsp程序处理复选框数据
当表单数据需要多个选项时,可使用复选框。以下是具有两个复选框的表单的示例html代码:checkbox.html 。
文件:checkbox.html -
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>复选框数据处理示例</title>
</head>
<body>
<div style="margin: auto; width: 80%;">
<h2>选择课程(多选)</h2>
<form action="checkbox.jsp" method="post">
<input type="checkbox" name="maths" checked="checked" /> 数学 <input
type="checkbox" name="physics" /> 物理 <input type="checkbox"
name="chemistry" checked="checked" /> 化学 <input
type="submit" value="选择提交" />
</form>
</div>
</body>
</html> 文件:checkbox.jsp -
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>复选框数据处理示例</title>
</head>
<body>
<div style="margin: auto; width: 80%;">
<h2>复选框数据处理示例</h2>
<ul>
<li><p>
<b>数学:</b>
<%=request.getparameter("maths")%>
</p></li>
<li><p>
<b>物理:</b>
<%=request.getparameter("physics")%>
</p></li>
<li><p>
<b>化学:</b>
<%=request.getparameter("chemistry")%>
</p></li>
</ul>
</div>
</body>
</html> 部署运行以上项目,然后打开浏览器访问url: http://localhost:8080/formprocessing/checkbox.html ,看到结果如下 -
选择对应选项然后提交,上述程序将产生以下结果 -
读取所有表单参数
以下是使用httpservletrequest的getparameternames()方法读取所有可用的表单参数的通用示例。此方法返回一个枚举,其中包含未指定顺序的参数名称。
当有了这个枚举后,就可以使用标准方式循环枚举,使用hasmoreelements()方法来确定何时停止并使用nextelement()方法来获取每个参数名称。
文件:allformparameters.html -
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>获取所有表单数据</title>
<!-- file: allformparameters.jsp -->
</head>
<body>
<body>
<div style="margin: auto; width: 80%;">
<h2>选择课程(多选)</h2>
<form action="allformparameters.jsp" method="post">
<input type="checkbox" name="maths" checked="checked" value="math"/> 数学 <input
type="checkbox" name="physics" value="phys"/> 物理 <input type="checkbox"
name="chemistry" checked="checked" value="chem"/> 化学 <input type="submit"
value="选择提交" />
</form>
</div>
</body>
</html> 文件:allformparameters.jsp -
<%@ page language="java" contenttype="text/html; charset=utf-8"
pageencoding="utf-8"%>
<%@ page import="java.io.*,java.util.*"%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>获取所有表单数据</title>
</head>
<body>
<div style="margin: auto; width: 80%;">
<h2>获取所有表单数据</h2>
<table width="100%" border="1" align="center">
<tr bgcolor="#949494">
<th>param name</th>
<th>param value(s)</th>
</tr>
<%
enumeration paramnames = request.getparameternames();
while (paramnames.hasmoreelements()) {
string paramname = (string) paramnames.nextelement();
out.print("<tr><td>" + paramname + "</td>n");
string paramvalue = request.getparameter(paramname);
out.println("<td> " + paramvalue + "</td></tr>n");
}
%>
</table>
</div>
</body>
</html> 部署运行以上项目,然后打开浏览器访问url: http://localhost:8080/formprocessing/allformparameters.html ,看到结果如下 -
选择对应选项然后提交,上述程序将产生以下结果 -
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!