1、jsp內置對象是指不用聲明就能夠在jsp頁面的腳本部分直接使用的組件html
是JSP容器爲每一個頁面提供的Java對象,開發者能夠直接使用它們而不用顯式聲明。JSP內置對象也被稱爲預約義變量。java
按照期做用分類爲:web
1.輸入輸出對象 : HttpServletRequest的request, HttpServletResponse的response, JspWriter的out;緩存
2.做用域通訊對象 : HttpSession的session, ServletContext的application, PageContext的pageContext;tomcat
3.servlet對象 : Object的page, ServletConfig的config;服務器
4.錯誤對象 : Throwable的exception; cookie
對象 | 描述 | 做用域 |
---|---|---|
request | HttpServletRequest類的實例,提供對客戶端HTTP請求數據的訪問 | request |
response | HttpServletResponse類的實例,響應信息,用來向客戶端輸出數據。 | page |
out | PrintWriter類的實例,用於把結果輸出至網頁上(提供對輸出流的訪問) | page |
session | HttpSession類的實例,session對象用來跟蹤在各個客戶端請求間的會話。 | session |
application | ServletContext類的實例,與應用上下文有關,經過向application中添加屬性,則全部組成您web應用的JSP文件都能訪問到這些屬性。 | application |
config | ServletConfig類的實例,該對象容許將初始化數據傳遞給一個jsp頁面 | page |
pageContext | PageContext類的實例,提供對JSP頁面全部對象以及命名空間的訪問 | page |
page | 相似於Java類中的this關鍵字(jsp頁面對應的servlet類實例) | page |
Exception | Exception類的對象,表明發生錯誤的JSP頁面中對應的異常對象 | page |
2、各個對象詳解session
1.首先要了解JSP的四個做用域:app
application,session,request,page.jsp
名稱 | 做用域 |
---|---|
request | 在當前請求中有效 |
application | 在全部應用程序中有效,application做用域就是服務器或tomcat啓動到關閉的整段時間, |
page | 在當前頁面中有效,對於page對象的引用將在響應返回給客戶端以後被釋放,或者在請求被轉發到其餘地方後被釋放。 |
session | 在當前會話中有效 |
2.request經常使用方法
request.getParameter(String name)
用於獲取客戶端傳送給服務器端的參數(一般是表單中的參數)。
request.getMethod()
獲取客戶端發起請求的方法
request.getProtocol()
獲取客戶使用的協議
request.getServletPath()
獲取客戶提交信息的頁面。
request.getHeader("accept")
獲取HTTP頭文件中的accept、accept-encoding和Host的值。
示例以下:
String strHeader =request.getHeader("accept")
request.getCookies()
獲取客戶端請求包含的全部cookie的集合
示例以下:
<% Cookie cookies[] = request.getCookies(); for(int i = 0;i < cookies.length;i++){ out.write("cookies名稱:"+cookies[i].getName()+"\t\tcookies值:"+cookies[i].getValue()+"<br>"); } %>
request.setCharacterEncoding(String type)
重載正文中使用的字符編碼。
若是提交請求的頁面編碼和響應輸出頁面的編碼不一致,在處理中文時就會出現亂碼,這個時候,通常使用 request.setCharacterEncoding (String type)來處理。
request.getRemoteAddr()
獲取客戶端的ip地址
request.getRemoteHost()
獲取客戶端主機名稱
request.getServerName()
獲取服務器名稱
request.getServerPort()
獲取服務器的端口號
2.response經常使用方法
response.setHeader(String name,String value)
將指定的參數名稱和值設置到頭信息中
示例以下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%response.setHeader("Cache-Control","no-store")%> //實現禁止頁面緩存 <%response.setHeader("refresh","30")%> //定時每30秒自動刷新 <html> <head> <meta http-equiv="Content-type" content="text/html; charset=UTF-8" /> <title>test</title> </head> <body> </body> </html>
response.sendRedirect(String location)
把響應送到另一個位置進行處理(客戶端跳轉) 『request對象沒法傳遞到新頁面』
response .setCharacterEncoding(String type)
設置響應頁面的字符編碼
response.setContentType(String charset)
設置響應內容的類型
示例以下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> //設置響應內容類型爲office xls類型,以便實如今客戶端用Excel打開 <%response.setContentType("application/vnd.ms-excel");%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <table border="1px" cellspacing="0" cellpadding="0" width="850px"> <tr> <th>學號</th> <th>姓名</th> <th>專業</th> </tr> <tr> <td>11</td> <td>小馬</td> <td>通訊</td> </tr> </table> </body> </html>
response.addCookie(Cookie cookie)
添加一個指定的cookie到響應中
示例以下:
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <% Cookie cookie = new Cookie("iborder","no1"); response.addCookie(cookie); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> 添加一個指定的cookie到本頁面中 </body> </html>
3.out經常使用方法
一般狀況下,服務端要輸出到客戶端的內容,不直接寫到客戶端,而是先寫到一個輸出緩衝區,只有在下面三種狀況下,纔會把該緩衝區的內容輸出到客戶端:
(1)該jsp網頁已經完成信息的輸出;
(2)輸出緩衝區已滿;
(3)jsp中調用了out.flush()或response.flushbuffer()。
輸出緩衝區能夠由<%@ page buffer="1kb"%>來設定,當設置<%@ page buffer="none"%>時,表示不使用緩衝區,直接輸出。也能夠由response.setBufferSize()來設定緩衝區大小。
out.getBufferSize()
得到當前實際緩衝區的大小
out.clearBuffer()
清空當前緩衝區內容。若是已經有內容被輸出,不會返回IO錯誤信息
out.clear()
清空當前緩衝區內容。若是已經有內容被輸出,返回IO錯誤信息
out.flush()
馬上輸出緩衝區中的色內容
out.close()
先調用flush()功能,在關閉輸出流,該方法會在jsp頁面結束時自動調用。
out.print(String s)·
輸出字符串
out.println(String s)
輸出字符串,並以換行符結束
示例以下:
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" buffer="8kb"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <p>當前緩衝區大小爲<span><%=out.getBufferSize() %></span></p> <% out.flush(); out.println("1不可見,由於下一句將調用out.clearBuffer()<br>"); out.clearBuffer(); out.println("2可見,由於接下來將調用out.flush(),再調用out.clearBuffer()<br>"); out.flush(); out.clearBuffer(); out.println("3可見,由於接下來將調用out.close()<br>"); out.close(); %> </body> </html>
運行結果:
4.session經常使用方法
session對象用來保存單個用戶訪問時的一些信息。
服務器對每個客戶端會話,分配一個不會重複的會話sessionID,以此來區分不一樣用戶,從而實現針對每一用戶的請求,做出正確響應。
session.getId()
獲取sessionID
session.setAttribute(String key,Object value)
將value放入索引關鍵字爲key的字符串中
session.getAttribute(String key)
獲取session對象中索引關鍵字爲key的對象
session.removeAttribute(String key)
刪除session對象中索引關鍵字爲key的對象
session. isNew()
判斷是不是一個新的客戶。
示例以下:
(1)session.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <p>單擊提交按鈕查看存入的session</p> <form action="showsession.jsp" method="get"> <% session.setAttribute("username", "iborder"); %> <p>保存username爲<span><%="iborder" %></span></p> <input type="submit" name="submit" value="提交"> </form> </body> </html>
(2)showsession.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <p>取得username爲<span><%=session.getAttribute("username") %></span></p> </body> </html>
5.application經常使用方法
application對象實現了用戶間數據的共享,可存放全局變量
表明JSP文件生成的異常對象,只能在引入了下面代碼 的JSP文件中使用。
<%@ page isErrorPage="true"%>
application. setAttribute(String name,Object object)
在application中以name爲關鍵字存入一個對象
application.get Attribute(String name)
從application中獲取關鍵字 爲 name的 對象
application.remove Attribute(String name)
從application中獲取關鍵字 爲 name的 對象
示例以下:
網站訪問計數器
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String str = ""; int i=1; if(application.getAttribute("count") == null){ application.setAttribute("count", "1"); } else{ if(session.isNew()){ str = application.getAttribute("count").toString(); i = Integer.parseInt(str); application.setAttribute("count", Integer.toString(i+1)); } } %> <p>你是本站第<%=application.getAttribute("count") %>位訪問者</p> </body> </html>
6.config經常使用方法
config對象是在一個Servlet初始化時,JSP引擎向它傳遞信息用的,此信息包括Servlet初始化時所要用到的參數(經過屬性名和屬性值構成)以及服務器的有關信息(經過傳遞一個ServletContext對象)
String getServletName() | 返回Servlet的名字 |
ServletContext getServletContext() | 返回含有服務器相關信息的ServletContext對象 |
String getInitParameter(String name) | 返回初始化參數的值 |
Enumeration getInitParameterNames() | 返回Servlet初始化所需全部參數的枚舉 |
7.pageContext經常使用方法
pageContext是頁面上下文對象,提供了對JSP頁面內全部的對象及名字空間的訪問,PageContext類擁有getRequest,getResponse,getOut,getSession等方法,他至關於集成其餘內置對象的功能。
JspWriter getOut() | 返回當前客戶端響應被使用的JspWriter流(out) |
HttpSession getSession() | 返回當前頁中的HttpSession對象(session) |
Object getPage() | 返回當前頁的Object對象(page) |
ServletRequest getRequest() | 返回當前頁的ServletRequest對象(request) |
ServletResponse getResponse() | 返回當前頁的ServletResponse對象(response) |
Exception getException() | 返回當前頁的Exception對象(exception) |
ServletConfig getServletConfig() | 返回當前頁的ServletConfig對象(config) |
ServletContext getServletContext() | 返回當前頁的ServletContext對象(application) |
void setAttribute(String name,Object attribute) | 設置屬性及屬性值 |
void setAttribute(String name,Object obj,int scope) | 在指定範圍內設置屬性及屬性值 |
public Object getAttribute(String name) | 取屬性的值 |
Object getAttribute(String name,int scope) | 在指定範圍內取屬性的值 |
public Object findAttribute(String name) | 尋找一屬性,返回起屬性值或NULL |
void removeAttribute(String name) | 刪除某屬性 |
void removeAttribute(String name,int scope) | 在指定範圍刪除某屬性 |
int getAttributeScope(String name) | 返回某屬性的做用範圍 |
Enumeration getAttributeNamesInScope(int scope) | 返回指定範圍內可用的屬性名枚舉 |
void release() | 釋放pageContext所佔用的資源 |
void forward(String relativeUrlPath) | 使當前頁面重導到另外一頁面 |
void include(String relativeUrlPath) | 在當前位置包含另外一文件 |
8.page經常使用功能
page對象就是指向當前JSP頁面自己,相似於類中的this指針
class getClass | 返回此Object的類 |
int hashCode() | 返回此Object的hash碼 |
boolean equals(Object obj) | 判斷此Object是否與指定的Object對象相等 |
void copy(Object obj) | 把此Object拷貝到指定的Object對象中 |
Object clone() | 克隆此Object對象 |
String toString() | 把此Object對象轉換成String類的對象 |
void notify() | 喚醒一個等待的線程 |
void notifyAll() | 喚醒全部等待的線程 |
void wait(int timeout) | 使一個線程處於等待直到timeout結束或被喚醒 |
void wait() | 使一個線程處於等待直到被喚醒 |
void enterMonitor() | 對Object加鎖 |
void exitMonitor() | 對Object開鎖 |
9.exception對象
exception對象是一個異常對象,當一個頁面在運行過程當中發生了異常,就產生這個對象。若是一個JSP頁面要應用此對象,就必須把isErrorPage設爲true,不然沒法編譯。