JavaWeb - Response筆記

主要內容

  1. HTTP協議:響應消息
  2. Response對象
  3. ServletContext對象

一、HTTP協議:

1-請求消息:客戶端發送給服務器端的數據

數據格式:請求行、請求頭、請求空行、請求體;

2-響應消息:服務器端發送給客戶端的數據

數據格式:響應行、頭、空行、體

1-響應行

  1. 組成:協議/版本 響應狀態碼 狀態碼描述
  2. 響應狀態碼:服務器告訴客戶端瀏覽器本次請求和響應的一個狀態。
    1. 狀態碼都是3位數字
    2. 分類:

      1xx:服務器接收客戶端消息,但沒有接收完成,等待一段時間後,發送1xx多狀態碼
      2xx:成功。代表:200
      3xx:重定向。代表:302(重定向),304(訪問緩存)
      4xx:客戶端錯誤。
      代表:
      404:請求路徑沒有對應的資源)
      405:請求方式沒有對應的doXxx方法
      5xx:服務器端錯誤。代表:500(服務器內部出現異常)

2-響應頭

  1. 格式:頭名稱: 值
    2. 常見的響應頭:

     > Content-Type:服務器告訴客戶端本次**<u>響應體數據格式以及編碼格式</u>**
     >
     > Content-disposition:服務器告訴客戶端**以什麼格式打開響應體數據**
     > 	值:
     > 		in-line:默認值,在當前頁面內打開
     > 		attachment;filename=xxx:以附件形式打開響應體。文件下載
    

3-響應空行

4-響應體:傳輸的數據

<!-- 響應字符串格式 -->
HTTP/1.1 200 OK
Content-Type: text/html;charset=UTF-8
Content-Length: 101
Date: Wed, 06 Jun 2018 07:08:42 GMT
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
  hello , response
  </body>
</html>

二、Response對象

功能:設置響應消息

設置響應行

  1. 格式:HTTP/1.1 200 ok
  2. 設置狀態碼:setStatus(int sc)

設置響應頭:

​ setHeader(String name, String value)

設置響應體:

  • 使用步驟:
    1. 獲取輸出流:

字符輸出流:PrintWriter getWriter()
字節輸出流:ServletOutputStream getOutputStream()

2. 使用輸出流,將數據輸出到客戶端瀏覽器。

案例:

案例一:完成重定向

  • 重定向:資源跳轉的方式

  • 代碼實現:

    //訪問/responseDemo1,會自動跳轉到/responseDemo2資源
    //1.設置狀態碼爲302
    resp.setStatus(302);
    //2.設置響應頭location
    resp.setHeader("location", "/response/responseDemo2");
    
    //以上兩步可以換成下面的語句
    resp.sendRedirect("/response/responseDemo2");
    
  • 重定向redirect和轉發forward

    重定向redirect 轉發forward
    地址欄發生變化:responseDemo1變成responseDemo2 轉發地址欄路徑不變
    重定向可以訪問其他站點(服務器)的資源 轉發只能訪問當前服務器下的資源
    重定向是兩次請求。
    先請求到Demo1,再鏈接到Demo2
    不能使用request對象來共享數據:
    轉發是一次請求。可以使用request對象來共享數據
    //Demo1
    req.setAttribute("msg", "response");
    //Demo2
    Object msg = req.getAttribute("msg");
    System.out.println(msg);
    //重定向:不能共享數據--Demo2不能獲取到msg的值
    //轉發:共享數據
    
  • 路徑寫法:

    路徑分類

    1. 相對路徑:通過相對路徑不可以確定唯一資源

如:./index.html
不以/開頭,以.開頭路徑
規則:找到當前資源和目標資源之間的相對位置關係

  	./:當前目錄;../:後退一級目錄
  
  ```html
  <!DOCTYPE html>
  <html lang="en">
  <head>
      <meta charset="UTF-8">
      <title>Title</title>
  </head>
  <body>
      <h1>找到當前資源和目標資源之間的相對位置關係</h1>
      <p>
          當前資源:location.html
          http://localhost/response/location.html
      </p>
      <p>
          目標資源:
          http://localhost/response/responseDemo2
      </p>
      <a href="./responseDemo2">
          responseDemo2
      </a>
  </body>
  </html>
  ```


