標籤:post script title 分享 err extend out 多次 agent
#http協議版本
http1.0:當前瀏覽器用戶端與伺服器端建立串連之後,只能發送一次請求,一次請求之後串連關閉。
http1.1:當前瀏覽器用戶端與伺服器端建立串連之後,可以在一次串連中發送多次請求。(基本都使用1.1)
#請求資源
URL: 統一資源定位器。http://localhost:8080/day11/testImg.html。只能定位互連網資源。是URI的子集。
URI: 統一資源標記符。/day11/hello。用於標記任何資源。可以是本地檔案系統,區域網路的資(//192.168.14.10/myweb/index.html),可以是互連網。
#請求方式
常見的請求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE
常用的請求方式: GET(瀏覽器預設的請求方式) 和 POST
#要求標頭
Accept: text/html,image/* -- 瀏覽器接受的資料類型
Accept-Charset: ISO-8859-1 -- 瀏覽器接受的編碼格式
Accept-Encoding: gzip,compress --瀏覽器接受的資料壓縮格式
Accept-Language: en-us,zh- --瀏覽器接受的語言
Host: www.it315.org:80 --(必須的)當前請求訪問的目標地址(主機:連接埠)
If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT --瀏覽器最後的緩衝時間
Referer: http://www.it315.org/index.jsp -- 當前請求來自於哪裡
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) --瀏覽器類型
Cookie:name=eric -- 瀏覽器儲存的cookie資訊
Connection: close/Keep-Alive -- 瀏覽器跟伺服器串連狀態。close: 串連關閉 keep-alive:儲存串連。
Date: Tue, 11 Jul 2000 18:23:51 GMT -- 請求發出的時間
#請求實體
只有post請求才有實體。(post請求一般用於提交比較交敏感的資料)
HttpServletRequest對象
HttpServletRequest對象作用是用於擷取請求資料
核心的API:
請求行:
request.getMethod(); 請求方式
request.getRequetURI() / request.getRequetURL() 請求資源
request.getProtocol() 請求http協議版本
要求標頭:
request.getHeader("名稱") 根據要求標頭擷取請求值
request.getHeaderNames() 擷取所有的要求標頭名稱
實體內容:
request.getInputStream() 擷取實體內容資料
範例程式碼
1、jsp代碼
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"> <title>My JSP ‘index.jsp‘ starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="/HttpTest/HttpTest" method="get"> 使用者名稱:<input type="text" name="name"></br> 密 碼 :<input type="text" name="password"></br> <input type="submit" value="提交"> </form> </hr> </br> </br> <form action="/HttpTest/HttpTest" method="POST"> 使用者名稱:<input type="text" name="name"></br> 密 碼 :<input type="text" name="password"></br> <input type="submit" value="提交"> </form> </body></html>
2、servlet代碼
public class HttpTest extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //t1(request); // t2(request); } //接收post方式的請求 @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { InputStream in=req.getInputStream(); //得到實體內容 byte[] buf=new byte[1024]; int len=0; while ((len=in.read(buf))!=-1) { String str=new String(buf,0,len); System.out.println(str); } } private void t2(HttpServletRequest request) { /** * 要求標頭 */ Enumeration<String> enums=request.getHeaderNames(); while (enums.hasMoreElements()) { String string = (String) enums.nextElement(); String value=request.getHeader(string); System.out.println(string+" : "+value); } } private void t1(HttpServletRequest request) { /** * 請求行 */ System.out.println("請求的方式:"+request.getMethod()); System.out.println("URI:"+request.getRequestURI()); System.out.println("URL:"+request.getRequestURL()); System.out.println("http:"+request.getProtocol()); }}
Http(1)