首页 > 经验记录 > JSP几个作用域通信对象:session application 和 pageContext ,request

JSP几个作用域通信对象:session application 和 pageContext ,request

<%@ 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>
<%
//在当前页面的上下文Map中保存命名为date1的对象
pageContext.setAttribute("date1", "hello");
//在当次请求范围内有效的作用域通讯对象
request.setAttribute("date2", "java");
//session代表当次会话对象的内存区域内保存名为date2的对象
//在一次浏览器进程范围内有效,只要浏览器不关就会一直存在,无论新开多少个窗口
session.setAttribute("date3", "world!");
//代表的是当前应用的内存区域保存名为date3的对象
//在服务器的启动和停止范围内有效,只要运行这行代码,就一直存在于内存中,只要服务器不停
application.setAttribute("date4", "233");
%>
<h2>页面作用于对象属性值:<%=pageContext.getAttribute("date1") %></h2>
<h2>请求对象属性值:<%=request.getAttribute("date2") %></h2>
<h2>session对象属性值:<%=session.getAttribute("date3") %></h2>
<h2>application对象属性值:<%=application.getAttribute("date4") %></h2>
<p><a href="demo1.jsp">demo1</a></p>
<%
//内部转发
//request.getRequestDispatcher("demo1.jsp").forward(request, response);
//重定向
//response.sendRedirect("demo1.jsp");
%>
</body>
</html>
<%@ 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> <% //在当前页面的上下文Map中保存命名为date1的对象 pageContext.setAttribute("date1", "hello"); //在当次请求范围内有效的作用域通讯对象 request.setAttribute("date2", "java"); //session代表当次会话对象的内存区域内保存名为date2的对象 //在一次浏览器进程范围内有效,只要浏览器不关就会一直存在,无论新开多少个窗口 session.setAttribute("date3", "world!"); //代表的是当前应用的内存区域保存名为date3的对象 //在服务器的启动和停止范围内有效,只要运行这行代码,就一直存在于内存中,只要服务器不停 application.setAttribute("date4", "233"); %> <h2>页面作用于对象属性值:<%=pageContext.getAttribute("date1") %></h2> <h2>请求对象属性值:<%=request.getAttribute("date2") %></h2> <h2>session对象属性值:<%=session.getAttribute("date3") %></h2> <h2>application对象属性值:<%=application.getAttribute("date4") %></h2> <p><a href="demo1.jsp">demo1</a></p> <% //内部转发 //request.getRequestDispatcher("demo1.jsp").forward(request, response); //重定向 //response.sendRedirect("demo1.jsp"); %> </body> </html>
<%@ 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>
	<%
		//在当前页面的上下文Map中保存命名为date1的对象
		pageContext.setAttribute("date1", "hello");
		//在当次请求范围内有效的作用域通讯对象
		request.setAttribute("date2", "java");
		//session代表当次会话对象的内存区域内保存名为date2的对象
		//在一次浏览器进程范围内有效,只要浏览器不关就会一直存在,无论新开多少个窗口
		session.setAttribute("date3", "world!");
		//代表的是当前应用的内存区域保存名为date3的对象
		//在服务器的启动和停止范围内有效,只要运行这行代码,就一直存在于内存中,只要服务器不停
		application.setAttribute("date4", "233");
	%>
	<h2>页面作用于对象属性值:<%=pageContext.getAttribute("date1") %></h2>
	<h2>请求对象属性值:<%=request.getAttribute("date2") %></h2>
	<h2>session对象属性值:<%=session.getAttribute("date3") %></h2>
	<h2>application对象属性值:<%=application.getAttribute("date4") %></h2>
	<p><a href="demo1.jsp">demo1</a></p>
	<%
		//内部转发
		//request.getRequestDispatcher("demo1.jsp").forward(request, response);
		//重定向
		//response.sendRedirect("demo1.jsp");
	%>
</body>
</html>

 
 
他们都是以Map的形式表现的,键值对
一般用的最多的就是session和request了

           


CAPTCHAis initialing...
EA PLAYER & 该歌单包含收费歌曲无法播放,已自动跳过 1 首单曲,请联系网站管理员更换歌单。

历史记录 [ 注意:部分数据仅限于当前浏览器 ]清空

  • 「Welcome」默认的曲单
  • 1. この櫻ノ詩の下 - 松本文紀
  • 作曲 : 松本文紀
  • 纯音乐,请欣赏
この櫻ノ詩の下 - 松本文紀
00:00/00:00