九大内置对象
对象名 | 类型 | 说明 | 四大作用域 |
---|---|---|---|
request | javax.servlet.http.HttpServletRequest | 请求对象,可以获取请求信息 | 一次请求 重定向或跨越超过一个页面 则丢失 |
response | javax.servlet.http.HttpServletResponse | 响应对象。可以设置响应信息 | 无 |
session | javax.servlet.http.HttpSession | 由session=“true”开关 可以获取会话信息。 | 一次会话(浏览器关闭) |
application | javax.servlet.ServletContext | application对象ServletContext对象实例,可以获取整个工程的一些信息。 | 服务器的关闭既销毁 |
config | javax.servlet.ServletConfig | ServletConfig 对象实例,可以获取 Servlet的配置信息 | 无 |
exception | java.lang.Throwable | 异常对象只有在jsp页面的page 指令中设置isErrorPage=“false”开关 | 无 |
out | javax.servlet.jsp.JspWriter | javax.servlet.jsp.JspWriter | 无 |
pageContext | javax.servlet.jsp.PageContext | 当前页面上下文对象 | 自身作用域 只能在当前页面 |
page | java.lang.Object当前对象this | 当前servlet实例 | 无 |
1.0四大作用域
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>九大内置对象</title>
</head>
<body>
<%--九大内置对象
request 请求 ---> _jspService 提供
response 响应 ---> _jspService 提供
session ---> _jspService 自动创建
application ---> _jspService 自动创建
config ---> _jspService 自动创建
exception ---> _jspService 指令创建 isErrorPage="true" 创建
out ---> _jspService 自动创建
pageContext ---> _jspService 自动创建
page ---> _jspService 自动创建 当前jsp对象 page = this;
--%>
<%
//4个作用域
//pageContext 自身作用域 仅能作用在当前JSP 不能跨请求
pageContext.setAttribute("name0","张三0");
//request 一次请求 重定向或跨越超过一个页面 则丢失
request.setAttribute("name1","张三1");
//session 一次会话(浏览器关闭)
session.setAttribute("name2","张三2");
//application 服务器的关闭既销毁
application.setAttribute("name3","张三3");
//转发
//request.getRequestDispatcher("作用域获取.jsp").forward(request,response);
// response.sendRedirect("作用域获取.jsp");
%>
<a href="作用域获取.jsp">跳转</a>
</body>
</html>
作用域.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>作用域获取页面</title>
</head>
<body>
<%--pageContext 获取--%>
pageContext作用域获取:<%=pageContext.getAttribute("name0")%><br/>
<%--request 获取--%>
request作用域获取:<%=request.getAttribute("name1")%><br/>
<%--session 获取--%>
session作用域获取:<%=session.getAttribute("name2")%><br/>
<%--application 获取--%>
application作用域获取:<%=application.getAttribute("name3")%>
</body>
</html>
2.0 jsp的out和getWriter()方法的区别
1 out是JspWriter类型,getWriter()是PrintWriter类型
2 out输出到缓冲区中,没有写到response中,getWriter()直接写到response中。
3 out一般用在jsp中,getWriter()用在Servlet中
getWriter 加载更快
如果想out 加载更快的话可以out.flush()
3.0 pageContext 对象
3.1pageContext 作用
1.作为容器使用
2.代表页面上下文 获取其他八个内置对象
3.2作为容器的使用
<html>
<head>
<title>pageContext作为容器的使用</title>
</head>
<body>
<%--赋值--%>
<%
//用pageContext存在其他作用域里
//作用域值从大到小
// 1 pageContext
// 2 request
// 3 session
// 4 application
//pageContext 自身作用域
pageContext.setAttribute("user", "张三", PageContext.PAGE_SCOPE);
//request 作用域
pageContext.setAttribute("password", "123456", PageContext.REQUEST_SCOPE);
//session 作用域
pageContext.setAttribute("age", "20", PageContext.SESSION_SCOPE);
//application 作用域
pageContext.setAttribute("sex", "男", PageContext.APPLICATION_SCOPE);
%>
<%--取值--%>
我是PageContext自身作用域的值:<%=pageContext.getAttribute("user",1)%><br/>
我是request作用域的值:<%=pageContext.getAttribute("password",PageContext.REQUEST_SCOPE)%><br/>
我session作用域的值:<%=pageContext.getAttribute("age",PageContext.SESSION_SCOPE)%><br/>
我application作用域的值:<%=pageContext.getAttribute("sex",4)%><br/>
<%--四大作用域里查询--%><br/>
全局查询的值:<%=pageContext.findAttribute("password")%>
<%--如果存在重复的值则按作用域的范围从小到大获取 先获取到则输出--%>
</body>
</html>
```
## 3.3代表上下文 获取其他八个内置对象
```typescript
<html>
<head>
<title>pageContext作为容器的使用</title>
</head>
<body>
<%--赋值--%>
<%
//用pageContext存在其他作用域里
//作用域值从大到小
// 1 pageContext
// 2 request
// 3 session
// 4 application
//pageContext 自身作用域
pageContext.setAttribute("user", "张三", PageContext.PAGE_SCOPE);
//request 作用域
pageContext.setAttribute("password", "123456", PageContext.REQUEST_SCOPE);
//session 作用域
pageContext.setAttribute("age", "20", PageContext.SESSION_SCOPE);
//application 作用域
pageContext.setAttribute("sex", "男", PageContext.APPLICATION_SCOPE);
%>
<%--取值--%>
我是PageContext自身作用域的值:<%=pageContext.getAttribute("user",1)%><br/>
我是request作用域的值:<%=pageContext.getAttribute("password",PageContext.REQUEST_SCOPE)%><br/>
我session作用域的值:<%=pageContext.getAttribute("age",PageContext.SESSION_SCOPE)%><br/>
我application作用域的值:<%=pageContext.getAttribute("sex",4)%><br/>
<%--四大作用域里查询--%><br/>
全局查询的值:<%=pageContext.findAttribute("password")%>
<%--如果存在重复的值则按作用域的范围从小到大获取 先获取到则输出--%>
</body>
</html>
```