JSP九大內置對象詳解

1、Request對象

    客戶端的請求信息被封裝在Request對象中,經過它才能瞭解到客戶的需求,而後作出響應。它是HttpServletRequest類的實例。html

  1. object getAttribute(String name): 返回指定屬性的屬性值
  2. Enumeration getAttributeNames(): 返回全部可用屬性名的枚舉
  3. String getCharacterEncoding(): 返回字符編碼方式
  4. int getContentLength(): 返回請求體的長度(以字節數)
  5. String getContentType() :獲得請求體的MIME類型
  6. ServletInputStream getInputStream() :獲得請求體中一行的二進制流
  7. String getParameter(String name): 返回name指定參數的參數值
  8. Enumeration getParameterNames(): 返回可用參數名的枚舉
  9. String[] getParameterValues(String name): 返回包含參數name的全部值的數組
  10. String getProtocol() :返回請求用的協議類型及版本號
  11. String getScheme(): 返回請求用的計劃名,如:http.https及ftp等
  12. String getServerName(): 返回接受請求的服務器主機名
  13. int getServerPort() :返回服務器接受此請求所用的端口號
  14. BufferedReader getReader(): 返回解碼過了的請求體
  15. String getRemoteAddr(): 返回發送此請求的客戶端IP地址
  16. String getRemoteHost() :返回發送此請求的客戶端主機名
  17. void setAttribute(String key,Object obj) 設置屬性的屬性值
  18. String getRealPath(String path): 返回一虛擬路徑的真實路徑

樣例1

<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
<title>request對象_例1</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
<input type="text" name="qwe">
<input type="submit" value="提交">
</form>
請求方式:<%=request.getMethod()%><br>
請求的資源:<%=request.getRequestURI()%><br>
請求用的協議:<%=request.getProtocol()%><br>
請求的文件名:<%=request.getServletPath()%><br>
請求的服務器的IP:<%=request.getServerName()%><br>
請求服務器的端口:<%=request.getServerPort()%><br>
客戶端IP地址:<%=request.getRemoteAddr()%><br>
客戶端主機名:<%=request.getRemoteHost()%><br>
表單提交來的值:<%=request.getParameter("qwe")%><br>
</body>
</html>

樣例2

<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.Enumeration"%>
<html>
<head>
<title>request對象_例2</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
   用戶名:<input type="text" name="username">  
   密 碼:<input type="text" name="userpass">  
   <input type="submit" value="進入" >
</form>
<%
String str="";
if(request.getParameter("username")!=null && request.getParameter("userpass")!=null){
   Enumeration enumt = request.getParameterNames();
   while(enumt.hasMoreElements()){
      str=enumt.nextElement().toString();
      out.println(str+":"+request.getParameter(str)+"<br>");
   }
}
%>
</body>
</html>

樣例3

<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
   <title>request對象_例3</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
擅長:<input type="checkbox" name="cb" value="ON1">VC++ 
       <input type="checkbox" name="cb" value="ON2">JAVA 
       <input type="checkbox" name="cb" value="ON3">DELPHI 
       <input type="checkbox" name="cb" value="ON4">VB 
       <br>
       <input type="submit" value="進入" name="qwe">
</form>
<%
if(request.getParameter("qwe")!=null ){
   for(int i=0;i<request.getParameterValues("cb").length;i++){
      out.println("cb"+i+":"+request.getParameterValues("cb")[i]+"<br>");
   }
   out.println(request.getParameter("qwe"));
}
%>
</body>
</html>

2、Response對象

    Response對象包含了響應客戶請求的有關信息,但在JSP中不多直接用到它。它是HttpServletResponse類的實例。java

  1. String getCharacterEncoding(): 返回響應用的是何種字符編碼
  2. ServletOutputStream getOutputStream() :返回響應的一個二進制輸出流
  3. PrintWriter getWriter(): 返回能夠向客戶端輸出字符的一個對象
  4. void setContentLength(int len): 設置響應頭長度
  5. void setContentType(String type) :設置響應的MIME類型
  6. sendRedirect(java.lang.String location): 從新定向客戶端的請求

