JSP基础语法-九大内置对象

本文介绍了JSP的基础语法,特别是九大内置对象的使用。这些内置对象在Servlet的service方法中作为局部变量可以直接使用。内容包括JSP的四个范围对象——请求、会话、应用范围,以及如何利用pageContext对象在不同范围内存储数据的示例。
摘要由CSDN通过智能技术生成

JSP基础语法

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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值