HTTP協(xié)議
- 基于TCP連接的傳輸協(xié)議
- 默認端口是80
- 基于請求—響應(yīng)模式的協(xié)議
HTTP協(xié)議的請求頭
* GET http://localhost:8080/HelloWeb/Hello.html HTTP/1.1
* 告訴服務(wù)器請求的是那一個資源
* HTTP/1.1,使用的HTTP協(xié)議的版本
* Accept: text/html, application/xhtml+xml, image/jxr, */*
* 支持的數(shù)據(jù)格式/類型,
* */*: 代表我什么類型都支持
* Accept-Language: zh-CN
* 支持的語言格式
* User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko
* 瀏覽器的類型和操作系統(tǒng)的信息
* Accept-Encoding: gzip, deflate
* 支持的編碼方式,gzip數(shù)據(jù)壓縮模式,deflate默認
* Host: localhost:8080
* 請求的主機名
* Connection: Keep-Alive(長連接)
HTTP協(xié)議的響應(yīng)頭
* 響應(yīng)碼:
* 重要的響應(yīng)碼
* 200 : 請求成功
* 302 : 重定向
* 304 : 請求的頁面沒有發(fā)生改變
* 404 : 請求的資源不存在
* 500 : 服務(wù)器內(nèi)部錯誤
* Location: 主要和302響應(yīng)碼結(jié)合,實現(xiàn)重定向的功能
* Server:服務(wù)器信息
* Content-Length:返回的數(shù)據(jù)的長度
* Content-Type:返回的數(shù)據(jù)的類型
* Last-Modified:標記當(dāng)前頁面最后修改時間.檢查頁面是否更新過,如果更新過,就請求新數(shù)據(jù);如果沒有更新過,使用緩存數(shù)據(jù).
* Refresh:1;url=xxx:第一個參數(shù)代表時間,多少秒后刷新當(dāng)前頁面,單位是秒;第二個參數(shù)代表要跳轉(zhuǎn)到的頁面
* Content-Disposition:attachment;filename=aaa.zip
* Expires:-1 禁止客戶端緩存數(shù)據(jù)
* Cache-Control:no-cache: 禁止客戶端緩存數(shù)據(jù)
* Date:服務(wù)器返回數(shù)據(jù)的時間(GMT)
HttpServletResponse
- setStatus(int sc) : 設(shè)置響應(yīng)碼
- setHeader(String name, String value) : 設(shè)置響應(yīng)頭
- getOutputStream() : 獲得字節(jié)流用于輸出響應(yīng)體內(nèi)容
- getWriter() : 獲得字符流用于輸出響應(yīng)體內(nèi)容
重定向:
法一:通過設(shè)置狀態(tài)碼和頭信息來實現(xiàn)重定向
//通過設(shè)置狀態(tài)碼和頭信息來實現(xiàn)重定向
response.setStatus(302);
response.setHeader("Location", request.getContextPath()+"/success.jsp");
法二:通過設(shè)定sendRedirect來重定向
response.sendRedirect(request.getContextPath()+"/success.jsp");
方法三:定時重定向
response.getWriter().write("一秒后跳轉(zhuǎn)");
response.setHeader("Refresh", "1;URL=" + request.getContextPath() + "/success.jsp");
注意:定時跳轉(zhuǎn)還可以通過在jsp中設(shè)置meta標簽
<meta content="3;url=http://www.baidu.com" http-equiv="refresh">
設(shè)置禁止緩存
通過設(shè)置響應(yīng)頭:
- 設(shè)置信息頭 : "Cache-Control", "no-cache"
- 設(shè)置信息頭 : "Pragma", "no-cache"
- 設(shè)置信息頭 : "Expires", "Thu, 01 Dec 1994 16:00:00 GMT"
- setDateHeader("Expires", -1) 注:因為setHeader("Expires",日期)比較難寫,所以啊,用setDateHeader代替
注意:由于市面上的瀏覽器比較多,支持的屬性不一致,所以通常情況下會把三個信息頭同時設(shè)置
文件下載
方法一:通過對應(yīng)文件的超鏈接(如果該文件文件格式瀏覽器可以解析,將不會下載)
方法二:通過Servlet程序
package org.luoluo.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* Created by luozhiyun on 17/1/16.
*/
@WebServlet(name = "CServlet",urlPatterns = "/servlet/CServlet")
public class CServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//獲取文件名
String fileName = request.getParameter("fileName");
//獲取文件類型
String mimeType = getServletContext().getMimeType(fileName);
//設(shè)置文件類型
response.setContentType(mimeType);
//獲取文件的絕對磁盤路徑
String path = getServletContext().getRealPath("/files/"+fileName);
//設(shè)置響應(yīng)頭,并以附件的方式存儲
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
//寫入IO流
FileInputStream fis = new FileInputStream(path);
//servlet的IO流不用關(guān),瀏覽器會自動處理
ServletOutputStream os = response.getOutputStream();
byte[] bytes = new byte[1024];
int read = -1;
while ((read = fis.read(bytes)) != -1) {
os.write(bytes,0,read);
}
fis.close();
}
}
HttpServletRequest
- request.getMethod() : 獲取請求方式
- request.getRequestURI() : 獲取請求資源路徑
- request.getQueryString() : 獲取查詢字符串
- request.getHeader() : 獲取請求頭信息
- request.getParameter() : 獲取查詢參數(shù)
Request獲得請求行相關(guān)數(shù)據(jù)
- 以下結(jié)果以請求 http://localhost:8080/HelloServlet/LineInfoServlet?name=zhangsan&age=12 為基準
- getRequestURL() : 獲取用戶在瀏覽器地址欄中輸入的完整地址,但不包含請求參數(shù),即"?"以后的數(shù)據(jù)
-
getRequestURI() : 獲取資源的絕對路徑
- 結(jié)果 : /HelloServlet/LineInfoServlet
-
getContextPath() : 獲取當(dāng)前Web應(yīng)用的路徑
- 結(jié)果 : /HelloServlet
- getServletPath() : 獲取當(dāng)前資源的路徑
- 結(jié)果 : /LineInfoServlet
- getQueryString() : 獲取查詢字符串
- 結(jié)果 : name=zhangsan&age=12
- getMethod() : 獲取請求方式
- 結(jié)果 : GET
- getRemoteAddr() : 獲取客戶端IP
- getRemoteHost() : 獲取客戶端主機
- getRemotePort() : 獲取客戶端端口號
- 結(jié)果 : 49881
在設(shè)定默認的servlet的時候,獲取靜態(tài)資源:
String requestURI = request.getRequestURI();
String path = requestURI.substring(request.getContextPath().length()+1);
String realPath = getServletContext().getRealPath(path);