3、Session對象

    Session對象指的是客戶端與服務器的一次會話,從客戶連到服務器的一個WebApplication開始,直到客戶端與服務器斷開鏈接爲止。它是HttpSession類的實例。sql

  1. long getCreationTime():返回SESSION建立時間
  2. public String getId():返回SESSION建立時JSP引擎爲它設的唯一ID號
  3. long getLastAccessedTime():返回此SESSION裏客戶端最近一次請求時間
  4. int getMaxInactiveInterval():返回兩次請求間隔多長時間此SESSION被取消(ms)
  5. String[] getValueNames():返回一個包含此SESSION中全部可用屬性的數組
  6. void invalidate():取消SESSION,使SESSION不可用
  7. boolean isNew():返回服務器建立的一個SESSION,客戶端是否已經加入
  8. void removeValue(String name):刪除SESSION中指定的屬性
  9. void setMaxInactiveInterval():設置兩次請求間隔多長時間此SESSION被取消(ms)
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.util.*" %>
<html>
<head><title>session對象_例1</title><head>
<body><br>
   session的建立時間:<%=session.getCreationTime()%>  <%=new Date(session.getCreationTime())%><br><br>
   session的Id號:<%=session.getId()%><br><br>
   客戶端最近一次請求時間:<%=session.getLastAccessedTime()%>  <%=new java.sql. Time(session.getLastAccessedTime())%><br><br>
   兩次請求間隔多長時間此SESSION被取消(ms):<%=session.getMaxInactiveInterval()%><br><br>
   是不是新建立的一個SESSION:<%=session.isNew()?"是":"否"%><br><br>
<%
   session.putValue("name","霖苑編程");
   session.putValue("nmber","147369");
%>
<%
   for(int i=0;i<session.getValueNames().length;i++)
      out.println(session.getValueNames()[i]+"="+session.getValue(session.getValueNames()[i]));
%>
<!--返回的是從格林威治時間(GMT)1970年01月01日0:00:00起到計算當時的毫秒數-->
</body>
</html>

4、Out對象

    Out對象是JspWriter類的實例,是向客戶端輸出內容經常使用的對象。編程

  1. void clear():清除緩衝區的內容
  2. void clearBuffer():清除緩衝區的當前內容
  3. void flush():清空流
  4. int getBufferSize():返回緩衝區以字節數的大小,如不設緩衝區則爲0
  5. int getRemaining():返回緩衝區還剩餘多少可用
  6. boolean isAutoFlush():返回緩衝區滿時,是自動清空仍是拋出異常
  7. void close():關閉輸出流
<%@page contentType="text/html;charset=gb2312"%>
<html><head><title>out對象_例1:緩存測試</title></head>
<%@page buffer="1kb"%>
<body>
<%
   for(int i=0;i<2000;i++)
      out.println(i+"{"+out.getRemaining()+"}");
%><br>
緩存大小:<%=out.getBufferSize()%><br>
剩餘緩存大小:<%=out.getRemaining()%><br>
自動刷新:<%=out.isAutoFlush()%><br>
<%--out.clearBuffer();--%>
<%--out.clear();--%>
<!--缺省狀況下:服務端要輸出到客戶端的內容,不直接寫到客戶端,而是先寫到一個輸出緩衝區中.只有在下面三中狀況下,纔會把該緩衝區的內容輸出到客戶端上:
   1.該JSP網頁已完成信息的輸出
   2.輸出緩衝區已滿
   3.JSP中調用了out.flush()或response.flushbuffer()
-->
</body>
</html>

5、Page對象

    Page對象就是指向當前JSP頁面自己,有點象類中的this指針,它是java.lang.Object類的實例。數組

  1. class getClass:返回此Object的類
  2. int hashCode():返回此Object的hash碼
  3. boolean equals(Object obj):判斷此Object是否與指定的Object對象相等
  4. void copy(Object obj):把此Object拷貝到指定的Object對象中
  5. Object clone():克隆此Object對象
  6. String toString():把此Object對象轉換成String類的對象
  7. void notify():喚醒一個等待的線程
  8. void notifyAll():喚醒全部等待的線程
  9. void wait(int timeout):使一個線程處於等待直到timeout結束或被喚醒
  10. void wait():使一個線程處於等待直到被喚醒
  11. void enterMonitor():對Object加鎖
  12. void exitMonitor():對Object開鎖

