JavaWeb学习笔记之9大内置对象

本文详细介绍了JSP中的九大内置对象,包括PageContext、Request、Response、Session、Application等,并探讨了它们的作用域及应用场景。此外,还展示了如何通过实例演示错误页面跳转、页面拼接等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

九大内置对象

  • PageContext 存东西
  • Request 存东西
  • Response
  • Session 存东西
  • Application【ServletContext】存东西
  • config【ServletConfig】
  • out
  • page 基本不用
  • exception 和Java异常一样

有关存东西的四大对象

	//作用域从低到高 pageContext->request->session->application寻找数据的顺序
	pageContext.setAttribute("name1","伞兵一号卢本伟");//保存的数据只在一个页面有效
    request.setAttribute("name2","伞兵二号卢本伟");//保存的数据只在一次请求中有效,请求转发会携带这个数据
    session.setAttribute("name3","伞兵三号卢本伟");//保存的数据只在一次会话中有效,从 打开浏览器到关闭浏览器
    application.setAttribute("name4","伞兵四号卢本伟");//保存的数据只在服务器中有效,从打开服务器到关闭服务器

在这里插入图片描述
使用场景:

  1. request:客户端向服务器发送请求,产生的数据,用户看完了就没用了,比如:新闻,用户看完就没有用了
  2. session:客户端向服务端发送请求,产生的数据,用户一会看完还有用,比如:购物车
  3. application:客户端向服务端发送请求,产生的数据,一个用户用完了,其他用户还可能使用,比如:聊天数据

引申:
JVM的双亲委派机制(向上寻找,向下使用):
如果要去寻找一个类,先从导入的包中进行寻找,找不到就去类加载器中找,类加载器中找不到就去RT.JAR包中去找,都找不到才是告诉Class not found

有关案例

  1. 错误页面跳转
//jsp2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--出现错误就跳到相应的界面--%>
<%@page errorPage="Error/500.jsp" %>
<%--表明这个页面是一个错误页面--%>
<%--<%@page isErrorPage="true" %>--%>

<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    int i=0;
    i=1/0;
%>
</body>
</html>

//500.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <img src="..\Img\500.png" alt="" height="100%" width="100%">
</body>
</html>

另一种方法

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

	<error-page>
		<error-code>404</error-code>
		<location>/Error/404.jsp</location>
	</error-page>
	<error-page>
		<error-code>500</error-code>
		<location>/Error/500.jsp</location>
	</error-page>
</web-app>
  1. jsp拼接页面
//jsp3.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%@include file="common/header.jsp"%>
    <h1>网页主体</h1>
<%@include file="common/footer.jsp"%>

<hr>

//jsp标签补全:ctrl+shift+enter
//jsp标签:jsp:include
拼接页面,本质还是三个
    <jsp:include page="/common/header.jsp"/>
         <h1>网页主体</h1>
    <jsp:include page="/common/footer.jsp"/>

</body>
</html>

//header.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是header</h1>
//footer.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是footer</h1>
  1. 测试不同对象的作用域
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--内置对象--%>
<%
    //作用域从低到高 pageContext->request->session->application寻找数据的顺序
    pageContext.setAttribute("name1","伞兵一号卢本伟");//保存的数据只在一个页面有效
    request.setAttribute("name2","伞兵二号卢本伟");//保存的数据只在一次请求中有效,请求转发会携带这个数据
    session.setAttribute("name3","伞兵三号卢本伟");//保存的数据只在一次会话中有效,从 打开浏览器到关闭浏览器
    //下面和上面的效果一样
    session.setAttribute("name","name");
    application.setAttribute("name4","伞兵四号卢本伟");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>

<%--脚本内部的代码会被原封不动的生成到jsp.java文件中
要求:这里的代码必须保证Java语法的正确性--%>
<%
    //从 pageContext中取出,通过寻找的方式找,pageContext->request->session->application寻找数据的顺序
    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>${name1}</h1>
<h1>${name2}</h1>
<h1>${name3}</h1>
<h1>${name4}</h1>
<h1><%=name5%></h1>

</body>
</html>

运行结果
在这里插入图片描述

  1. 两种转发
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //以下两个方法一样
    pageContext.forward("pageContextDemo01.jsp");
    //request.getRequestDispatcher("pageContextDemo01.jsp").forward(request,response);
%>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值