jsp中的四種對象範圍

來源:互聯網
上載者:User

標籤:

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中的四種對象範圍

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.