码迷,mamicode.com
首页 > Web开发 > 详细

jsp中的四种对象作用域

时间:2015-09-21 01:34:31      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

page:当前页面,也就是只要挑到别的页面就失效了,可以近似理解为java的this对象

request:一次会话,简单的理解就是一次请求范围内有效,例如如果通过forward方式跳转,则forward目标页面仍然可以拿到request中的属性值,再例如如果通过redirect方式进行页面跳转,由于redirect相当于重新发出的请求,request中的值会消失

session:浏览器进程,只要当前页面没有被关闭(没有被浏览器强制清除),不管怎么跳转都是有效的(也可以说是存在一个会话周期中)

application:服务器,只要服务器没有重启(没有被程序强制清除),数据就有效

pageScope.jsp

 

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ page import="java.util.*" %>

<!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>

<body>

<%

//设置两个page范围的数据key->value

pageContext.setAttribute("name","page_barry");

pageContext.setAttribute("age",21);

%>

<%

//取值

String name=(String)pageContext.getAttribute("name");

int age=(Integer)pageContext.getAttribute("age");

%>

<font>姓 名:<%=name %></font>

<font>年 龄:<%=age %></font>

 

</body>

</html>

 

pageContext01.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ page import="java.util.*" %>

<%@ page errorPage="error.jsp" %>

<!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>

<%

pageContext.setAttribute("name0","pageInfo");

request.setAttribute("name1","requestInfo");

session.setAttribute("name2","sessionInfo");

application.setAttribute("name3","applicationInfo");

 

out.println("使用pageContext<br/>");

out.println("page中的属性值:"+pageContext.getAttribute("name0")+"<br/>");

out.println("request中的属性值:"+pageContext.getRequest().getAttribute("name1")+"<br/>");

out.println("session中的属性值:"+pageContext.getSession().getAttribute("name2")+"<br/>");

out.println("application中的属性值:"+pageContext.getServletContext().getAttribute("name3")+"<br/>");

%>

</body>

</html>

 

pageScope_2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ page import="java.util.*" %>

<!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>

<body>

<%

//设置两个page范围的数据key->value

pageContext.setAttribute("name","page_barry");

pageContext.setAttribute("age",21);

%>

 

<jsp:forward page="pageTarget_2.jsp"></jsp:forward>

</body>

</html>

 

pageTarget_2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ page import="java.util.*" %>

<!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>

<body>

 

<%

//取值

String name=(String)pageContext.getAttribute("name");

int age=(Integer)pageContext.getAttribute("age");

%>

<font>姓 名:<%=name %></font>

<font>年 龄:<%=age %></font>

 

</body>

</html>

 

requestScope.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>

<%

//设置两个request范围的数据key->value

request.setAttribute("name","request_barry");

request.setAttribute("age",12);

%>

 

<jsp:forward page="requestTarget.jsp"></jsp:forward>

</body>

</html>

requestTarget.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ page import="java.util.*" %>

<!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>

<%

//取值

String name=(String)request.getAttribute("name");

int age=(Integer)request.getAttribute("age");

//获取头信息

Enumeration enu=request.getHeaderNames();

while(enu.hasMoreElements()){

String headerName=(String)enu.nextElement();

String headerValue=request.getHeader(headerName);

%>

<h4><%=headerName %>&nbsp;<%=headerValue %></h4>

<%

}

%>

<font>姓名<%=name %></font><br>

<font>年龄<%=age %></font>

</body>

</html>

 

requestScope_2.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>

<%

//设置两个request范围的数据key->value

request.setAttribute("name","request_barry");

request.setAttribute("age",12);

%>

 

<%response.sendRedirect("requestTarget_2"); %>

</body>

</html>

requestTarget_2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"

    pageEncoding="utf-8"%>

<%@ page import="java.util.*" %>

<!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>

<%

//取值

String name=(String)request.getAttribute("name");

int age=(Integer)request.getAttribute("age");

//获取头信息

Enumeration enu=request.getHeaderNames();

while(enu.hasMoreElements()){

String headerName=(String)enu.nextElement();

String headerValue=request.getHeader(headerName);

%>

<h4><%=headerName %>&nbsp;<%=headerValue %></h4>

<%

}

%>

<font>姓名<%=name %></font><br>

<font>年龄<%=age %></font>

</body>

</html>

 

sessionScope.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>

<%

//设置两个session范围的数据key->value

session.setAttribute("name","session_barry");

session.setAttribute("age",12);

%>

 

<h1>session值设置完毕!</h1>

 

 

</body>

</html>

 

sessionTarget.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>

<%

String name=(String)session.getAttribute("name");

Integer age=(Integer)session.getAttribute("age");

%>

<font>姓名<%=name %></font><br>

<font>年龄<%=age %></font>

</body>

</html>

 

applicationScope.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>

<%

//设置两个application范围的数据key->value

application.setAttribute("name","application_barry");

application.setAttribute("age",12);

%>

 

<h1>application值设置完毕!</h1>

 

 

</body>

</html>

 

applicationTarget.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>

<%

//取值

String name=(String)application.getAttribute("name");

int age=(Integer)application.getAttribute("age");

%>

<font>姓名<%=name %></font><br>

<font>年龄<%=age %></font>

</body>

</html>

jsp中的四种对象作用域

标签:

原文地址:http://www.cnblogs.com/daochong/p/4824811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!