1、什么是内置对象?
在Servlet的service方法中有九个局部变量,可以直接拿来使用
1.jsp代码:
<%@page contentType="text/html; charset=UTF-8" isErrorPage="true"%>
<%--
关于JSP中的九大内置对象
1、什么是内置对象?
在Servlet的service方法中有九个局部变量,可以直接拿来使用
内置对象的名称 对应的完整类名
----------------------------------------------------------------------------------
pageContext javax.servlet.jsp.PageContext
request javax.servlet.http.HttpServletRequest
session javax.servlet.http.HttpSession
application javax.servlet.ServletContext
response javax.servlet.http.HttpServletResponse
out javax.servlet.jsp.JspWriter
config javax.servlet.ServletConfig
exception java.lang.Throwable
page java.lang.Object【this】<几乎不用,就是java.lang.Object page=this;和pageContext无关>
--%>
pageContext = <%=pageContext %><br>
request = <%=request %><br>
session = <%=session %><br>
application = <%=application %><br>
response = <%=response %><br>
out = <%=out %><br>
config = <%=config %><br>
exception = <%=exception %><br>
page = <%=page %><br>
2、JSP+Servlet共四个范围对象
1.jsp代码:
<%@page contentType="text/html; charset=UTF-8"%>
<%--
JSP+Servlet共四个范围对象:
pageContext javax.servlet.jsp.PageContext 页面范围<只能在同一个jsp页面当中传递数据>
request javax.servlet.http.HttpServletRequest 请求范围<只能在同一次请求当中传递数据>
session javax.servlet.http.HttpSession 会话范围<可以在多次请求之间传递数据,但是这多次请求属于同一个会话>
application javax.servlet.ServletContext 应用范围<可以在多个会话之间传递数据>
pageContext < request < session < application
优先使用小范围
--%>
<%
pageContext.setAttribute("pageContext", "pageContextData");
request.setAttribute("request", "requestData");
session.setAttribute("session", "sessionData");
application.setAttribute("application", "applicationData");
%>
<%-- 第一次请求:验证同页面范围对象是否可以获取 --%>
<%--
<%=pageContext.getAttribute("pageContext") %><br>
<%=request.getAttribute("request") %><br>
<%=session.getAttribute("session") %><br>
<%=application.getAttribute("application") %><br>
--%>
<%-- 第二次请求:验证不同页面转发时范围对象是否可以获取 --%>
<%--
<jsp:forward page="/2.jsp"/>
--%>
<%-- 第三次请求:重定向是两次请求,验证不同页面转发时范围对象是否可以获取
1.输入本页面地址调用,不关闭浏览器,输入重定向目标页地址调用看能否获取到数据
2.不关闭服务器,输入重定向目标页地址调用看能否获取到数据
--%>
<%
response.sendRedirect(request.getContextPath() + "/2.jsp");
%>
<%-- 第四次请求:关闭tomcat重启后输入重定向目标页地址调用看能否获取到数据 --%>
2.jsp代码:
<%@page contentType="text/html; charset=UTF-8"%>
<%=pageContext.getAttribute("pageContext") %><br>
<%=request.getAttribute("request") %><br>
<%=session.getAttribute("session") %><br>
<%=application.getAttribute("application") %><br>
2、pageContext对象代表jsp页面上下文对象。通过pageContext对象可以获取整个JSP页面环境中的所有数据;
使用pageContext对象往request,session,application对象中存储数据。
向request范围存储数据:
pageContext.setAttribute("username", "lili", PageContext.REQUEST_SCOPE);
向session范围存储数据:
pageContext.setAttribute("username", "lili", PageContext.SESSION_SCOPE);
向application范围存储数据:
pageContext.setAttribute("username", "lili", PageContext.APPLICATION_SCOPE);
1.jsp的代码:
<%@page contentType="text/html; charset=UTF-8"%>
<%--
关于内置对象pageContext
内置对象名称:pageContext
全类名:javax.servlet.jsp.PageContext
pageContext是JSP中提供的,在Servlet中没有。
pageContext被称为页面上下文对象
pageContext页面上下文对象中包含了整个jsp页面当中所有的元素。【当前jsp页面中所有的元素都放在pageContext中了】
通过pageContext可以获取到request、session.....
--%>
<%=pageContext.getRequest()%><br>
<%=pageContext.getResponse() %><br>
<%=pageContext.getSession() %><br>
<%=pageContext.getServletContext() %><br>
<%=pageContext.getServletConfig()%><br>
<%
pageContext.setAttribute("username", "request", PageContext.REQUEST_SCOPE);
pageContext.setAttribute("username", "session", PageContext.SESSION_SCOPE);
pageContext.setAttribute("username", "application", PageContext.APPLICATION_SCOPE);
%>
<%=request.getAttribute("username") %><br>
<%=session.getAttribute("username") %><br>
<%=application.getAttribute("username") %><br>