request对象:
主要用于接收客户端发送而来的请求信息,客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。
通过getParmeter方法获得属性内容:
:<%@page contentType="text/html" pageEncoding="GBK" %>
<html>
<%
request.setCharacterEncoding("GBK");
String name=request.getParameter("name");
String password=request.getParameter("password");
%>
<body>
<h3>姓名:<%=name%></h3>
<h3>密码:<%=password%></h3>
</body>
</html>
如果按照最古老的方法获得表单的内容就是所有的参数使用getParmeter()接受,
而所有的数组则使用getParmeterValues()接受,但是现在可以换用一种新的方式
getPameterNames()接受
代码用例:
<%@page contentType="text/html" pageEncoding="GBK" %>
<html>
<body>
<form action="day2.jsp" method="post">
姓名:<input type="text" name="uname"><br>
性别:<input type="radio" name="sex" value="男" CHECKED>
男
<input type="radio" name="sex" value="女">女<br>
城市: <select name="city">
<option value="北京">北京</option>
<option value="天晶">天晶</option>
</select><br>
爱好:<input type="checkbox" name="**inst" value="唱歌" >唱歌
<input type="checkbox" name="**inst" value="跳舞">跳舞
<input type="checkbox" name="**inst" value="读书">读书<br>
自我介绍:<textarea cols="30" rows="3" name="note">
</textarea><br>
<input type="hidden" name="uid" value="1"><!-- 隐藏域 -->
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
day2.jsp代码:
<%@page contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*" %>
<html>
<body>
<%
request.setCharacterEncoding("GBK");
Enumeration en=request.getParameterNames();
%>
<table border="1">
<tr>
<td>名称</td>
<td>内容</td>
</tr>
<%
while(en.hasMoreElements()){
String parmName=(String)en.nextElement();
%>
<tr>
<td><%=parmName%></td>
<td>
<%
if(parmName.startsWith("**")){
String ParamValue[] =request.getParameterValues(parmName);
for(int i=0;i<ParamValue.length;i++){
%>
<%=ParamValue[i]%>
<%
}
}
else{
%>
<%=request.getParameter(parmName) %></td>
</tr>
<%
}
%>
<%
}
%>
</table>
</body>
</html>

本文介绍了如何使用request对象处理HTTP请求中的表单数据。通过示例代码展示了如何获取单个参数、数组参数及所有参数名,并针对不同类型的输入项采用不同的方法来获取其值。
586

被折叠的 条评论
为什么被折叠?



