服务器可以响应字符类型的数据吗,HTTP - Response
HTTP - Response
基本概念
數據格式
Response對象
功能
設置響應消息
1. 完成重定向
重定向和轉發的區別(forward 和 redirect):
路徑的寫法
1. 路徑的分類
1. 相對路徑, 通過相對路徑不可以確定唯一資源
2. 絕對路徑, 通過絕對路徑可以確定唯一資源
如何判斷絕對路徑是否要加虛擬目錄
動態獲取虛擬目錄
2. 服務器輸出字符數據到瀏覽器
驗證碼實例
基本概念
響應消息: 服務器端發送給客戶端的數據
數據格式
1. 響應行
1. 組成: 協議/版本響應狀態碼狀態碼描述
HTTP/1.1200OK
2. 響應狀態碼: 服務器告訴客戶端瀏覽器本次請求和響應的一個狀態
1. 狀態碼都是3位數字
2. 分類
1. 1xx: 服務器接收客戶端學習, 但沒有接收完成, 等待一段時間后, 發送1xx狀態碼
2. 2xx: 成功
代表: 200
3. 3xx: 重定向
代表: 302
4. 4xx
代表: 404(請求路徑沒有對應的資源)
405(請求方式沒有對應的方法(doPost(), doGet()))
5. 5xx
代表: 500, 服務器內部異常
2. 響應頭
1. 格式
頭名稱: 值
2. 常見響應頭
1. Content-Type: 服務器告訴客戶端本次相應提數據格式以及編碼格式
2. Content-disposition: 服務器告訴客戶端以什么格式打開相應體數據
值:
in-line: 默認值, 在當前頁面內打開
attachment: 以附件形式打開響應體, 文件下載時使用
3. 響應空行
一個空行, 分隔響應頭和響應體
4. 響應體
傳輸的數據
5. 響應字符串格式
HTTP/2 200 OK
server: JSP3/2.0.14
date: Tue, 23 Jun 2020 12:52:13 GMT
content-type: application/javascript
content-encoding: gzip
etag: "4e097-5a8b7ef21bb00"
last-modified: Tue, 23 Jun 2020 03:27:08 GMT
expires: Fri, 21 Jun 2030 04:44:30 GMT
age: 29263
accept-ranges: bytes
cache-control: max-age=315360000
vary: Accept-Encoding,User-Agent
ohc-cache-hit: jhcm76 [4]
ohc-response-time: 1 0 0 0 0 0
X-Firefox-Spdy: h2
Response對象
功能
設置響應消息
1. 設置響應行
1. 格式: HTTP/1.1200OK
2. 設置狀態碼: setStatus(int sc)
2. 設置響應頭
setHeader(String name, String value)
3. 設置響應體
使用步驟
1. 獲取輸出流
字符輸出流: PrintWriter getWriter()
字節輸出流: ServletOutputStream getOutputStream()
2. 使用輸出流, 將數據輸出到客戶端瀏覽器
案例
1. 完成重定向
重定向: 資源跳轉的方式
1. 告訴瀏覽器重定向, 狀態碼302
2. 告訴瀏覽器B資源的路徑, 響應頭location: B資源的路徑
public class ResponseDemo01 extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("demo01......");
//訪問/responseDemo01, 會自動調轉到/responseDemo02
//1. 設置狀態碼為302
resp.setStatus(302);
//2. 設置響應頭location
resp.setHeader("location", "/response/responseDemo02");
//簡單的重定向方法
//resp.sendRedirect("/response/responseDemo02");
}
}
重定向和轉發的區別(forward 和 redirect):
重定向的特點:
1. 地址欄發生變化
2. 重定向可以訪問其他站點(服務器)的資源
3. 重定向是兩次請求, 不能使用request對象共享數據
轉發的特點:
1. 轉發地址欄路徑不變
2. 轉發只能訪問當前服務器下的資源
3. 轉發是一次請求, 可以使用request對象共享數據
路徑的寫法
1. 路徑的分類
1. 相對路徑, 通過相對路徑不可以確定唯一資源
如
./index.html
不以 / 開頭, 以 . 開頭
規則: 找到訪問當前資源和目標資源之間的相對位置關系
./ : 當前資源所在的目錄, 可以省略
../ : 當前資源后退一級的目錄
2. 絕對路徑, 通過絕對路徑可以確定唯一資源
如
http://localhost/response/responseDemo02
/response/responseDemo02
以 / 開頭路徑為絕對路徑
如何判斷絕對路徑是否要加虛擬目錄
* 規則: 判斷定義的路徑是給誰用的(判斷請求將來從哪發出)
* 給客戶端瀏覽器使用: 需要加虛擬目錄(項目的訪問路徑)
點擊超鏈接
資源重定向
* 給服務器使用: 不需要加虛擬目錄
資源跳轉
動態獲取虛擬目錄
String request.getContextPath();
2. 服務器輸出字符數據到瀏覽器
步驟
獲取字符輸出流
輸出數據
注意亂碼問題
PrintWriter writer = resp.getWriter()獲取的流的默認編碼是ISO-8859-1
設置該流的編碼
告訴瀏覽器響應體使用的編碼
實例:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1. 獲取流對象之前, 設置流的默認編碼 ISO-8859-1 -> GBK
// resp.setCharacterEncoding("GBK");
//2. 告訴瀏覽器, 服務器發送的消息體數據的編碼, 建議瀏覽器使用該編碼解碼
//下面這行代碼同時會設置輸出流的編碼形式為utf-8, 所以不需要再寫第一行代碼
// resp.setHeader("content-type", "text/html;charsert=utf-8");
//簡單的形式, 設置編碼
resp.setContentType("text/html;charset=utf-8");
// 1. 獲取字符輸出流
PrintWriter writer = resp.getWriter();
//2. 輸出數據
writer.write("
hello response
");
writer.write("
你好
");
//輸出流不需要刷新, 會自動刷新
}
服務器輸出字節數據到瀏覽器
步驟
獲取字符輸出流
輸出數據
實例:
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設置編碼
resp.setContentType("text/html;charset=utf-8");
//1. 獲取字節輸出流
ServletOutputStream outputStream = resp.getOutputStream();
//2. 輸出數據
outputStream.write("你好".getBytes("utf-8"));
}
驗證碼實例
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
int width = 100;
int height = 50;
//1. 創建對象, 在內存中存圖片(驗證碼圖片對象)
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//2. 美化圖片
//2.1 填充背景色
Graphics graphics = image.getGraphics();//畫筆對象
graphics.setColor(Color.PINK);//設置顏色
graphics.fillRect(0, 0, width, height);//填充矩形
//2.2 畫邊框
graphics.setColor(Color.BLUE);
graphics.drawRect(0, 0, width-1, height-1);//畫矩形邊框
//寫驗證碼
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
str += str.toLowerCase();
str += "0123456789";
//生成隨機角標
Random random = new Random();
for (int i = 1; i <=5 ; i++) {
//獲取字符
int index = random.nextInt(str.length());
char ch = str.charAt(index);
graphics.setColor(Color.BLUE);
graphics.drawString(ch+"", width/5*i, height/2);
}
//2.4 畫干擾線
graphics.setColor(Color.GREEN);
for (int i = 0; i < 10; i++) {
//隨機生成坐標點
int x1 = random.nextInt(width);
int x2 = random.nextInt(width);
int y1 = random.nextInt(height);
int y2 = random.nextInt(height);
//畫線
graphics.drawLine(x1,x2,y1,y2);
}
//3. 將圖片輸出展示
ImageIO.write(image, "jpg", resp.getOutputStream());
resp.setContentType("text/html;utf-8");
PrintWriter writer = resp.getWriter();
writer.write("nihao圖片");
}
總結
以上是生活随笔為你收集整理的服务器可以响应字符类型的数据吗,HTTP - Response的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 知音难寻的下一句是什么呢?
- 下一篇: 浪潮服务器怎么装虚拟机,VMware 6