當Java遇見了Html--Jsp九大內置對象篇

jsp內置對象對象是web容器創建的一組對象,不使用new關鍵詞久可以使用的內置對象。 
九大內置對象包括以下:

  • out –JspWriter
  • request –ServletRequest
  • reponse –ServletResponse
  • config –ServletConfig
  • session –HttpSession
  • application –ServlerContext
  • page –HttpJspPage
  • pageContext –PageContext
  • exception –Exception

1、out對象

JspWriter類實例,是向客戶端負責輸出內容的。 
常用方法如下:

  • println();
  • clear (),如果在flush()之後調用會拋出異常。
  • clearBuffer();
  • flush();
  • isAutoFlush();

舉例子:

“` 

out內置對象


<% 
out.println(“

靜夜思

“); 
out.println(“牀前明月光
”); 
out.println(“疑是地上霜
”); 
out.flush(); 
//out.clear();//這裏會拋出異常。 
out.clearBuffer();//這裏不會拋出異常。 
out.println(“舉頭望明月
”); 
out.println(“低頭思故鄉
”);

%>
    緩衝區大小:<%=out.getBufferSize() %>byte<br>
    緩衝區剩餘大小:<%=out.getRemaining() %>byte<br>
   是否自動清空緩衝區:<%=out.isAutoFlush() %><BR>    


“`

2、request對象

客戶端的請求被封裝在request對象中,通過它可以瞭解客戶端的請求,然後作出響應,request請求具有request請求域。 
常用方法:

  • getParameter(String name)
  • getParamterValues(String name)
  • setAttribute(String name,Onject o)
  • getAttribute(string name)
  • getContetType();
  • getProtocol()
  • getServerName();

    舉個例子:用戶註冊提交數據給request.jsp,在request.jsp頁面根據request對象可以獲取提交過來的數據。

    reg.jsp 註冊jsp

    
    <body>
    <h1>用戶註冊</h1>
    <hr>
    <% 
       int number=-1;
       //說明用戶第一次訪問頁面,計數器對象還未創建
       if(application.getAttribute("counter")==null)
       {
           application.setAttribute("counter", 0);
       }
       number = Integer.parseInt(application.getAttribute("counter").toString());
       number++;
       application.setAttribute("counter", number);
    %>
    <!-- <form name="regForm" action="request.jsp" method="post"> -->
    <form name="regForm" action="response.jsp" method="post">
    <table>
      <tr>
        <td>用戶名:</td>
        <td><input type="text" name="username"/></td>
      </tr>
      <tr>
        <td>愛好:</td>
        <td>
           <input type="checkbox" name="favorite" value="read">讀書
           <input type="checkbox" name="favorite" value="music">音樂
           <input type="checkbox" name="favorite" value="movie">電影
           <input type="checkbox" name="favorite" value="internet">上網
        </td>
      </tr>
      <tr>
         <td colspan="2"><input type="submit" value="提交"/></td>
      </tr>
    </table>
    </form>
    <br>
    <br>
    <a href="request.jsp?username=李四">測試URL傳參數</a>
    
    <br>
    <br>
    <center>
             您是第<%=number %>位訪問本頁面的用戶。
    </center>
    </body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    request.jsp

    <body>
    <h1>request內置對象</h1>
    <% 
       request.setCharacterEncoding("utf-8"); //解決中文亂碼問題,無法解決URL傳遞中文出現的亂碼問題。
       request.setAttribute("password", "123456");
    
    %>
        用戶名:<%=request.getParameter("username") %><br>   
        愛好 :<% 
           if(request.getParameterValues("favorite")!=null)
           {
               String[] favorites = request.getParameterValues("favorite");
               for(int i=0;i<favorites.length;i++)
               {
                  out.println(favorites[i]+"  ");
               }
            }
        %> <br>
         密碼:<%=request.getAttribute("password") %><br> 
         請求體的MIME類型:<%=request.getContentType() %><br>
         協議類型及版本號:  <%=request.getProtocol() %><br>
         服務器主機名 :<%=request.getServerName() %><br>
         服務器端口號:<%=request.getServerPort() %><BR>
         請求文件的長度 :<%=request.getContentLength() %><BR>
         請求客戶端的IP地址:<%=request.getRemoteAddr() %><BR>
         請求的真實路徑:<%=request.getRealPath("request.jsp") %><br>
         請求的上下文路徑:<%=request.getContextPath() %><BR>         
    
    
    
    </body>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

3、response對象

