JDBC+Servlet+JSP整合開發之26.JSP內建對象

–使用內建對象的目的
–內建對象
–out 內建對象
–request 內建對象
–response 對象
–session 內建對象
–pageContext 內建對象
–application 內建對象
–config 內建對象
–page 內建對象
–exception 內建對象
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
? 使用內建對象的目的
–JSP 爲了簡化頁面的開發提供了一些內建對象
–這些內建對象在使用以前不須要實例化
–它們由容器(如:Tomcat)來實現和管理
–在全部的JSP頁面中都能使用內建對象
–全部的的內建對象只能在代碼塊和表達式中使用
–不能在JSP聲明中使用
? 內建對象
–request
TestJspServlet.java
package com.michael.servlet;    

import java.io.IOException;    
import java.io.PrintWriter;    

import javax.servlet.ServletException;    
import javax.servlet.http.HttpServlet;    
import javax.servlet.http.HttpServletRequest;    
import javax.servlet.http.HttpServletResponse;    

public class TestJspServlet extends HttpServlet {    

         /**    
         * Constructor of the object.    
         */
    
         public TestJspServlet() {    
                 super();    
        }    

         /**    
         * Destruction of the servlet. <br>    
         */
    
         public void destroy() {    
                 super.destroy(); // Just puts "destroy" string in log    
                 // Put your code here    
        }    

         /**    
         * The doGet method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to get.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doGet(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    

                doPost(request,response);    
        }    

         /**    
         * The doPost method of the servlet. <br>    
         *    
         * This method is called when a form has its tag value method equals to post.    
         *    
         * @param request the request send by the client to the server    
         * @param response the response send by the server to the client    
         * @throws ServletException if an error occurred    
         * @throws IOException if an error occurred    
         */
    
         public void doPost(HttpServletRequest request, HttpServletResponse response)    
                         throws ServletException, IOException {    
                request.setAttribute( "name", "michael");    
                request.getRequestDispatcher( "/RequestJsp.jsp").forward(request, response);    
        }    

         /**    
         * Initialization of the servlet. <br>    
         *    
         * @throws ServletException if an error occurs    
         */
    