2. 絕對路徑:通過絕對路徑可以確定唯一資源

  如:http://localhost/response/responseDemo2		/response/responseDemo2

以/開頭的路徑
規則:判斷定義的路徑是給誰用的?判斷請求將來從哪兒(客戶端/服務器)發出

  1. 給客戶端瀏覽器使用:需要加虛擬目錄(項目的訪問路徑)
  
     建議虛擬目錄動態獲取:request.getContextPath()
     <a> , <form>  重定向...
  
  1. 給服務器使用:不需要加虛擬目錄
     
     - 轉發路徑


案例二:服務器輸出字符數據到瀏覽器

  • 步驟:
    1. 獲取字符輸出流
    2. 輸出數據
  • 注意:

    亂碼問題:

    PrintWriter pw = response.getWriter();獲取的流的默認編碼是ISO-8859-1

  1. 設置該流的默認編碼:
    response.setCharacterEncoding(“utf-8”);
  2. 告訴瀏覽器響應體使用的編碼:
    response.setHeader(“content-type”,“text/html;charset=utf-8”);
  3. //簡單的形式,設置編碼:
    response.setContentType(“text/html;charset=utf-8”);

案例三:服務器輸出字節數據到瀏覽器

  • 步驟:
    1. 獲取字節輸出流
    2. 輸出數據

補充案例:驗證碼

  1. 本質:圖片
  2. 目的:防止惡意表單註冊
package web.servlet;

@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int width = 100;
        int height = 50;

        //1.創建一個對象,在內存中圖片(驗證碼對象),Ctrl+P查看方法參數
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        //2.美化圖片
        //2.1 填充背景色
        Graphics g = image.getGraphics();//畫筆對象
        g.setColor(Color.pink);//設置畫筆顏色
        g.fillRect(0, 0, width, height);//填充顏色

        //2.2繪製邊框
        g.setColor(Color.BLUE);
        g.drawRect(0, 0, width - 1, height - 1);//邊框佔一個像素,故-1

        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        //生成隨機角標
        Random ran = new Random();

        //2.3寫驗證碼
        for (int i = 1; i < 5; i++) {
            int index1 = ran.nextInt(str.length());
            char ch1 = str.charAt(index1);//隨機字符,ch+""==轉換成String
            g.drawString(ch1+"", width/5*i, height/2);
        }

        //2.4畫干擾線
        g.setColor(Color.GREEN);//干擾線顏色
        for (int i = 1; i < 11; i++) {
            int x1 = ran.nextInt(width);
            int x2 = ran.nextInt(width);

            int y1 = ran.nextInt(height);
            int y2 = ran.nextInt(height);
            g.drawLine(x1, y1, x2, y2);

        }
        //3.將圖片輸出到頁面顯示
        ImageIO.write(image, "jpg", resp.getOutputStream());

    }
}

/*
        有了驗證碼後,更換時需要點擊圖片或者看不清換一張,這時候就需要綁定時間
        分析:點擊超鏈接或者圖片,需要換一張
            1.給超鏈接和圖片綁定單擊事件
            2.重新設置圖片的src屬性
         */

<!-- register.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script>
        /*
        有了驗證碼後,更換時需要點擊圖片或者看不清換一張,這時候就需要綁定時間
        分析:點擊超鏈接或者圖片,需要換一張
            1.給超鏈接和圖片綁定單擊事件
            2.重新設置圖片的src屬性
         */
        window.onload=function () {
            //1.獲取圖片對象
            var img = document.getElementById("checkcode");
            //2.綁定單擊事件
            img.onclick = function () {
                //加時間戳
                var date = new Date().getTime();
                img.src = "/response/checkCodeServlet?" + date;
            }
        }
    </script>
</head>
<body>
    <img id="checkcode" src="/response/checkCodeServlet"/>
    <a id="changecode" href="">看不清換一張</a>
</body>
</html>

三、ServletContext對象:

1-概念:

代表整個web應用,可以和**程序的容器(服務器)**來通信

2-獲取:

  1. 通過request對象獲取
    request.getServletContext();
  2. 通過HttpServlet獲取
    this.getServletContext();
//1. 通過request對象獲取
ServletContext context1 = req.getServletContext();
//2. 通過HttpServlet獲取
ServletContext context2 = this.getServletContext();

System.out.println(context1);
System.out.println(context2);

System.out.println(context1 == context2);//true

