JSP中Request属性范围

JSP属性范围,通过以下几个测试代码来学习request属性的范围


测试一(JSP动态指令方式传参):

测试内容:

<jsp:param .../>添加参数,通过<jsp:forward page="...">来实现服务器端跳转,以此来测试request属性的范围:

页面RequestScopeDemo.jsp (添加info1参数) —> 通过<jsp:forward page="RequestScopeDemo_1.jsp">跳转

页面RequestScopeDemo_1.jsp (添加info2参数) —> 通过<jsp:forward page="RequestScopeDemo_2.jsp">跳转

页面RequestScopeDemo_2.jsp (request对象中取到了info1、info2两个参数的值)

CODE:

RequestScopeDemo.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 -  Page 1</title></head>
<body>
	<h1>跳转前的页面:page 1</h1>
	<%
		//想输入一个字符串“<jsp:param ... >”不能直接输出必须通过转义输出
		String info1="page 1 页面中添加(<jsp:param ... />)的参数 !";
		request.setCharacterEncoding("utf-8");
	%>
	<jsp:forward page="RequestScopeDemo_1.jsp">
		<jsp:param name="info1" value="<%=info1%>" />
	</jsp:forward>
</body>
</html>

RequestScopeDemo1.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 - page2</title></head>
<body>
	<h1>跳转中页面:page 2</h1>
	<%
		String info2="page 2 页面中添加(<jsp:param ... />)的参数 !";	
	%>
	info参数:<%=request.getParameter("info2")%><br/>
	<jsp:forward page="RequestScopeDemo_2.jsp">
		<jsp:param name="info2" value="<%=info2%>" />
	</jsp:forward>
</body>
</html>

RequestScopeDemo2.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 - page3</title></head>
<body>
	<h1>跳转到的页面:page 3</h1>
	<%
		/*request对象的源头设置了编码,所以这里就不需要设置啦*/
		//request.setCharacterEncoding("utf-8");		
	%>
	<h2>Page1页面中传递过来的info1参数:<%=request.getParameter("info1")%></h2><br/>
	<h2>Page2页面中传递过来的info2参数:<%=request.getParameter("info2")%></h2><br/>
	<h2>request.getCharacterEncoding()的值:<%=request.getCharacterEncoding()%></h2>
</body>
</html>

测试结果:


测试二(request.setAttribute()封装属性方式传参):

测试内容:

request.setAttribute()方法封装参数,通过<jsp:forward page="...">来实现服务器端跳转,以此来测试request属性的范围:

页面RequestScopeDemo_t.jsp (添加info1参数) —> 通过<jsp:forward page="RequestScopeDemo_t1.jsp">跳转

页面RequestScopeDemo_t1.jsp (添加info2参数) —> 通过<jsp:forward page="RequestScopeDemo_t2.jsp">跳转

页面RequestScopeDemo_t1.jsp (request对象中取到了info1、info2两个参数的值)

CODE:

RequestScopeDemo_t.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 -  Page 1</title></head>
<body>
	<h1>跳转前的页面:page 1</h1>
	<%
		request.setCharacterEncoding("utf-8");
		request.setAttribute("info1","Page1中添加(setAttribute)的参数");
	%>
	<jsp:forward page="RequestScopeDemo_t1.jsp" />
</body>
</html>

RequestScopeDemo_t1.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 - page2</title></head>
<body>
	<h1>跳转中页面:page 2</h1>
	<%
		request.setAttribute("info2","Page2中添加(setAttribute)的参数");
	%>
	跳转中的页面,info1参数:<%=request.getAttribute("info1")%><br/>
	<jsp:forward page="RequestScopeDemo_t2.jsp" />
</body>
</html>

RequestScopeDemo_t2.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 - page3</title></head>
<body>
	<h1>跳转到的页面:page 3</h1>
	<h2>第一个页面,info1参数:<%=request.getAttribute("info1")%></h2><br/>
	<h2>第二个页面,info2参数:<%=request.getAttribute("info2")%></h2><br/>
	<h2>request.getCharacterEncoding()的值:<%=request.getCharacterEncoding()%></h2>
</body>
</html>

测试结果:


测试三(URL地址方式传参):

测试内容:

URL地址方式添加参数,通过超链接<a href=".." ..>来实现服客户端跳转,以此来测试request属性的范围:

页面RequestScopeDemo_1.jsp 页中通过<a href="RequestScopeDemo_2.jsp?info1=Page1页面的参数">链接到下一个页面</a>跳转

页面RequestScopeDemo_2.jsp 页中通过<a href="RequestScopeDemo_3.jsp?info2=Page1页面的参数">链接到下一个页面</a>跳转

页面RequestScopeDemo_2.jsp(request对象中取到了info2的参数值)

CODE:

RequestScopeDemo_1.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Qequest属性范围 -  Page 1</title></head>
<body>
	<h1>跳转前的页面:page 1</h1>
	<%
		request.setCharacterEncoding("utf-8");
	%>
	<a href="RequestScopeDemo_2.jsp?info1=Page1页面的参数">链接到下一个页面</a>
</body>
</html>

RequestScopeDemo_2.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 - page2</title></head>
<body>
	<h1>跳转中页面:page 2</h1>
	<%
		request.setAttribute("info2","Page2中的参数");
	%>
	跳转中的页面,info1参数:<%=request.getParameter("info1")%><br/>
	<a href="RequestScopeDemo_3.jsp?info2=Page2页面的参数">链接到下一个页面</a>
</body>
</html>

RequestScopeDemo_3.jsp :

<%@ page contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head><title>Request属性范围 - page3</title></head>
<body>
	<h1>跳转到的页面:page 3</h1>
	<h2>第一个页面,info1参数:<%=request.getParameter("info1")%></h2><br/>
	<h2>第二个页面,info2参数:<%=request.getParameter("info2")%></h2><br/>
	<h2>request.getCharacterEncoding()的值:<%=request.getCharacterEncoding()%></h2>
</body>
</html>

测试结果:



总结:request属性在服务器端跳转时request相当于是一个消息信息在各个页面中传递,各个页面是共享同一个request对象。


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。