         public void init() throws ServletException {    
                 // Put your code here    
        }    

}
RequestJsp.jsp
p_w_picpath
測試request是否被封裝成HttpServletRequest接口
p_w_picpath
–response
–pageContext
–session
TestJspServlet.java
p_w_picpath
RequestJsp.jsp
p_w_picpath
測試
p_w_picpath
下面測試request和session的做用域
RequestJsp2.jsp
p_w_picpath
看下效果:
p_w_picpath
p_w_picpath
發現session做用域針對當前頁面,age爲20,而request針對當前請求,請求結束就終止了,name爲null。
TestJspServlet.java
p_w_picpath
RequestJsp.jsp
p_w_picpath
看下效果:
p_w_picpath
–application
p_w_picpath
p_w_picpath
看下效果:
p_w_picpath 
測試其做用域
p_w_picpath
如今只有application有值,其餘都爲空哈~
p_w_picpath
–out
–config
–page
–exception
p_w_picpath
p_w_picpath
p_w_picpath
? out 內建對象
–做用
? out 對象被封裝成 javax.servlet.jsp.JspWriter接口
? 表示爲客戶端打開的輸出流
? PrintWriter使用它向客戶端發送輸出流
–主要方法
? 輸出各類數據類型的數據,如:out.print(boolean)、
out.println(boolean)/out.print(int)、out.println(int)等
? out.newLine() :輸出一個換行字符
? out.flush():輸出緩衝區裏的數據
? out.close():關閉輸出流
? out.clearBuffer():清除緩衝區中的數據,並把數據輸出到客戶端
? out.clear():清除緩衝區中的數據,但不把數據輸出到客戶端
? out.getBufferSize():得到緩衝區大小
? out.getRemaining():得到緩衝區中剩餘大小
? out.isAutoFlush():判斷緩衝區是否自動刷新
TestOut.jsp
p_w_picpath
測試:
p_w_picpath
? request 內建對象
– 做用
? request 對象表明請求對象
? 它被封裝成HttpServletRequest接口
? 具備HttpServletRequest接口的全部特徵
? 它做爲jspService()方法的一個參數由容器傳遞給JSP頁面
– 主要方法
? 取得請求參數的方法
– String getParameter(String name) 取得name 的參數值
– Enumeration getParameterNames( ) 取得全部的參數名稱
– String [] getParameterValues(String name) 取得全部name 的參數值
– Map getParameterMap( ) 取得一個要求參數的Map
? 取得請求標頭的方法
– String getHeader(String name) 取得name 的標頭
– Enumeration getHeaderNames() 取得全部的標頭名稱
– Enumeration getHeaders(String name) 取得全部name 的標頭
– Map getParameterMap( ) 取得一個要求參數的Map
– int getIntHeader(String name) 取得整數類型name 的標頭
– long getDateHeader(String name) 取得日期類型name 的標頭
– Cookie [] getCookies( ) 取得與請求有關的cookies
? 其餘請求的方法
–String getContextPath( ) 取得Context 路徑
–String getMethod( ) 取得HTTP 的方法(GET、POST)
–String getProtocol( ) 取得使用的協議 (HTTP/1.一、HTTP/1.0 )
–String getQueryString( ) 取得請求的參數字符串,不過,HTTP的方法必須爲GET
–String getRequestedSessionId( ) 取得用戶端的Session ID
–String getRequestURI( ) 取得請求的URL,可是不包括請求的參數字符串
–Cookie [] getCookies( ) 取得與請求有關的cookies
–String getRemoteAddr( ) 取得用戶的IP 地址
–String getRemoteHost( ) 取得用戶的主機名稱
–int getRemotePort( ) 取得用戶的主機端口
–String getRemoteUser( ) 取得用戶的名稱
–void getCharacterEncoding(String encoding) 設定編碼格式,用來解決頁
面傳遞中文的問題
下面看下servlet和jsp是如何獲取請求參數
先看下servlet哈~
MyForm.jsp
p_w_picpath
TestRequestServlet.java
p_w_picpath
測試:
p_w_picpath
p_w_picpath
下面咱們經過JSP中request內建對象來獲取哈~
MyForm.jsp
p_w_picpath
TestRequestJsp.jsp
p_w_picpath
測試:
p_w_picpath
p_w_picpath
下面再測試Enumeration getHeaderNames() 取得全部的標頭名稱
首先咱們仍是來看下Servlet是怎麼操做的哈~
TestRequestServlet.java
p_w_picpath
測試:
p_w_picpath
再看下jsp如何操做哈~
TestRequestJsp.jsp
p_w_picpath
測試:
p_w_picpath
測試String getContextPath()取得Context路徑
TestRequestJsp.jsp
p_w_picpath
測試:
p_w_picpath
p_w_picpath
測試String getQueryString( ) 取得請求的參數字符串,不過,HTTP的方法必須爲GET
MyForm2.jsp
p_w_picpath
TestRequestJsp.jsp
p_w_picpath
測試:
p_w_picpath
p_w_picpath
一樣咱們能夠動態指定,不過HTTP的方法必須爲POST
MyForm2.jsp
p_w_picpath
測試
p_w_picpath
p_w_picpath
String getRemoteAddr()取得用戶的IP地址
p_w_picpath
測試:
p_w_picpath
裝localhost換成IP地址192.168.1.100測試下
p_w_picpath
咱們換臺電腦測試下
p_w_picpath
? response 對象
– 做用
? response 對象主要將JSP 處理數據後的結果傳回到客戶端
? response 對象是實現 javax.servlet.http.HttpServletResponse 接口
– 主要方法
? 設定表頭的方法
– void addCookie(Cookie cookie) 新增cookie
– void addDateHeader(String name, long date) 新增long類型的值到name標頭
– void addHeader(String name, String value) 新增String 類型的值到name 標頭
– void addIntHeader(String name, int value) 新增int 類型的值到name 標頭
– void setDateHeader(String name, long date) 指定long類型的值到name標頭
– void setHeader(String name, String value) 指定String 類型的值到name 標頭
– void setIntHeader(String name, int value) 指定int 類型的值到name 標頭
? 設定響應狀態碼的方法
– void sendError(int sc) 傳送狀態碼(status code)
– void sendError(int sc, String msg) 傳送狀態碼和錯誤信息
– void setStatus(int sc) 設定狀態碼
? 用來URL 重寫(rewriting)的方法
– String encodeRedirectURL(String url) 對使用sendRedirect( )方法的URL予以編碼
void sendError(int sc) 傳送狀態碼(status code)來動態手動設置狀態碼
p_w_picpath
測試:
p_w_picpath
測試void sendError(int sc, String msg) 傳送狀態碼和錯誤信息
p_w_picpath
測試:
p_w_picpath
String encodeRedirectURL(String url) 對使用sendRedirect( )方法的URL予以編碼
p_w_picpath 
測試:
p_w_picpath
? session 內建對象
– 做用
? session 對象表示目前用戶的會話(session)情況。
? 用此項機制能夠輕易識別每個用戶, 而後針對每個別用戶的要求,給予正確的響應。
? session 對象實現javax.servlet.http.HttpSession 接口
– 主要方法
? long getCreationTime() 取得session產生的時間,單位是毫秒,由1970 年1 月1日零時算起
? String getId()  取得session 的ID
? long getLastAccessedTime() 取得用戶最後經過這個session送出請求的時間,單 位是毫秒,由1970 年1 月1 日零時算起
? long getMaxInactiveInterval() 取得最大session不活動的時間,若超過這時間,session 將會失效,時間單位爲秒
? void invalidate() 取消session 對象,並將對象存放的內容徹底實效
? boolean isNew() 判斷session 是否爲"新"的,所謂"新"的session,表示session
已由服務器產生,可是client 還沒有使用
? void setMaxInactiveInterval(int interval) 設定最大session不活動的時間, 若超過這時間,session 將會失效,時間單位爲秒
? application 內建對象
–做用
? application 對象實現javax.servlet.ServletContext 接口
? 它主要功用在於取得或更改 Servlet 的設定
–主要方法
? 容器相關信息的方法
– int getMajorVersion( )  取得Container 主要的Servlet API 版本,如: 2 
–int getMinorVersion( ) 取得Container 次要的Servlet API 版本,如:4 
–String getServerInfo( ) 取得Container 的名稱和版本
?有關服務端的路徑和文件的方法
–String getMimeType(String file)  取得指定文件的MIME 類型
–ServletContext getContext(String uripath)  取得指定Local URL 的 Application context   
String getRealPath(String path)取得本地端path的絕對路徑
?有關日誌記錄的方法
–void log(String message)  將信息寫入log 文件中
–void log(String message, Throwable throwable)  將stack trace 所產生的異常信息寫入log文件中
TestApplication.jsp
p_w_picpath
測試:
p_w_picpath
? pageContext 內建對象
– 做用
? pageContext對象可以存取其餘隱含對象。
? 當隱含對象自己也支持屬性時,pageContext對象也提供存取那些屬性的方法。
– 主要方法
? 取得其餘隱含對象的方法
– Exception getException( ) 返回目前網頁的異常,不過此網頁要爲error page, 例如:exception 隱含對象
– JspWriter getOut( ) 返回目前網頁的輸出流,例如:out 隱含對象
– Object getPage( ) 返回目前網頁的Servlet 實體(instance),例如:page 隱含對象
– ServletRequest getRequest( ) 返回目前網頁的請求,例如:request 隱含對象
– ServletResponse getResponse( ) 返回目前網頁的響應,例如:response 隱含對象
– ServletConfig getServletConfig( ) 返回目前此網頁的ServletConfig 對象,例如:config 隱含對象
– ServletContext getServletContext( ) 返回目前此網頁的執行環境(context),例如: application隱含對象
– HttpSession getSession( ) 返回和目前網頁有聯繫的會話(session),例如: session 隱含對象
? 取得屬性的方法
– Object getAttribute(String name, int scope) 返回name 屬性,範圍爲scope 的 屬性對象, 回傳類型爲 java.lang.Object
– Enumeration getAttributeNamesInScope(int scope) 返回全部屬性範圍爲scope 的屬性名稱,回傳類型爲Enumeration
– int getAttributesScope(String name) 返回屬性名稱爲name 的屬性範圍
– void removeAttribute(String name) 移除屬性名稱爲name 的屬性對象
– void removeAttribute(String name, int scope) 移除屬性名稱爲name,範圍爲 scope 的屬性對象
– void setAttribute(String name, Object value, int scope) 指定屬性對象的名稱爲name、值 爲value、範圍爲scope
– Object findAttribute(String name) 尋找在全部範圍中屬性名稱爲name 的屬性對象
? 範圍常量
– PAGE_SCOPE  存入pageContext 對象的屬性範圍
– REQUEST_SCOPE  存入request 對象的屬性範圍
– SESSION_SCOPE  存入session 對象的屬性範圍
– APPLICATION_SCOPE   存入application 對象的屬性範圍
TestPageContext.jsp
  p_w_picpath
測試:
p_w_picpath
? exception 內建對象
–做用
?當JSP 網頁有錯誤時會產生異常,而exception 對象就
來針對這個異常作處理
–主要方法
? getMessage( ) 得到錯誤信息
? getLocalizedMessage( ) 得到本地錯誤信息
? printStackTrace(new java.io.PrintWriter(out)) 
打印堆棧信息
MyErrorPage.jsp
p_w_picpath
TestException.jsp
  p_w_picpath
測試:
p_w_picpath 
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
相關文章
相關標籤/搜索