5.九大内置对象
-
PageContext——>页面上下文,用于存东西
-
Request——>请求,用于存东西
-
Response——>响应
-
Session——>会话,用于存东西
-
Application——>ServletContext,用于存东西
-
Config——>ServletConfig
-
Out——>输出
-
Page——>页面
-
Exception——>异常
作用域:
pageContext.setAttribute("name1", "Edwin1"); //保存的数据只在一个页面中有效。
request.setAttribute("name2","Edwin2"); //保存的数据,只在一次请求中有效,请求转发会携带这个数据。
session.setAttribute("name3","Edwin3"); //保存的数据,只在一次会话中有效,从打开浏览器到关闭浏览器。
application.setAttribute("name4","Edwin4"); //保存的数据,只在服务器中有效,从打开服务器到关闭服务器。
request:请求,客户端向服务器发送的请求,产出的数据,用户使用完毕后,就没用了。Eg:新闻,小网站。
session:客户端向服务器发送的请求,产出的数据,用户使用完毕后,还会继续用。Eg:购物车。
application:客户端向服务器发送的请求,产出的数据,一个用户使用完毕后,其他用户可能也会使用。Eg:聊天数据。
代码举例:
JspContextDemo1:
<%--
Created by IntelliJ IDEA.
User: 元
Date: 2020.6.9
Time: 上午 9:16
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>PageContextDemo1</title>
</head>
<body>
<%--内置对象--%>
<%
pageContext.setAttribute("name1", "Edwin1"); //保存的数据只在一个页面中有效。
request.setAttribute("name2","Edwin2"); //保存的数据,只在一次请求中有效,请求转发会携带这个数据。
session.setAttribute("name3","Edwin3"); //保存的数据,只在一次会话中有效,从打开浏览器到关闭浏览器。
application.setAttribute("name4","Edwin4"); //保存的数据,只在服务器中有效,从打开服务器到关闭服务器。
%>
<%--脚本片段中的代码,会被原封不动地生成到Jsp.java中
此处的代码,务必保证符合Java语法规则。--%>
<%
/*通过PageContext取出我们的保存的值
作用域是从底层到高层:page——>request——>session——>application
其中有一个JVM,双亲委派机制
*/
String name1 = (String)pageContext.findAttribute("name1");
String name2 = (String)pageContext.findAttribute("name2");
String name3 = (String)pageContext.findAttribute("name3");
String name4 = (String)pageContext.findAttribute("name4");
String name5 = (String)pageContext.findAttribute("name5");
%>
<%--使用El表达式输出————>${}--%>
<h1>取出来的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<%--对比El表达式与常规输出:--%>
<h3>${name5}</h3>
<hr>
<h3><%=name5%></h3>
</body>
</html>
JspContextDemo3:
<%--
Created by IntelliJ IDEA.
User: 元
Date: 2020.6.9
Time: 上午 10:37
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>PageContextDemo03</title>
</head>
<body>
<%--跳槽--%>
<%
pageContext.forward("/index.jsp");
// 等价于: request.getRequestDispatcher("/index.jsp").forward(request,response);
%>
</body>
</html>
PageDemo02:
<%--
Created by IntelliJ IDEA.
User: 元
Date: 2020.6.9
Time: 上午 9:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>PageDemo02</title>
</head>
<body>
<%--脚本片段中的代码,会被原封不动地生成到Jsp.java中
此处的代码,务必保证符合Java语法规则。--%>
<%
/*通过PageContext取出我们的保存的值*/
String name1 = (String)pageContext.findAttribute("name1");
String name2 = (String)pageContext.findAttribute("name2");
String name3 = (String)pageContext.findAttribute("name3");
String name4 = (String)pageContext.findAttribute("name4");
String name5 = (String)pageContext.findAttribute("name5");
%>
<%--使用El表达式输出————>${}--%>
<h1>取出来的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<%--对比El表达式与常规输出:--%>
<h3>${name5}</h3>
<hr>
<h3><%=name5%></h3>
</body>
</html>
《成功的花》——冰心
成功的花,
人们只惊羡她现时的明艳!
然而当初她的芽儿,
浸透了奋斗的泪泉,
洒遍了牺牲的血雨!
参考文献
《【狂神说Java】JavaWeb入门到实战》
2020.06.09