本文是servlet的入門篇,主要簡單介紹下http協議html
__ 1.http協議:__java
2.查看http協議的工具:git
GET / HTTP/1.1 Host: www.baidu.com
GET /HttpSer HTTP/1.1 Host: localhost:8080 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-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.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8
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
下面將對這兩個協議進行介紹:github
GET /HttpSer HTTP/1.1-請求行 Host: localhost:8080--請求頭;有多個key-value組成 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-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.36 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 --可選,實體內容;
GET /HttpSer HTTP/1.1 -請求行
1.請求資源的URL和URI比較:
URL: 統一資源定位符。http://localhost:8080/HttpSer/index.html。只能定位互聯網資源。是URI 的子集.
URI: 統一資源標記符。/HttpSer/index.html。用於標記任何資源。能夠是本地文件系統,局域網的資源(//192.168.14.10/HttpSer/index.html),能夠是互聯網。
2.請求方式:
常見的請求方式: GET 、 POST、 HEAD、 TRACE、 PUT、 CONNECT 、DELETE
經常使用的請求方式: GET(有將實體信息放在瀏覽器地址欄) 和 POST(隱藏實體內容)
3. servlet得到請求行信息:_web
/*** 1.1請求行的得到*/ System.out.println("請求方式:"+request.getMethod());//得到提交方式 System.out.println("請求URI:"+request.getRequestURI());//得到uri System.out.println("請求url:"+request.getRequestURL());//得到url System.out.println("得到協議:"+request.getProtocol());//得到所用協議 ##輸出: 請求方式:GET 請求URI:/HttpSer/TestRequst 請求url:http://localhost:8080/HttpSer/TestRequst 得到協議:HTTP/1.1
4. 測試提交方式:
新創建web工程,創建TestMethod.html文件:
創建Servlet類TestMethod.java修改get和post方法:chrome
protected void doGet(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) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out.println("post 方式提交"); }
運行tomcat能夠看淡提交方式的不一樣:瀏覽器地址欄顯示的不一樣,servlet調用的方法也不一樣;
get提交
post提交
通過對比請求能夠發現
get方式在請求行多了?name=wang&pass=123
post多了實體內容:Content-Type: application/x-www-form-urlencoded
請求內容同以下:數組
#get方式: GET /HttpSer/TestMethod?name=wang&pass=123 HTTP/1.1 Host: localhost:8080 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Upgrade-Insecure-Requests: 1 User-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.36 Referer: http://localhost:8080/HttpSer/testMethod.html Accept-Encoding: gzip, deflate, sdch Accept-Language: en-US,en;q=0.8 #post方式: POST /HttpSer/TestMethod HTTP/1.1 Host: localhost:8080 Connection: keep-alive Content-Length: 18 Cache-Control: max-age=0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Origin: http://localhost:8080 Upgrade-Insecure-Requests: 1 User-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.36 Content-Type: application/x-www-form-urlencoded Referer: http://localhost:8080/HttpSer/testMethod.html Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.8
1. 請求頭介紹:
請求頭主要包含一些有用信息:
1.Host: localhost:8080主機地址和端口
2.Connection: keep-alive 鏈接方式
3.User-Agent:瀏覽器的一些信息
4.Referer:來訪頁面
5.Content:實體內容;post纔有
2. servlet得到請求頭主要的方法:
request.getHeader("Host"));經過建得到相應請求的內容;
Enumeration
3. 演示以下:
修改doget方法
/*** 設置參數查詢的編碼 *該方法只能對請求實體內容的數據編碼起做用。POST提交的數據在實體內容中,因此該方法對POST方法有效! *GET方法的參數放在URI後面,因此對GET方式無效!!! */ request.setCharacterEncoding("utf-8"); /** * 1.1請求行的得到 */ System.out.println("請求方式:"+request.getMethod()); System.out.println("請求URI:"+request.getRequestURI()); System.out.println("請求url:"+request.getRequestURL()); System.out.println("得到協議:"+request.getProtocol()); /** * 1.2請求頭的得到 */ //經過鍵得到請求頭的內容 System.out.println("得到host:"+request.getHeader("Host")); System.out.println("得到瀏覽器的User-Agent:"+request.getHeader("User-Agent")); //經過迭代器迭代,得到鍵 在取鍵值 Enumeration<String> headerNames = request.getHeaderNames(); while(headerNames.hasMoreElements()){ String key=headerNames.nextElement(); System.out.println(key+":"+request.getHeader(key)); } /** * 獲得請求實體內容 * 好比:實體爲name=peace&pass=1234 */ ServletInputStream in = request.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); } #輸出以下: 請求方式:GET 請求URI:/HttpSer/TestRequst 請求url:http://localhost:8080/HttpSer/TestRequst 得到協議: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.36 host:localhost:8080 connection:keep-alive accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 upgrade-insecure-requests:1 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.36 referer:http://localhost:8080/HttpSer/testMethod.html accept-encoding:gzip, deflate, sdch accept-language:en-US,en;q=0.8 cookie:CNZZDATA1255712369=1133597550-1443969628-%7C1443969628//此去後面文章會介紹;
1. 輸入參數:
輸入參數:
對於get來講就是跟在url後面的內容
/TestParam?name="peace"&password="sisi" ;name="peace"&password="sisi"這就是輸入參數
對於post來講就是實體內容,不可見
2. Servlet中得到輸入參數的方法:
String name=request.getParameter("name");得到對應輸入參數名字的內容
Enumeration
String[] hobits = request.getParameterValues(names);若是對應名字的內容是一個數組,使用這個方法得到,好比複選框
3. 演示以下:
1.創建testParam.html
2.修改doget方法:服務器
/** * 設置參數查詢的編碼 * 該方法只能對請求實體內容的數據編碼起做用。POST提交的數據在實體內容中,因此該方法對POST方法有效! * GET方法的參數放在URI後面,因此對GET方式無效!!! */ request.setCharacterEncoding("utf-8"); //得到全部的方式 System.out.println("提交方式:"+request.getMethod()); //得到輸入參數 String name=request.getParameter("name"); String pass=request.getParameter("password"); System.out.println("name:"+name+",pass:"+pass); /*此去爲若是get方式提交出現亂碼,使用; * if("GET".equals(request.getMethod())){ password = new String(password.getBytes("iso-8859-1"),"utf-8"); }*/ System.out.println(">>>>>>>>>>>>>>>>>"); //得到全部輸入參數的名字 Enumeration<String> params = request.getParameterNames(); while(params.hasMoreElements()) { String names=params.nextElement(); //若是是複選框,使用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)); } } ##輸出結果以下: 提交方式:POST name:peace,pass:124 >>>>>>>>>>>>>>>>> name:peace password:124 gender:男 籍貫:湖南 hobit: 籃球,足球, info:一條小鯊魚peace id:001
HTTP/1.1 302 Found ---響應行 Server: Apache-Coyote/1.1 ---響應頭, 有多個key-value組成 Location: http://localhost:8080/HttpSer/ Transfer-Encoding: chunked Date: Fri, 09 Oct 2015 08:55:42 GMT
HTTP/1.1 302 Found
1基本介紹
1.HTTP/1.1:採用的協議
2.302:狀態碼
常見的狀態:
200 : 表示請求處理完成並完美返回
302: 表示請求須要進一步細化。
404: 表示客戶訪問的資源找不到。
500: 表示服務器的資源發送錯誤。(服務器內部錯誤)
3.Found:狀態描述,常見ok和found
2. servlet中的方法
tomcat服務器把請求信息封裝到HttpServletRequest對象,且把響應信息封裝到HttpServletResponse
response.setStatus(404);//設置狀態碼
response.sendError(404);// 設置錯誤頁面
3. 演示以下
response.setStatus(404);//錯誤代碼,沒有反應 response.sendError(404);// 發送404的狀態碼+404的錯誤頁面 #輸出結果: HTTP/1.1 404 Not Found Server: Apache-Coyote/1.1 Content-Type: text/html;charset=ISO-8859-1 Content-Language: en Content-Length: 949 Date: Sat, 10 Oct 2015 13:09:53 GMT
1. 基本介紹
格式:Server: Apache-Coyote/1.1;Server響應頭名,後面的是響應值;
頭裏面主要包括:Server,服務器類型;Location:跳轉網頁地址 Conten*:實體內容
2. servlet中的方法
response.setHeader("server", "JBoss");修改對應頭名的內容;
3. 演示以下
//修改服務器類型 response.setHeader("server", "JBoss"); /** * 修改實體內容 */ //瀏覽器能直接看到的內容就是實體內容 response.getWriter().println("hello peace");//字符內容,經常使用 //response.getOutputStream().write("hello world".getBytes());//字節內容。不能兩個同時使用 #輸出以下: HTTP/1.1 200 OK server: JBoss Content-Length: 12 Date: Sat, 10 Oct 2015 13:11:04 GMTHTTP/1.1 200 OK server: JBoss Content-Length: 12 Date: Sat, 10 Oct 2015 13:11:04 GMT
/** * 測試重定向:與轉發不一樣 * 使用請求重定向:發送一個302狀態嗎+location的響應 * */ response.setStatus(302);//設置狀態碼 response.setHeader("location", "/HttpSer/adv.html");//設置重定向頁面 //簡單寫法 // response.sendRedirect("/HttpSer/adv.html"); #輸出: HTTP/1.1 302 Found Server: Apache-Coyote/1.1 location: /HttpSer/adv.html Content-Length: 12 Date: Sat, 10 Oct 2015 13:15:26 GMT
/** * 定時刷新 * 原理:瀏覽器解析refresh頭,獲得頭以後從新請求當前資源 * */ //response.setHeader("refresh", "1");//每隔1秒刷新一次 //隔5秒後轉到另外的資源 //response.setHeader("refresh", "5;url=/HttpSer/adv.html"); #輸出: HTTP/1.1 200 OK Server: Apache-Coyote/1.1 refresh: 1 Content-Length: 12 Date: Sat, 10 Oct 2015 13:18:39 GMT HTTP/1.1 200 OK Server: Apache-Coyote/1.1 refresh: 5;url=/HttpSer/adv.html Content-Length: 12 Date: Sat, 10 Oct 2015 13:21:29 GMT
response.setCharacterEncoding("utf-8"); /** * 1. 服務器發送給瀏覽器的數據類型和內容編碼 */ //response.setHeader("content-type", "text/html");//設置內容爲html //response.setContentType("text/html;charset=utf-8");//和上面代碼等價。推薦使用此方法 //response.setContentType("text/xml");//設置內容爲xml //response.setContentType("image/png");//設置內容爲圖片
/** * 設置如下載方式打開文件 */ //response.setHeader("Content-Disposition", "attachment; filename="+file.getName());
File file = new File("/media/peace/本地磁盤/andriod/1.png");//WebContent /** * 發送圖片 */ FileInputStream in = new FileInputStream(file); byte[] buf = new byte[1024]; int len = 0; //把圖片內容寫出到瀏覽器 while( (len=in.read(buf))!=-1 ){ response.getOutputStream().write(buf, 0, len); }
來自一條小鯊魚(rlovep.com)
代碼下載