java基礎81 jsp的內置對象(網頁知識)
1、什麽是內置對象?
在jsp開發中,會頻繁使用到一些對象,如:HttpSession,ServletContext,HttpServletRequest。
如果每次使用這些對象時,都要去創建對象 顯示非常麻煩。所有sun公司就設計jsp時,在jsp頁面加載完畢後會自動幫助開發者創建好這些對象,而開發者只需要直接使用這些對象去調用方法即可,這些創建好的對象就叫內置對象。
舉例:
Servlet: HttpSession session=Request.getSession(true);(需要開發者動手做) Jsp: Tomcat服務器: HttpSession session= request.getSession(true);(不需要開發者動手做) 開發者只需要用session對象去調用相應的方法即可,比如:session.getId();
2、9大內置對象
內置對象名 | 對應類型 |
request response config application session exception page out pageContext |
HttpServletRequest HttpServletResponse ServletConfig ServletContext HttpSession Throwable Object(this) JspWriter pageContext |
2.1、Out 對象
out對象是jspWrite類中的,相當於帶緩沖的PrintWriter
printWriter:
writer(內容):直接向瀏覽器寫出內容
jspWriter
writer(內容):向jsp緩沖區寫出內容
當滿足以下條件之一,緩沖區內容就會寫出(如下面代碼)
1) 緩沖區滿了
2) 刷新緩沖區
3) 關閉緩沖區
4) 執行完jsp頁面
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" buffer="1kb" %> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP ‘MyJsp.jsp‘ starting page</title> 13 14 <meta http-equiv="pragma" content="no-cache"> 15 <meta http-equiv="cache-control" content="no-cache"> 16 <meta http-equiv="expires" content="0"> 17 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 18 <meta http-equiv="description" content="This is my page"> 19 <!-- 20 <link rel="stylesheet" type="text/css" href="styles.css"> 21 --> 22 </head> 23 <body> 24 <% 25 //方法1:頂部設置緩沖區的大小為1Kb(buffer="1kb") 26 for(int i=1;i<=500;i++){ 27 out.write("123"); 28 } 29 //查看緩沖區大小 30 System.out.println("當前緩沖區的大小:"+out.getBufferSize()); 31 //查看緩沖區剩余大小 32 System.out.println("剩余緩沖區大小:"+out.getRemaining()); 33 //方法2:刷新緩存 34 //out.flush(); 35 36 response.getWriter().write("a"); //解析:方法1或2,都能讓123先輸出來,最後輸出a;如果兩者都去掉,那麽先輸出a,後輸出123. 37 %> 38 </body> 39 </html>
2.2、pageContext 對象
PageContext對象類型是PageContext,叫jsp的上下文對象
2.2.1、可以獲取其他八個內置對象
1 public class pageContext{ 2 public void _jspService(){ 3 //創建了對象 4 HttpSession session=””… 5 ServletConfig config=””…. 6 //把8個常見使用的內置對象封裝到了PageContext對象中了 7 public void method(PageContext pageContext){ 8 //從pageContext對象中獲取其他八個內置對象 9 JspWriter out=pageContext.getOut(); 10 .... 11 } 12 } 13 }
2.2.2、本身就是一個域對象
ServletContext context域對象
HttpServletRequest request域對象
HttpSession session域對象
PageContext page 域對象
作用:保存數據和獲取數據,用於共享數據
#保存數據
1)默認情況下,保存到page域:PageContext.setAttribute(“name”);
2)可以向四個域對象保存數據:PageContext.setAttribute(“name”,域範圍常量);
#獲取數據
1)默認情況,從page域獲取:PageContext.getAttribute(“name”);
2)可以從4個域中獲取數據:PageContext.getAttribute(“name”,域範圍常量);
域範圍常量
PageContext.PAGE_Scope
PageContext.REQUEST_SCOPE
PageContext.SESSION_SCOPE
PageContext.APPLICATION_SCOPE
3)自動在四個域中搜索數據:PageContext.findAttribute(“name”);
順序:page域 ----> request域 ----> session域 ----> context域(application域)
3、jsp中的四個域對象
PageContext page域
request reuquest域
session session域
application context域
1)域對象的作用
保存數據和獲取數據 ,用於數據共享.
2)域對象方法
SetAttribute(“name”,Object)保存數據
getAttribute(“name”)獲取數據
removeAttribute(“name”)清除數據
3)域對象的範圍
Page域:只能在當前jsp頁面使用(當前頁面)
Request域:只能在同一個請求中使用(轉發)
Session域:只能在同一個會話(Seesion對象)中使用(私有的)
Context域:只能在同一個文本應用中使用(全局的)
原創作者:DSHORE 作者主頁:http://www.cnblogs.com/dshore123/ 原文出自:https://www.cnblogs.com/dshore123/p/9816583.html 歡迎轉載,轉載務必說明出處。(如果本文對您有幫助,可以點擊一下右下角的 推薦,或評論,謝謝!) |
G M T
選項 : 歷史 : 反饋 : Donate | 關閉 |
java基礎81 jsp的內置對象(網頁知識)