004_Jsp九大内置对象
一. Jsp的9大內置對象
1. 所謂內置對象, 就是在jsp頁面中不用創建, 可以直接使用的對象。
2. Jsp的9大內置對象如下:
2.1. javax.servlet.http.HttpServletRequest request
2.2. javax.servlet.http.HttpServletResponse response
2.3. javax.servlet.jsp.PageContext pageContext;
2.4. javax.servlet.http.HttpSession session;
2.5. java.lang.Throwable exception =
org.apache.jasper.runtime.JspRuntimeLibrary.getThrowable(request);
2.6. javax.servlet.ServletContext application;
2.7. javax.servlet.ServletConfig config;
2.8. javax.servlet.jsp.JspWriter out = null;
2.9. java.lang.Object page = this;
3. 在Jsp代碼里, Alt+/提示, 會顯示內置對象, 下面這個沒有exception這個內置對象
4. 只有在isErrorPage為true的時候, 才會有exception這個內置對象
二. Jsp的4個是作用域對象
1. Jsp的4個是作用域對象如下:
1.1. javax.servlet.jsp.PageContext pageContext;
1.2. javax.servlet.http.HttpServletRequest request
1.3. javax.servlet.http.HttpSession session;
1.4. javax.servlet.ServletContext application;
2. 作用域對象可以存值和取值, 它們的取值范圍有限定。
3. 作用域范圍大小: pageContext-->request-->session-->application。
4. 新建一個名稱為Jsp9InnerObject的Web工程
5. 在WebContent下新建一個index.html
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isErrorPage="true"%><!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>同一頁面獲取Jsp的4個作用域的值</title></head><body><%pageContext.setAttribute("pageContext", "我是pageContext內置對象");request.setAttribute("request", "我是request內置對象");session.setAttribute("session", "我是session內置對象");application.setAttribute("application", "我是application內置對象");%><%=pageContext.getAttribute("pageContext") %><br/><%=request.getAttribute("request") %><br/><%=session.getAttribute("session") %><br/><%=application.getAttribute("application") %><br/></body> </html>6. 部署運行, 同一頁面4個值都有
7. 新建一個forward.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>一次請求獲取Jsp的4個作用域的值</title></head><body><%=pageContext.getAttribute("pageContext") %><br/><%=request.getAttribute("request") %><br/><%=session.getAttribute("session") %><br/><%=application.getAttribute("application") %><br/></body> </html>8. 修改index.jsp, 然服務器幫我們找到forward.jsp
9. 部署運行, pageContext的值為null
10. 新建一個sendRedirect.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>二次請求獲取Jsp的4個作用域的值</title></head><body><%=pageContext.getAttribute("pageContext") %><br/><%=request.getAttribute("request") %><br/><%=session.getAttribute("session") %><br/><%=application.getAttribute("application") %><br/></body> </html>11. 修改index.jsp, 讓瀏覽器發送二次請求找到sendRedirect.jsp
12. 部署運行, pageContext和request的值為null
13. 關閉瀏覽器, 再次請求forward.jsp, pageContext、request和session的值為null
14. pageContext作用域僅限于當前的頁面。
15. request作用域僅限于一次請求。
16. session作用域限于一次會話(打開瀏覽器訪問到關閉瀏覽器是一次會話)。
17. application整個工程都可以訪問, 服務器關閉后就不能訪問了。
18. pageContext還可以獲取到其他八個內置對象:
三. out對象和response對象
1. out對象向頁面輸出內容
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>out對象</title></head><body><%out.write("這是使用out對象輸出的內容");%></body> </html>2. response對象向頁面輸出內容
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><!DOCTYPE html> <html><head><meta charset="UTF-8" /><title>response對象</title></head><body><%response.getWriter().write("這是使用response對象輸出的內容");%></body> </html>?
總結
以上是生活随笔為你收集整理的004_Jsp九大内置对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 003_Jsp动作标签
- 下一篇: 005_EL表达式