6、Application對象

    Application對象實現了用戶間數據的共享,可存放全局變量。它開始於服務器的啓動,直到服務器的關閉,在此期間,此對象將一直存在;這樣在用戶的先後鏈接或不一樣用戶之間的鏈接中,能夠對此對象的同一屬性進行操做;在任何地方對此對象屬性的操做,都將影響到其餘用戶對此的訪問。服務器的啓動和關閉決定了application對象的生命。它是ServletContext類的實例。緩存

  1. Object getAttribute(String name):返回給定名的屬性值
  2. Enumeration getAttributeNames():返回全部可用屬性名的枚舉
  3. void setAttribute(String name,Object obj):設定屬性的屬性值
  4. void removeAttribute(String name):刪除一屬性及其屬性值
  5. String getServerInfo():返回JSP(SERVLET)引擎名及版本號
  6. String getRealPath(String path):返回一虛擬路徑的真實路徑
  7. ServletContext getContext(String uripath):返回指定WebApplication的application對象
  8. int getMajorVersion():返回服務器支持的Servlet API的最大版本號
  9. int getMinorVersion():返回服務器支持的Servlet API的最大版本號
  10. String getMimeType(String file): 返回指定文件的MIME類型
  11. URL getResource(String path):返回指定資源(文件及目錄)的URL路徑
  12. InputStream getResourceAsStream(String path):返回指定資源的輸入流
  13. RequestDispatcher getRequestDispatcher(String uripath):返回指定資源的RequestDispatcher對象
  14. Servlet getServlet(String name):返回指定名的Servlet
  15. Enumeration getServlets():返回全部Servlet的枚舉
  16. Enumeration getServletNames():返回全部Servlet名的枚舉
  17. void log(String msg):把指定消息寫入Servlet的日誌文件
  18. void log(Exception exception,String msg):把指定異常的棧軌跡及錯誤消息寫入Servlet的日誌文件
  19. void log(String msg,Throwable throwable):把棧軌跡及給出的Throwable異常的說明信息 寫入Servlet的日誌文件

樣例1

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION對象_例1</title><head>
<body><br>
   JSP(SERVLET)引擎名及版本號:<%=application.getServerInfo()%><br><br>
   返回/application1.jsp虛擬路徑的真實路徑:<%=application.getRealPath("/application1.jsp")%><br><br>
   服務器支持的Servlet API的大版本號:<%=application.getMajorVersion()%><br><br>
   服務器支持的Servlet API的小版本號:<%=application.getMinorVersion()%><br><br>
   指定資源(文件及目錄)的URL路徑:<%=application.getResource("/application1.jsp")%><br><br>
   <!--能夠將application1.jsp換成一個目錄--><br><br>
<%
   application.setAttribute("name","霖苑計算機編程技術培訓學校");
   out.println(application.getAttribute("name"));
   application.removeAttribute("name");
   out.println(application.getAttribute("name"));
%>
</body>
</html>

樣例2

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION對象_例2</title><head>
<body><br>
<!--因爲application一直存在於服務器端,能夠利用此特性對網頁記數-->
<%
   if(application.getAttribute("count")==null)
      application.setAttribute("count","1");
   else
     application.setAttribute("count",Integer.toString(Integer.valueOf(application.getAttribute("count").toString()).intValue()+1));
%>
   你是第<%=application.getAttribute("count")%>位訪問者
</body>
<!--因爲getAttribute()方法獲得的是一個Object類型對象,用getString()方法轉化爲String類型-->
<!--用Integer類的valueOf()方法把獲得的String轉化成Integer的對象,在用intValue()方法獲得int型,再加1,最後把計算的結果用Integer.toString()方法轉化成setAttribute()方法所要求的String類型-->
</html>

樣例3

<%@ page contentType="text/html;charset=gb2312"%>
<html>
<head><title>APPLICATION對象_例3</title><head>
<body><br>
<!--因爲application一直存在於服務器端,能夠利用此特性對網頁記數-->
<%
   String str=application.getAttribute("count").toString();//getAttribute("count")返回的是Object類型
   int i=0;
   if(str==null)
      application.setAttribute("count","1");
   else
      i=Integer.parseInt(str); //out.println(i);
      application.setAttribute("count",++i+"");
%>
   你是第<%=application.getAttribute("count")%>位訪問者
</body>
</html>