response對象包含了響應客戶端請求的有關信息,它具有頁面作用域,該頁面的作用域只對該頁面有效。常用方法:

  • getCharacterEncoding()
  • setContentType();
  • getWriter();該方法打應輸出流總是前於 out.println();
  • sendRedirect(String location)

請求重定向和請求轉發:

  • 請求重定向:客戶端行爲:response.sendDirect();兩次請求,前一次請求的請求對象不會保存,地址欄的url地址會發生改變
  • 請求轉發:服務器行爲,request.getResuestDispatcher().forward();一次請求,轉發後請求對象會保存,地址欄url地址不會變。

4、session對象

一些基本概念:

  • session表示客戶端與服務器的一次會話
  • web中session指的是用戶在瀏覽某個網站,是進入網站到關閉瀏覽器這段時間
  • 它是保存在服務器的內存中,不同用戶有不同的session
  • 它在第一個jsp頁面被裝載時自動創建,完成會話期管理。

它的的一些常用方法:

  • getCreationTime();
  • String getId();
  • setAttribute(String name,Object o);
  • getAttribute(String name);
  • String[] getValueNames();
  • int getMaxInactivieInterval();單位 秒
  • setMaxInactiveInterval();

舉個例子:


<body>
    <h1>session內置對象</h1>
    <hr>
    <% 
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
      Date d = new Date(session.getCreationTime());
      session.setAttribute("username", "admin"); 
      session.setAttribute("password", "123456");
      session.setAttribute("age", 20);

      //設置當前session最大生成期限單位是秒
      //session.setMaxInactiveInterval(10);//10秒鐘

    %>
    Session創建時間:<%=sdf.format(d)%><br>    
    Session的ID編號:<%=session.getId()%><BR>
         從Session中獲取用戶名:<%=session.getAttribute("username") %><br>

    <a href="session_page2.jsp" target="_blank">跳轉到Session_page2.jsp</a>     

  </body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

session的生命週期:

  • 創建: 當客戶端第一次訪問某個頁面jsp或者servlet,服務器會創建一個 sessionId,每次客戶端向服務器發送請求時,都會將sessionId攜帶過去,服務器會對sessionId進行校驗。
  • 活動: 當客戶端通過超鏈接打開新頁面屬於同一次會話;當瀏覽頁面全部關閉,重新打開屬於一次新的會話。
  • 銷燬:調用sesson.invalidate();session過期,默認是30分鐘;服務器重啓;

5、application對象

  • application實現了用戶數據共享,可存放全局變量。
  • application 開始於服務器的重啓,終止於服務器的關閉
  • application 是ServletContext實例。

常用方法:

  • setAttribute(String ,Object);
  • getAttribute(String);
  • Enumeration getAttributeNames();
  • getServerInfo();返回Jsp 引擎名和版本號

舉個例子:


<body>
    <h1>application內置對象</h1>
    <% 
       application.setAttribute("city", "北京");
       application.setAttribute("postcode", "10000");
       application.setAttribute("email", "[email protected]");

    %>
         所在城市是:<%=application.getAttribute("city") %><br>
    application中的屬性有:<% 
         Enumeration attributes = application.getAttributeNames();
         while(attributes.hasMoreElements())
         {
            out.println(attributes.nextElement()+"  ");
         }
    %><br>
    JSP(SERVLET)引擎名及版本號:<%=application.getServerInfo() %><br>              

  </body>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

6、page對象

page對象就是指當前jsp頁面本身,有點像this指針,它是Java.lang.Object類的實例。常用的方法就是Object 類的方法。

  • getClass()
  • hashCode();
  • equals();
  • copy();
  • clone()
  • toString();
  • notify();
  • notifyAll();
  • wait();

7、pageContext對象

  • pageContext 提供了對jsp頁面所有的對象及名字空間的訪問
  • 它可以取application 某一屬性,也可以取session;
  • 相當於頁面所有功能的集大成者。

方法:

  • getOut()
  • geSession();
  • getPage();
  • getReuest();
  • getResponse();
  • setAttribute();
  • getAttibute();
  • getAttributeScope();
  • forward();
  • include();

8、config對象

它是在一個servlet初始化時,jsp頁面用它傳遞信息,比如servlet初始化參數;以及服務器的有關信息。

  • ServletContext getServletContext();
  • getInitParameter(String);
  • Enumeration getInitParameterNames();

9、exception對象

即異常對象。如果一個jsp想要用此對象,就必須把isErrorPage 設爲true.

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8" isErrorPage="true" %>
<%
  • 1
  • 2
  • 1
  • 2

九大內置對象,講解完畢,感謝大家,後一篇文章會講述除了jsp的九大內置對象其他內容。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章