程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> JSP中的請求、響應、Cookie、會話HttpSession

JSP中的請求、響應、Cookie、會話HttpSession

編輯:關於JSP

1 JSP中的請求與響應 1.1 請求與響應的基本概念 請求:當用戶在浏覽器中輸入 URL並提交或提交表單時,一個請求即被創建並送到服務器上。   響應:當服務器接收到用戶的請求時,會根據用戶的具體請求做相應的處理操作,並將操作結果返回給用戶。   1.2 request對象的常用方法 object getAttribute(String name) 返回指定屬性的屬性值  Enumeration getAttributeNames() 返回所有可用屬性名的枚舉 String getCharacterEncoding() 返回字符編碼方式  int getContentLength() 返回請求體的長度(以字節數)  String getContentType() 得到請求體的MIME類型  ServletInputStream getInputStream() 得到請求體中一行的二進制流  String getParameter(String name) 返回name指定參數的參數值 Enumeration getParameterNames() 返回可用參數名的枚舉 String[] getParameterValues(String name) 返回包含參數name的所有值的數組  String getProtocol() 返回請求用的協議類型及版本號  String getScheme() 返回請求用的計劃名,如:http.https及ftp等  String getServerName() 返回接受請求的服務器主機名  int getServerPort() 返回服務器接受此請求所用的端口號  BufferedReader getReader() 返回解碼過了的請求體  String getRemoteAddr() 返回發送此請求的客戶端IP地址  String getRemoteHost() 返回發送此請求的客戶端主機名 void setAttribute(String key,Object obj) 設置屬性的屬性值  String getRealPath(String path) 返回一虛擬路徑的真實路徑 void setCharacterEncoding(String charset) 指定請求的編碼 1.3 response對象的常用方法 String getCharacterEncoding() 返回響應用的是何種字符編碼  ServletOutputStream getOutputStream() 返回響應的一個二進制輸出流  PrintWriter getWriter() 返回可以向客戶端輸出字符的一個對象  void setContentLength(int len) 設置響應頭長度  void setContentType(String type) 設置響應的MIME類型  sendRedirect(java.lang.String location) 重新定向客戶端的請求 2 Cookie Cookie是指某些網站為了辨別用戶身份、進行session跟蹤而儲存在用戶本地終端上的數據(通常經過加密)。   2.1 Cookie的使用 創建Cookie [java]  Cookie c= new Cookie("name","value");       其中,name為創建的Cookie名稱,將來讀取這個Cookie時使用;value為Cookie對象的值。   讀取Cookie [html]  Cookie cookies[] = request.getCookies();       得到的是一個Cookie數組,使用for循環遍歷:   [java]    for(int i=0;i<cookies.length;i++)   System.out.println(cookies[i].getValue());        向客戶端添加Cookie 將cookie放入到HTTP響應報頭,可以使用HttpServletResponse的addCookie方法   [java]   response.addCookie(c);       刪除Cookie 刪除Cookie時,只需要將Cookie的最大時效設置為0       [java]   for(int i=0;i<cookies.length;i++){   cookies[i].setMaxAge(0);   response.addCookie(cookies[i])}   3 會話HttpSession 3.1 會話的常用方法www.2cto.com Object getAttribute(String name)   根據name獲取設置的屬性值。 void setAttribute(String name,Object value)   設置session對象屬性值   void removeAttribute(String name)   刪除session對象的name屬性 Enumeration getAttributeName()    返回捆綁到當前會話的所有屬性名 long getCreationTime()   返回表示會話創建的一個長整型值      long getLastAccessedTime()  最後訪問日期和時間的一個長整型值        String getId()  返回會話ID       int getMaxInactiveInterval()   返回會話存活的最大秒數。  void setMasInactiveInterval(int seconds)   設置最大的生存時間    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved