jsp的request对象实例详解
一 request对象定义
客户端的请求信息被封装在request对象中,通过它才能了解客户的需求,然后做出响应。它是httpservletrequest类的实例。request对象具有请求域,即完成客户端的请求之前,该对象一直有效。
二 request对象方法
三 实例
<%@ page language="java" import="java.util.*" contenttype="text/html; charset=utf-8"%>
<%
string path = request.getcontextpath();
string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/";
%>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en">
<html>
<head>
<base href="<%=basepath%>" rel="external nofollow" >
<title>my jsp 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css" rel="external nofollow" >
-->
</head>
<body>
<h1>request内置对象</h1>
<%
request.setcharacterencoding("utf-8"); //解决中文乱码问题,无法解决url传递中文出现的乱码问题。
request.setattribute("password", "123456");
%>
用户名:<%=request.getparameter("username") %><br>
爱好 :<%
if(request.getparametervalues("favorite")!=null)
{
string[] favorites = request.getparametervalues("favorite");
for(int i=0;i<favorites.length;i++)
{
out.println(favorites[i]+" ");
}
}
%> <br>
密码:<%=request.getattribute("password") %><br>
请求体的mime类型:<%=request.getcontenttype() %><br>
协议类型及版本号: <%=request.getprotocol() %><br>
服务器主机名 :<%=request.getservername() %><br>
服务器端口号:<%=request.getserverport() %><br>
请求文件的长度 :<%=request.getcontentlength() %><br>
请求客户端的ip地址:<%=request.getremoteaddr() %><br>
请求的真实路径:<%=request.getrealpath("request.jsp") %><br>
请求的上下文路径:<%=request.getcontextpath() %><br>
</body>
</html>
四 运行效果
五 小知识点
1、解决url传递中文参数乱码问题
修改d:apache-tomcat-7.0.81conf中的server.xml
<connector port="8888" protocol="http/1.1"
connectiontimeout="20000"
redirectport="8443" uriencoding="utf-8"/>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!