您的位置:首页 > 产品设计 > UI/UE

02_04 JSP内置对象之request

2015-08-09 17:28 726 查看
request属性
主要作用是接收客户端发送过来的请求信息,如参数等。
Request是javax.servlet.http.HttpServletRequest接口的实例化对象。常用方法有:
No
方法
类型
描述
1
Public String getParameter(String name)
普通
取得客户端发来的请求参数内容
2
Public String[] getParameter(String name)
普通
取得客户端发来的一组请求参数内容
3
Public Enumeraton getParameterName()
普通
取得全部请求参数的名称
4
Pulbic String getRemoteAddr()
普通
得到客户端IP地址
5
Void setCharacterEncoding(String env)
普通
设置统一的请求编码
6
Public ovid isUserInRole(Sting role)
普通
进行用户身份的验证
7
等等
普通
1.乱码解决
request.setCharacterEncoding("GBK"); //设置统一编码

<!doctype html>
<html>
<head>
<meta charset="GBK">
<title>request</title>
</head>
<body>
<form action="request_demo01.jsp" method="post">
请输入信息:<input type="text" name="info" >
<input type="submit" value="提交" >
</form>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>request</title>
</head>
<body>
<%
//设置统一编码
request.setCharacterEncoding("GBK");
//接收表单提交的参数
String content = request.getParameter("info");
%>
<h2><%=content%></h2>
</body>
</html>
注意:
如果是编辑器不支持中文则运行时也是不会显示中文。如直接安装的sublime、DW都不是直接支持中文的。EditPlus编辑肯定支持中文,但写代码速度慢,比较适合初学者。
2.接收请求参数
2.1单一参数可使用getParameter()方法接收;而一组参数要使用getParameterValues()方法接收。若一组参数使用getParameter()接收只是接收和一个选中参数。例
<!doctype html>
<html>
<head>
<meta charset="GBK">
<title>udbful</title>
</head>
<body>
<form action="request_demo02.jsp" method="post">
姓名:<input type="text" name="username"><br>
兴趣:<input type="checkbox" name="inst" value="唱歌">唱歌
<input type="checkbox" name="inst" value="跳舞">跳舞
<input type="checkbox" name="inst" value="游泳">游泳
<input type="checkbox" name="inst" value="看书">看书
<input type="checkbox" name="inst" value="上网">上网
<input type="hidden" name="id" value="3">		<!--定义隐藏域-->
<br><input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>include</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String id = request.getParameter("id");
String name = request.getParameter("username");
String inst[] = request.getParameterValues("inst");
%>

<h2>编号:<%=id%></h2>
<h2>姓名:<%=name%></h2>

<%
if(null != inst){
%>
<h3>兴趣:
<%
for(int x=0; x<inst.length; x++){
%>
<%=inst[x]%>、
<%
}
%>
</h3>
<%
}
%>
</body>
</html>
2.2URL地址重写(通过地址栏传递参数)
动态页面地址?参数名称1=参数内容1&参数名称2=参数内容2&……

<%@page contentType="text/html" pageEncoding="GBK"%>
<html>
<head>
<title>include</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
String param1 = request.getParameter("name");
String param2 =request.getParameter("password");
%>

<h2>用户名:<%=param1%></h2>
<h2>密码:<%=param2%></h2>

</body>
</html>
在浏览器中输出下面地址 http://localhost/newudbful/Chapter6/02request/request_demo03.jsp?name=zzg&password=123 2.3关于表单提交方式post与get
两者有个明显的区别在于,post提交表单,提交的内容不会显示在地址栏上;而get提交时,提交的内容会以地址重写方式显示在地址栏上。一般情况表单提交方式用post比较多。一般而言当提交表单数据较大时则是用post方式提交。 2.4使用getParameterNames()方法传递参数,此方式一般用于表单动态变化的情况,如购物车网站程序开发中。

<!doctype html>
<html>
<head>
<meta charset="GBK">
<title>udbful</title>
</head>
<body>
<form action="request_demo04.jsp" method="post">
姓名:<input type="text" name="username"><br>
性别:<input type="radio" name="sex" value="男" CHECKED >男
<input type="radio" name="sex" value="女" >女<br>
城市:<select name="city">
<option value="北京">北京</option>
<option value="上海">上海</option>
<option value="南宁">南宁</option>
<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="游泳">游泳
<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="id" value="1"><br>		<!--定义隐藏域-->
<input type="submit" value="提交">
<input type="reset" value="重置">
</form>
</body>
</html>
<%@ page contentType="text/html" pageEncoding="GBK"%>
<%@ page import="java.util.*"%>
<html>
<head>
<title>include</title>
</head>
<body>
<%
request.setCharacterEncoding("GBK");
%>
<center>
<table border="1">
<tr>
<td>参数名称</td>
<td>参数内容</td>
</tr>
<%
//接收全部请求参数据的名称
Enumeration enu = request.getParameterNames();
//依次取出每一个参数名称
while(enu.hasMoreElements()){
String paramName = (String) enu.nextElement();
%>
<tr>
<td><%=paramName%></td>
<td>
<%
if(paramName.startsWith("**")){
String paramValue[] = request.getParameterValues(paramName);
for(int x=0; x<paramValue.length; x++){
%>
<%=paramValue[x]%>、
<%
}
}else{
String paramValue = request.getParameter(paramName);
%>
<%=paramValue%>
<%

}
%>
</td>
</tr>
<%
}
%>
</table>
</center>
</body>
</html>
3.显示全部的头信息(略)
使用getHeaderNames()/getHeader()方法
4.角色验证(略)
使用isUserInRole()方法及配置文件
5.IP地址、路径信息、提交方式取得
例1
<%@page contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*"%>
<html>
<head>
<title>include</title>
</head>
<body>
<%
//取得提交方式
String method = request.getMethod();
//取得客户端IP地址
String ip = request.getRemoteAddr();
//取得访问路径
String path = request.getServletPath();
//取得上下文资源名称
String contextPath =request.getContextPath();
%>
<h2>请求方式:<%=method%></h2>
<h2>IP地址:<%=ip%></h2>
<h2>访问路径:<%=path%></h2>
<h2>上下文名称:<%=contextPath%></h2>
</body>
</html>

例2
<%@page contentType="text/html" pageEncoding="GBK"%>
<%@page import="java.util.*"%>
<html>
<head>
<title>include</title>
</head>
<body>
<imgsrc="../images/udbful.jpg">
<img src="<%=request.getContextPath()%>/images/udbful.jpg";
</body>
</html>
<imgsrc="<%=request.getContextPath()%>/images/udbful.jpg";使用更加安全而不是用../

以上内容参考JAVAWEB开发实战经典(名师讲坛)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  request