Day 30
2月9日,年差不多过完了,单量也慢慢恢复了,开始变得不想上班了。。。。
- JSP内置对象简介
JSP提供了9个内置对象,这些对象无需声明即可直接在JSP页面中使用。它们分别是:
request:表示客户端的请求,类型为HttpServletRequest。
response:表示服务器端的响应,类型为HttpServletResponse。
session:表示用户会话,类型为HttpSession。
application:表示Servlet上下文,类型为ServletContext。
out:用于向客户端输出内容,类型为JspWriter。
pageContext:提供对页面属性的访问,类型为PageContext。
page:表示当前JSP页面本身,类型为Object。
config:表示Servlet配置信息,类型为ServletConfig。
exception:表示异常对象,仅在错误页面中使用,类型为Throwable。
- JSP作用域
JSP中的对象可以存储在不同的作用域中,作用域决定了对象的生命周期和可见性。JSP的四种作用域为:
page:仅在当前页面有效。
request:在一次请求中有效,可以跨多个页面(通过forward或include)。
session:在一次用户会话中有效。
application:在整个Web应用的生命周期内有效。
- 示例代码:使用内置对象和作用域
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>JSP内置对象示例</title>
</head>
<body>
<h1>JSP内置对象与作用域</h1>
<%
// 设置不同作用域的属性
pageContext.setAttribute("pageScope", "Page Scope Value", PageContext.PAGE_SCOPE);
request.setAttribute("requestScope", "Request Scope Value");
session.setAttribute("sessionScope", "Session Scope Value");
application.setAttribute("applicationScope", "Application Scope Value");
%>
<p>Page Scope: <%= pageContext.getAttribute("pageScope") %></p>
<p>Request Scope: <%= request.getAttribute("requestScope") %></p>
<p>Session Scope: <%= session.getAttribute("sessionScope") %></p>
<p>Application Scope: <%= application.getAttribute("applicationScope") %></p>
</body>
</html>
- 运行结果
页面将显示各个作用域中存储的值:
Page Scope: Page Scope Value
Request Scope: Request Scope Value
Session Scope: Session Scope Value
Application Scope: Application Scope Value
浙公网安备 33010602011771号