request对象常用方法总结

JSP内置对象:request对象

客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。常用方法如下:

常用方法方法介绍
String getParameter(String name)返回name指定参数的参数值
String[] getParameterValues(String name)返回包含参数name的所有值的数组
void setAttribute(String,Object)存储此请求中的属性
Object getAttribute(String name)返回指定属性的属性值
String getContentType()得到请求体的MIME类型
String getProtocol()返回请求用的协议类型及版本号
String getServerName()返回接受请求的服务器主机名
int getServerPort()返回服务器接受此请求所用的端口号
String getCharacterEncoding()返回字符编码方式
void setCharacterEncoding()设置请求的字符编码方式
int getContentLength()返回请求体的长度(以字节数)
String getRemoteAddr()返回发送此请求的客户端IP地址
String getRealPath(String path)返回一虚拟路径的真实路径
String request.getContextPath()返回上下文路径

测试代码:

  • reg.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>Insert title here</title>
    </head>
    <body>
    	<h1>用户注册</h1>
    	<form name="regForm" action="request.jsp" method="get">
    		<table>
    			<tr>
    				<td>用户名:</td>
    				<td><input type="text" name="username" /></td>
    			</tr>
    			<tr>
    				<td>爱好:</td>
    				<td><input type="checkbox" name="favorite" value="read">读书
    				<input type="checkbox" name="favorite" value="music">音乐
    				<input type="checkbox" name="favorite" value="moive">电影
    				<input type="checkbox" name="favorite" value="internet">上网</td>
    			</tr>
    			<tr>
    				<td colspan="2"><input type="submit" value="提交" /></td>
    			</tr>
    		</table>
    	</form>
    	<br>
    	<br>
    	<a href="request.jsp?username=xxx">测试URL传参数</a>
    </body>
    </html>
    
  • request.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>Insert title here</title>
    </head>
    <body>
    	<h1>request内置对象</h1>
    	<%
    		request.setAttribute("password", "123456");
    	%>
    	用户名:<%=request.getParameter("username")%>
    	<br> 
    	爱好:
    	<%
    		if (request.getParameterValues("favorite") != null) {
    			String[] fav = request.getParameterValues("favorite");
    			for (int i = 0; i < fav.length; i++) {
    				out.println(fav[i] + "&nbsp;&nbsp;");
    			}
    		}
    	%>
    	<br> 
    	密码:<%=request.getAttribute("password")%><br> 
    	请求体的mime类型:<%=request.getContentType()%><br>
    	协议类型及版本号:<%=request.getProtocol()%><br> 
    	服务器主机名:<%=request.getServerName()%><br>
    	请求的端口号:<%=request.getServerPort()%><br> 
    	字符编码方式:<%=request.getCharacterEncoding()%><br>
    	请求体的长度:<%=request.getContentLength()%><br> 
    	返回发送请求的客户端IP地址:<%=request.getRemoteAddr()%><br>
    	返回虚拟路径的真实路径:<%=request.getRealPath("request.jsp")%><br> 
    	返回上下文路径:<%=request.getContextPath()%><br>
    
    </body>
    </html>
    

代码运行结果

代码运行后浏览器中显示如下:
reg.jsprequest.jsp


end.

  • 14
    点赞
  • 74
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值