7、Exception對象

    Exception對象是一個例外對象,當一個頁面在運行過程當中發生了例外,就產生這個對象。若是一個JSP頁面要應用此對象,就必須把isErrorPage設爲true,不然沒法編譯。他其實是java.lang.Throwable的對象。服務器

  1. String getMessage():返回描述異常的消息
  2. String toString():返回關於異常的簡短描述消息
  3. void printStackTrace():顯示異常及其棧軌跡
  4. Throwable FillInStackTrace():重寫異常的執行棧軌跡

8、PageContext對象

    PageContext對象提供了對JSP頁面內全部的對象及名字空間的訪問,也就是說他能夠訪問到本頁所在的Session,也能夠取本頁面所在的application的某一屬性值,他至關於頁面中全部功能的集大成者,它的本 類名也叫PageContextsession

  1. JspWriter getOut():返回當前客戶端響應被使用的JspWriter流(out)
  2. HttpSession getSession():返回當前頁中的HttpSession對象(session)
  3. Object getPage():返回當前頁的Object對象(page)
  4. ServletRequest getRequest():返回當前頁的ServletRequest對象(request)
  5. ServletResponse getResponse():返回當前頁的ServletResponse對象(response)
  6. Exception getException():返回當前頁的Exception對象(exception)
  7. ServletConfig getServletConfig():返回當前頁的ServletConfig對象(config)
  8. ServletContext getServletContext():返回當前頁的ServletContext對象(application)
  9. void setAttribute(String name,Object attribute):設置屬性及屬性值
  10. void setAttribute(String name,Object obj,int scope):在指定範圍內設置屬性及屬性值
  11. public Object getAttribute(String name):取屬性的值
  12. Object getAttribute(String name,int scope):在指定範圍內取屬性的值
  13. public Object findAttribute(String name):尋找一屬性,返回起屬性值或NULL
  14. void removeAttribute(String name):刪除某屬性
  15. void removeAttribute(String name,int scope):在指定範圍刪除某屬性
  16. int getAttributeScope(String name):返回某屬性的做用範圍
  17. Enumeration getAttributeNamesInScope(int scope):返回指定範圍內可用的屬性名枚舉
  18. void release():釋放pageContext所佔用的資源
  19. void forward(String relativeUrlPath):使當前頁面重導到另外一頁面
  20. void include(String relativeUrlPath):在當前位置包含另外一文件
<%@page contentType="text/html;charset=gb2312"%>
<html><head><title>pageContext對象_例1</title></head>
<body><br>
<%
   request.setAttribute("name","霖苑編程");
   session.setAttribute("name","霖苑計算機編程技術培訓");
   //session.putValue("name","計算機編程");
   application.setAttribute("name","培訓");
%>
   request設定的值:<%=pageContext.getRequest().getAttribute("name")%><br>
   session設定的值:<%=pageContext.getSession().getAttribute("name")%><br>
   application設定的值:<%=pageContext.getServletContext().getAttribute("name")%><br>
   範圍1內的值:<%=pageContext.getAttribute("name",1)%><br>
   範圍2內的值:<%=pageContext.getAttribute("name",2)%><br>
   範圍3內的值:<%=pageContext.getAttribute("name",3)%><br>
   範圍4內的值:<%=pageContext.getAttribute("name",4)%><br>
<!--從最小的範圍page開始,而後是reques、session以及application-->
<%pageContext.removeAttribute("name",3);%>
   pageContext修改後的session設定的值:<%=session.getValue("name")%><br>
<%pageContext.setAttribute("name","應用技術培訓",4);%>
   pageContext修改後的application設定的值:<%=pageContext.getServletContext().getAttribute("name")%><br>
   值的查找:<%=pageContext.findAttribute("name")%><br>
   屬性name的範圍:<%=pageContext.getAttributesScope("name")%><br>
</body></html>

9、Config對象

    Config對象是在一個Servlet初始化時,JSP引擎向它傳遞信息用的,此信息包括Servlet初始化時所要用到的參數(經過屬性名和屬性值構成)以及服務器的有關信息(經過傳遞一個ServletContext對象)。app

  1. ServletContext getServletContext():返回含有服務器相關信息的ServletContext對象
  2. String getInitParameter(String name):返回初始化參數的值
  3. Enumeration getInitParameterNames():返回Servlet初始化所需全部參數的枚舉
相關文章
相關標籤/搜索