3-功能:

1-獲取MIME類型:

  • MIME類型:在互聯網通信過程中定義的一種文件數據類型

  • 格式: 大類型/小類型 text/html image/jpeg

  • 獲取:String getMimeType(String file)

    //2.通過HTTPServlet獲取
    ServletContext context = this.getServletContext();
    //3.定義文件名稱
    String filename = "a.jpg";
    //4.獲取MIME類型
    String mimeType = context.getMimeType(filename);
    System.out.println(mimeType);
    

2-域對象:共享數據

  1. setAttribute(String name,Object value)
  2. getAttribute(String name)
  3. removeAttribute(String name)
  • ServletContext對象範圍:所有用戶所有請求的數據
//Demo3設置請求時間
//2.通過HTTPServlet獲取
ServletContext context = this.getServletContext();
//設置數據
context.setAttribute("time","2020年2月9日12:09:48");
System.out.println("設置請求時間爲:2020年2月9日12:09:48");

//Demo4通過另外一個客戶端(瀏覽器)獲取時間
//2.通過HTTPServlet獲取
ServletContext context = this.getServletContext();
//獲取數據
Object msg = context.getAttribute("time");
System.out.println(msg);

3-獲取文件的真實(服務器)路徑

  • 方法:String getRealPath(String path)

    // 通過HttpServlet獲取
    ServletContext context = this.getServletContext();
    // 獲取文件的服務器路徑
    String b = context.getRealPath("/b.txt");//web目錄下資源訪問
    System.out.println(b);
    // File file = new File(realPath);
    
    String c = context.getRealPath("/WEB-INF/c.txt");//WEB-INF目錄下的資源訪問
    System.out.println(c);
    
    String a = context.getRealPath("/WEB-INF/classes/a.txt");//src目錄下的資源訪問
    System.out.println(a);
    

2020年2月9日12:31:08

案例:文件下載需求:

  1. 頁面顯示超鏈接
  2. 點擊超鏈接後彈出下載提示框
  3. 完成圖片文件下載

分析:

  1. 超鏈接指向的資源如果能夠被瀏覽器解析,則在瀏覽器中展示,如果不能解析,則彈出下載提示框。不滿足需求。
  2. 任何資源都必須彈出下載提示框。
  3. 使用響應頭設置資源的打開方式:content-disposition:attachment;filename=xxx。

步驟:

  1. 定義頁面,編輯超鏈接href屬性,指向Servlet,傳遞資源名稱filename

    <a href="/response/downloadServlet?filename=1.jpg">圖片1</a>
    <a href="/response/downloadServlet?filename=1.jpg">視頻1</a>
    
  2. 定義Servlet

  3. 獲取文件名稱

  4. 使用字節輸入流加載文件進內存

  5. 指定response的響應頭: content-disposition:attachment;filename=xxx

  6. 將數據寫出到response輸出流

//1.獲取請求參數,文件名稱
String filename = req.getParameter("filename");

//2.使用字節輸入流加載文件進內存
//2.1找到文件服務器路徑
ServletContext servletContext = this.getServletContext();
String realpath = servletContext.getRealPath("/img/" + filename);
//2.2用字節流關聯
FileInputStream fis = new FileInputStream(realpath);

//3.設置response的響應頭
//3.1設置響應頭類型:content-type
String mimeType = servletContext.getMimeType(filename);//獲取文件的mime類型
resp.setHeader("content-type", mimeType);

//3.2設置響應頭打開方式:content-disposition
resp.setHeader("content-disposition", "attachment;filename=" + filename);

//        //解決中文文件名問題
//        //1.獲取user-agent請求頭
//        String agent = req.getHeader("user-agent");
//        //2.使用工具類方法編碼文件名即可
//        filename = DownLoadUtils.getFileName(agent, filename);

//4.將輸入流的數據寫出到輸出流中
ServletOutputStream sos = resp.getOutputStream();

byte[] buff=new byte[1024*8];
int len = 0;
while ((len=fis.read(buff))!=-1){
    sos.write(buff, 0, len);
}
fis.close();

問題:

  • 中文文件問題

    工具類Utils不需要自己寫!!!

    • 解決思路:
      1. 獲取客戶端使用的瀏覽器版本信息
      2. 根據不同的版本信息,設置filename的編碼方式不同
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章