JavaWeb——response

功能:設(shè)置響應(yīng)消息

1.設(shè)置響應(yīng)行

  • 格式:
HTTP/1.1  200 OK 
  • 設(shè)置狀態(tài)碼:
setStatus(int sc)

2.設(shè)置響應(yīng)頭

setHeader(String name,String value)

3.設(shè)置響應(yīng)體

使用步驟:

1、獲取輸出流

  • 字符輸出流:PrinWriter getWriter()
  • 字節(jié)輸出流:ServletOutstream getOutputstream()

2、使用輸出流,將數(shù)據(jù)輸出到客戶端瀏覽器

4、案例

4.1 、完成重定向

  • 重定向:資源跳轉(zhuǎn)的方式
  • 代碼實現(xiàn):
       //訪問 Response1 會自動跳轉(zhuǎn)到 Response2
        //1.設(shè)置狀態(tài)碼為 302
       response.setStatus(302);
        //2.設(shè)置響應(yīng)頭
       response.setHeader("location","/Response2");
       //簡單的重定向方法
        response.sendRedirect("/Response2");

forward 和 redirect 區(qū)別

  • 重定向的特點

1.地址欄發(fā)生變化
2.重定向可以訪問其他站點(服務(wù)器)的資源
3.重定向是兩次請求,不能使用 request 對象來共享數(shù)據(jù)

  • 轉(zhuǎn)發(fā)的特點

1.轉(zhuǎn)發(fā)地址路徑不變
2.轉(zhuǎn)發(fā)只能訪問當前服務(wù)器下的資源
3.轉(zhuǎn)發(fā)是一次請求,可以使用 request 對象來共享數(shù)據(jù)

  • 路徑寫法
    路徑分類

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

1> 如:./index.html
不以 / 開頭,以 . 開頭路徑
2> 規(guī)則:找到當前資源與目標資源之間的相對位置關(guān)系
./:當前目錄
../:后退一級目錄

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

1> 如:http://localhost:8080/Response2
以 /開頭的路徑

2> 規(guī)則:判斷定義的路徑是給誰用?判斷請求從哪發(fā)出

1.給客戶端瀏覽器使用,需要加虛擬目錄(項目的訪問路徑)
建議虛擬目錄動態(tài)獲?。?/code>request.getContextPath();

eg:<a>,<form>,重定向

舉例:

 String contextPath = request.getContextPath();
 //簡單的重定向方法
  response.sendRedirect(contextPath+"/Response2");

2.給服務(wù)器使用,不需要加虛擬目錄
eg: 轉(zhuǎn)發(fā)路徑

4.2 、服務(wù)器輸出字符數(shù)據(jù)到瀏覽器

步驟:

1.獲取字符輸出流
2.輸出數(shù)據(jù)

       //獲取字符輸出流
        PrintWriter pw = response.getWriter();
        //輸出數(shù)據(jù)
        pw.write("你好啊,response");

注意:

亂碼問題

        //獲取流對象之前,設(shè)置流的默認編碼
        response.setCharacterEncoding("utf-8");
        //告訴瀏覽器,服務(wù)器發(fā)送的消息體數(shù)據(jù)的編碼,建議瀏覽器使用該編碼解碼
        response.setHeader("content-type","text/html;charset=utf-8");
        //簡單的形式,設(shè)置編碼
        response.setContentType("text/html;charset=utf-8");

最簡單的操作,直接使用下句即可解決亂碼:

   response.setContentType("text/html;charset=utf-8");

4.3 服務(wù)器輸出字節(jié)碼數(shù)據(jù)到瀏覽器

步驟:

1.獲取字節(jié)輸出流
2.輸出數(shù)據(jù)

        response.setContentType("text/html;charset=utf-8");
        //獲取字節(jié)輸出流
        ServletOutputStream sos = response.getOutputStream();
        //輸出數(shù)據(jù)
        sos.write("你好".getBytes("utf-8"));

4.4 驗證碼

html

<script>
/*
   點擊超鏈接或者圖片換一張
   1.給圖片或者超鏈接綁定單機事件
   2.重新設(shè)置圖片 src 屬性值
 */
window.onload=function () {
//獲取圖片對象
 var img = document.getElementById("tupian");
 //綁定單擊事件
    img.onclick=function () {
        //加時間戳
        var date=new Date().getTime();
        img.src="/day/Checkcode?"+date;
    }
}
    </script>
</head>
<body>
<img id="tupian" src="/day/Checkcode">
<a  id="change" href="">看不清換一張?</a>
</body>

servlet

@WebServlet("/Checkcode")
public class Checkcode extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int width = 100;
        int height = 50;

        //1.創(chuàng)建一個對象,在內(nèi)存中畫圖
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //2.美化圖片
        //填充背景色
        Graphics g = image.getGraphics();//畫筆對象
        g.setColor(Color.pink);//設(shè)置畫筆顏色
        g.fillRect(0, 0, width, height);
        //畫邊框
        g.setColor(Color.blue);
        g.drawRect(0, 0, width - 1, height - 1);

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

        for (int i = 0; i < 4; i++) {
            int index = ran.nextInt(str.length());
            //獲取字符
            char ch = str.charAt(index);
            //寫驗證碼
            g.drawString(ch + "", width /5 * i, height / 2);

        }
        //畫干擾線
        g.setColor(Color.orange);
        //隨機生成坐標點
        for(int i=0;i<10;i++){
            int x1=ran.nextInt(width);
            int x2=ran.nextInt(width);
            int y1=ran.nextInt(width);
            int y2=ran.nextInt(width);
            g.drawLine(x1,x2,y1,y2);

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

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

效果展示:點擊圖片切換驗證碼


image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容