<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    peace嘮叨

    人生得意須盡歡,莫斯金樽空對(duì)月。

    http協(xié)議介紹(servlet)

    本文是servlet的入門篇,主要簡(jiǎn)單介紹下http協(xié)議

    1.什么是http

    _ 1.http協(xié)議:_
    1. 復(fù)雜解釋:
      http(超文本傳輸協(xié)議)是一個(gè)基于請(qǐng)求與響應(yīng)模式的、無(wú)狀態(tài)的、應(yīng)用層的協(xié)議,常基于TCP的連接方式,HTTP1.1版本中給出一種持續(xù)連接的機(jī)制,絕大多數(shù)的Web開(kāi)發(fā),都是構(gòu)建在HTTP協(xié)議之上的Web應(yīng)用。
    2. 簡(jiǎn)單說(shuō):
      對(duì)瀏覽器客戶端 和 服務(wù)器端 之間數(shù)據(jù)傳輸?shù)母袷揭?guī)范.
    3. 協(xié)議版本:
      http1.0:當(dāng)前瀏覽器客戶端與服務(wù)器端建立連接之后,只能發(fā)送一次請(qǐng)求,一次請(qǐng)求之后連接關(guān)閉。
      http1.1:當(dāng)前瀏覽器客戶端與服務(wù)器端建立連接之后,可以在一次連接中發(fā)送多次請(qǐng)求。(基本都使用1.1)
        請(qǐng)求一次資源就會(huì)出現(xiàn)一次請(qǐng)求,比如三張圖片,就有三次請(qǐng)求,如果圖片是一樣 的就只有一次請(qǐng)求;

    2.查看http協(xié)議的工具:
    1. chrome(谷歌)瀏覽器查看:
      右鍵點(diǎn)擊查看元素(inspect element),點(diǎn)擊network即可;
    1
    2. 火狐:
      使用火狐的firebug插件(右鍵->firebug->網(wǎng)絡(luò))
    3. 使用系統(tǒng)自帶的telnet工具(遠(yuǎn)程訪問(wèn)工具)
    a)telnet localhost 8080 訪問(wèn)tomcat服務(wù)器
    b)ctrl+] 回車.可以看到回顯
    c)輸入請(qǐng)求內(nèi)容,回車,即可查看到服務(wù)器響應(yīng)信息。

    GET / HTTP/1.1Host: www.baidu.com

    2.http協(xié)議內(nèi)容:

    1. 請(qǐng)求:
    GET /HttpSer HTTP/1.1Host: localhost:8080Connection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36Accept-Encoding: gzip, deflate, sdchAccept-Language: en-US,en;q=0.8
    1. 響應(yīng):
    HTTP/1.1 302 Found Server: Apache-Coyote/1.1 Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked Date: Fri, 09 Oct 2015 08:55:42 GMT

    下面將對(duì)這兩個(gè)協(xié)議進(jìn)行介紹:

    3.http請(qǐng)求介紹:

    GET /HttpSer HTTP/1.1-請(qǐng)求行Host: localhost:8080--請(qǐng)求頭;有多個(gè)key-value組成Connection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36Accept-Encoding: gzip, deflate, sdchAccept-Language: en-US,en;q=0.8--可選,實(shí)體內(nèi)容;

    請(qǐng)求行介紹

    GET /HttpSer HTTP/1.1 -請(qǐng)求行
    1.請(qǐng)求資源的URL和URI比較:
        URL: 統(tǒng)一資源定位符。http://localhost:8080/HttpSer/index.html。只能定位互聯(lián)網(wǎng)資源。是URI 的子集.
        URI: 統(tǒng)一資源標(biāo)記符。/HttpSer/index.html。用于標(biāo)記任何資源。可以是本地文件系統(tǒng),局域網(wǎng)的資源(//192.168.14.10/HttpSer/index.html),可以是互聯(lián)網(wǎng)。
    2.請(qǐng)求方式:
        常見(jiàn)的請(qǐng)求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE
        常用的請(qǐng)求方式: GET(有將實(shí)體信息放在瀏覽器地址欄) 和 POST(隱藏實(shí)體內(nèi)容)
    3. servlet獲得請(qǐng)求行信息:_

    /*** 1.1請(qǐng)求行的獲得*/System.out.println("請(qǐng)求方式:"+request.getMethod());//獲得提交方式System.out.println("請(qǐng)求URI:"+request.getRequestURI());//獲得uriSystem.out.println("請(qǐng)求url:"+request.getRequestURL());//獲得urlSystem.out.println("獲得協(xié)議:"+request.getProtocol());//獲得所用協(xié)議##輸出:請(qǐng)求方式:GET請(qǐng)求URI:/HttpSer/TestRequst請(qǐng)求url:http://localhost:8080/HttpSer/TestRequst獲得協(xié)議:HTTP/1.1

    4. 測(cè)試提交方式:
    新建立web工程,建立TestMethod.html文件:
    4
    建立Servlet類TestMethod.java修改get和post方法:

    protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("get 方式提交"); }
    /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("post 方式提交"); }

    運(yùn)行tomcat可以看淡提交方式的不同:瀏覽器地址欄顯示的不同,servlet調(diào)用的方法也不同;
    get提交
    2
    post提交
    3
    經(jīng)過(guò)對(duì)比請(qǐng)求可以發(fā)現(xiàn)
    get方式在請(qǐng)求行多了?name=wang&pass=123
    post多了實(shí)體內(nèi)容:Content-Type: application/x-www-form-urlencoded
    請(qǐng)求內(nèi)容同如下:

    #get方式:GET /HttpSer/TestMethod?name=wang&pass=123 HTTP/1.1Host: localhost:8080Connection: keep-aliveAccept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36Referer: http://localhost:8080/HttpSer/testMethod.htmlAccept-Encoding: gzip, deflate, sdchAccept-Language: en-US,en;q=0.8#post方式:POST /HttpSer/TestMethod HTTP/1.1Host: localhost:8080Connection: keep-aliveContent-Length: 18Cache-Control: max-age=0Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8Origin: http://localhost:8080Upgrade-Insecure-Requests: 1User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.85 Chrome/45.0.2454.85 Safari/537.36Content-Type: application/x-www-form-urlencodedReferer: http://localhost:8080/HttpSer/testMethod.htmlAccept-Encoding: gzip, deflateAccept-Language: en-US,en;q=0.8

    請(qǐng)求頭介紹:

    1. 請(qǐng)求頭介紹:
    請(qǐng)求頭主要包含一些有用信息:
    1.Host: localhost:8080主機(jī)地址和端口
    2.Connection: keep-alive 連接方式
    3.User-Agent:瀏覽器的一些信息
    4.Referer:來(lái)訪頁(yè)面
    5.Content:實(shí)體內(nèi)容;post才有
    2. servlet獲得請(qǐng)求頭主要的方法:
    request.getHeader(“Host”));通過(guò)建獲得相應(yīng)請(qǐng)求的內(nèi)容;
    Enumeration headerNames = request.getHeaderNames();獲得請(qǐng)求頭所有的鍵值
    3. 演示如下:
    修改doget方法

    /*** 設(shè)置參數(shù)查詢的編碼*該方法只能對(duì)請(qǐng)求實(shí)體內(nèi)容的數(shù)據(jù)編碼起作用。POST提交的數(shù)據(jù)在實(shí)體內(nèi)容中,所以該方法對(duì)POST方法有效!*GET方法的參數(shù)放在URI后面,所以對(duì)GET方式無(wú)效!!! */request.setCharacterEncoding("utf-8");/** * 1.1請(qǐng)求行的獲得 */ System.out.println("請(qǐng)求方式:"+request.getMethod()); System.out.println("請(qǐng)求URI:"+request.getRequestURI()); System.out.println("請(qǐng)求url:"+request.getRequestURL()); System.out.println("獲得協(xié)議:"+request.getProtocol());/** * 1.2請(qǐng)求頭的獲得 *///通過(guò)鍵獲得請(qǐng)求頭的內(nèi)容 System.out.println("獲得host:"+request.getHeader("Host")); System.out.println("獲得瀏覽器的User-Agent:"+request.getHeader("User-Agent"));//通過(guò)迭代器迭代,獲得鍵 在取鍵值 Enumeration<String> headerNames = request.getHeaderNames();while(headerNames.hasMoreElements()){ String key=headerNames.nextElement(); System.out.println(key+":"+request.getHeader(key)); }/** * 得到請(qǐng)求實(shí)體內(nèi)容 * 比如:實(shí)體為name=peace&pass=1234 */ ServletInputStream in = request.getInputStream();byte[] buf=newbyte[1024];int len=0;while((len=in.read(buf))!=-1){ String str=new String(buf,0,len); System.out.println(str); }#輸出如下:請(qǐng)求方式:GET請(qǐng)求URI:/HttpSer/TestRequst請(qǐng)求url:http://localhost:8080/HttpSer/TestRequst獲得協(xié)議:HTTP/1.1獲得host:localhost:8080獲得瀏覽器的User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.101 Chrome/45.0.2454.101 Safari/537.36host:localhost:8080connection:keep-aliveaccept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8upgrade-insecure-requests:1user-agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/45.0.2454.101 Chrome/45.0.2454.101 Safari/537.36referer:http://localhost:8080/HttpSer/testMethod.htmlaccept-encoding:gzip, deflate, sdchaccept-language:en-US,en;q=0.8cookie:CNZZDATA1255712369=1133597550-1443969628-%7C1443969628//此去后面文章會(huì)介紹;

    輸入?yún)?shù)的介紹:

    1. 輸入?yún)?shù):
    輸入?yún)?shù):
    對(duì)于get來(lái)說(shuō)就是跟在url后面的內(nèi)容
    /TestParam?name=”peace”&password=”sisi” ;name=”peace”&password=”sisi”這就是輸入?yún)?shù)
    對(duì)于post來(lái)說(shuō)就是實(shí)體內(nèi)容,不可見(jiàn)
    2. Servlet中獲得輸入?yún)?shù)的方法:
    String name=request.getParameter(“name”);獲得對(duì)應(yīng)輸入?yún)?shù)名字的內(nèi)容
    Enumeration params = request.getParameterNames();獲得所有輸入?yún)?shù)的名字,返回一個(gè)迭代器
    String[] hobits = request.getParameterValues(names);如果對(duì)應(yīng)名字的內(nèi)容是一個(gè)數(shù)組,使用這個(gè)方法獲得,比如復(fù)選框

    3. 演示如下:
    1.建立testParam.html
    5
    2.修改doget方法:

    /** * 設(shè)置參數(shù)查詢的編碼 * 該方法只能對(duì)請(qǐng)求實(shí)體內(nèi)容的數(shù)據(jù)編碼起作用。POST提交的數(shù)據(jù)在實(shí)體內(nèi)容中,所以該方法對(duì)POST方法有效! * GET方法的參數(shù)放在URI后面,所以對(duì)GET方式無(wú)效!!! */ request.setCharacterEncoding("utf-8"); //獲得所有的方式
     System.out.println("提交方式:"+request.getMethod()); //獲得輸入?yún)?shù) String name=request.getParameter("name"); String pass=request.getParameter("password"); System.out.println("name:"+name+",pass:"+pass);/*此去為如果get方式提交出現(xiàn)亂碼,使用; * if("GET".equals(request.getMethod())){ password = new String(password.getBytes("iso-8859-1"),"utf-8"); }*/ System.out.println(">>>>>>>>>>>>>>>>>"); //獲得所有輸入?yún)?shù)的名字 Enumeration<String> params = request.getParameterNames(); while(params.hasMoreElements()) { String names=params.nextElement(); //如果是復(fù)選框,使用getParameterValues(names);方法 if("hobit".equals(names)){ System.out.println(names+":"); String[] hobits = request.getParameterValues(names); for(String s:hobits) System.out.print(s+","); System.out.println(); }
     else{ System.out.println(names+":"+request.getParameter(names)); } }##輸出結(jié)果如下:提交方式:POSTname:peace,pass:124>>>>>>>>>>>>>>>>>name:peacepassword:124gender:男籍貫:湖南hobit:籃球,足球,info:一條小鯊魚peaceid:001

    4.http響應(yīng)介紹:

    HTTP/1.1 302 Found ---響應(yīng)行 Server: Apache-Coyote/1.1 ---響應(yīng)頭, 有多個(gè)key-value組成Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked Date: Fri, 09 Oct 2015 08:55:42 GMT

    響應(yīng)行介紹:

    HTTP/1.1 302 Found
    1基本介紹
    1.HTTP/1.1:采用的協(xié)議
    2.302:狀態(tài)碼
    常見(jiàn)的狀態(tài):
    200 : 表示請(qǐng)求處理完成并完美返回
    302: 表示請(qǐng)求需要進(jìn)一步細(xì)化。
    404: 表示客戶訪問(wèn)的資源找不到。
    500: 表示服務(wù)器的資源發(fā)送錯(cuò)誤。(服務(wù)器內(nèi)部錯(cuò)誤)
    3.Found:狀態(tài)描述,常見(jiàn)ok和found
    2. servlet中的方法
    tomcat服務(wù)器把請(qǐng)求信息封裝到HttpServletRequest對(duì)象,且把響應(yīng)信息封裝到HttpServletResponse
    response.setStatus(404);//設(shè)置狀態(tài)碼
    response.sendError(404);// 設(shè)置錯(cuò)誤頁(yè)面
    3. 演示如下

    response.setStatus(404);//錯(cuò)誤代碼,沒(méi)有反應(yīng)response.sendError(404);// 發(fā)送404的狀態(tài)碼+404的錯(cuò)誤頁(yè)面#輸出結(jié)果:HTTP/1.1404 Not FoundServer: Apache-Coyote/1.1Content-Type: text/html;charset=ISO-8859-1Content-Language: enContent-Length: 949Date: Sat, 10 Oct 201513:09:53 GMT

    6

    響應(yīng)頭介紹:

    1. 基本介紹
    格式:Server: Apache-Coyote/1.1;Server響應(yīng)頭名,后面的是響應(yīng)值;
    頭里面主要包括:Server,服務(wù)器類型;Location:跳轉(zhuǎn)網(wǎng)頁(yè)地址 Conten*:實(shí)體內(nèi)容
    2. servlet中的方法
    response.setHeader(“server”, “JBoss”);修改對(duì)應(yīng)頭名的內(nèi)容;
    3. 演示如下

    //修改服務(wù)器類型 response.setHeader("server", "JBoss");/** * 修改實(shí)體內(nèi)容 *///瀏覽器能直接看到的內(nèi)容就是實(shí)體內(nèi)容 response.getWriter().println("hello peace");//字符內(nèi)容,常用//response.getOutputStream().write("hello world".getBytes());//字節(jié)內(nèi)容。不能兩個(gè)同時(shí)使用#輸出如下:HTTP/1.1200 OKserver: JBossContent-Length: 12Date: Sat, 10 Oct 201513:11:04 GMTHTTP/1.1200 OKserver: JBossContent-Length: 12Date: Sat, 10 Oct 201513:11:04 GMT

    7

    幾個(gè)常要的方面介紹

    1. 測(cè)試重定向:與轉(zhuǎn)發(fā)不同
    /** * 測(cè)試重定向:與轉(zhuǎn)發(fā)不同 * 使用請(qǐng)求重定向:發(fā)送一個(gè)302狀態(tài)嗎+location的響應(yīng) *  */response.setStatus(302);//設(shè)置狀態(tài)碼response.setHeader("location", "/HttpSer/adv.html");//設(shè)置重定向頁(yè)面//簡(jiǎn)單寫法// response.sendRedirect("/HttpSer/adv.html");#輸出:HTTP/1.1302 FoundServer: Apache-Coyote/1.1location: /HttpSer/adv.htmlContent-Length: 12Date: Sat, 10 Oct 201513:15:26 GMT
    1. 定時(shí)刷新:
    /** * 定時(shí)刷新 * 原理:瀏覽器解析refresh頭,得到頭之后重新請(qǐng)求當(dāng)前資源 * */ //response.setHeader("refresh", "1");//每隔1秒刷新一次 //隔5秒后轉(zhuǎn)到另外的資源 //response.setHeader("refresh", "5;url=/HttpSer/adv.html");#輸出: HTTP/1.1200 OKServer: Apache-Coyote/1.1refresh:1Content-Length: 12Date: Sat, 10 Oct 201513:18:39 GMTHTTP/1.1200 OKServer: Apache-Coyote/1.1refresh:5;url=/HttpSer/adv.htmlContent-Length: 12Date: Sat, 10 Oct 201513:21:29 GMT
    1. 設(shè)置編碼:
    response.setCharacterEncoding("utf-8");
    /** * 1. 服務(wù)器發(fā)送給瀏覽器的數(shù)據(jù)類型和內(nèi)容編碼 *///response.setHeader("content-type", "text/html");//設(shè)置內(nèi)容為html//response.setContentType("text/html;charset=utf-8");//和上面代碼等價(jià)。推薦使用此方法//response.setContentType("text/xml");//設(shè)置內(nèi)容為xml//response.setContentType("image/png");//設(shè)置內(nèi)容為圖片
    1. 設(shè)置為下載方式打開(kāi)文件:
    /** * 設(shè)置以下載方式打開(kāi)文件 *///response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
    1. 發(fā)送硬盤圖片給瀏覽器:
     File file = new File("/media/peace/本地磁盤/andriod/1.png");//WebContent/** * 發(fā)送圖片 */ FileInputStream in = new FileInputStream(file);byte[] buf = newbyte[1024];int len = 0;
    //把圖片內(nèi)容寫出到瀏覽器while( (len=in.read(buf))!=-1 ){ response.getOutputStream().write(buf, 0, len); }

    來(lái)自一條小鯊魚(rlovep.com)
    代碼下載

    posted on 2015-12-26 14:42 王和平 閱讀(3958) 評(píng)論(4)  編輯  收藏 所屬分類: javaWeb

    Feedback

    # re: http協(xié)議介紹(servlet) 2015-12-28 16:17 龍騎流江

    我點(diǎn)進(jìn)來(lái)的時(shí)候以為是講http協(xié)議的,看完了發(fā)現(xiàn)文不對(duì)題,是寫怎么傳遞傳輸  回復(fù)  更多評(píng)論   

    # re: http協(xié)議介紹(servlet)[未登錄](méi) 2015-12-28 21:29 王和平

    @龍騎流江
    您覺(jué)得應(yīng)該是怎樣的  回復(fù)  更多評(píng)論   

    # re: http協(xié)議介紹(servlet)[未登錄](méi) 2016-01-04 10:55 Eric Yang

    還不能夠完全理解  回復(fù)  更多評(píng)論   

    # re: http協(xié)議介紹(servlet) 2016-01-05 12:08 sqlyun

    這個(gè)寫的有點(diǎn)底層傳輸了,看著有點(diǎn)難,不過(guò)還是感謝分享。  回復(fù)  更多評(píng)論   


    My Links

    Blog Stats

    常用鏈接

    留言簿

    隨筆分類

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 免费看又黄又无码的网站| 午夜视频免费在线观看| 久久亚洲国产精品五月天婷| 青柠影视在线观看免费| 亚洲国产精品美女| 亚洲日本韩国在线| 日韩免费一区二区三区在线播放| 亚洲精品无码少妇30P| 精品亚洲一区二区| 在线免费观看毛片网站| 国产亚洲精品免费视频播放| 国产精品久久亚洲不卡动漫| 亚洲无人区一区二区三区| 亚洲性线免费观看视频成熟| 色多多www视频在线观看免费| 亚洲欧洲精品国产区| 亚洲男人第一无码aⅴ网站| 国产桃色在线成免费视频| 一个人看的www在线免费视频| 亚洲人成www在线播放| 亚洲国产成人高清在线观看| 国产午夜免费秋霞影院| h在线观看视频免费网站| 成人国产精品免费视频| 国产精品亚洲AV三区| 亚洲妓女综合网99| 亚洲国产精品嫩草影院在线观看| 国产片免费福利片永久| 成人福利免费视频| 久久爰www免费人成| 一级毛片aaaaaa视频免费看| 亚洲综合一区二区三区四区五区 | 亚洲国模精品一区| 国外成人免费高清激情视频 | 全黄a免费一级毛片人人爱| 国产精彩免费视频| 国产成人久久AV免费| caoporm超免费公开视频| 亚洲av永久无码| 亚洲AV综合色区无码二区偷拍| 久久亚洲伊人中字综合精品|