更新時間:2022-08-23 10:18:05 來源:動力節(jié)點(diǎn) 瀏覽1910次
與request相對,response對象是設(shè)置響應(yīng)消息。在瀏覽器按下F12鍵--點(diǎn)擊網(wǎng)絡(luò)--ctrl+R: 可以查看用戶在瀏覽器端每次操作時瀏覽器向服務(wù)器發(fā)送的響應(yīng)消息。
response作為tomcat服務(wù)器已實(shí)現(xiàn)的對象,最直接的用法就是在servlet類中調(diào)用response對象的方法,response對象的功能是設(shè)置響應(yīng)消息,所以方法的總體邏輯就是設(shè)置響應(yīng)消息,這類方法的關(guān)鍵在于對響應(yīng)消息的理解。感興趣的小伙伴也可以了解一下Tomcat教程,相信對大家的學(xué)習(xí)會有一定的幫助。
reponse對象設(shè)置響應(yīng)消息的方法:
-setStatus(int sc):設(shè)置狀態(tài)碼
-setHeader(String name, String value) :設(shè)置響應(yīng)頭
設(shè)置響應(yīng)體的方法:
-PrintWriter getWriter():獲取字符輸出流對象
-ServletOutputStream getOutputStream():獲取字節(jié)輸出流對象
重定向方法:
-sendRedirect(String Path): 資源跳轉(zhuǎn)
redirect(重定向)和forward(轉(zhuǎn)發(fā))同為資源跳轉(zhuǎn)方法,但有所區(qū)別:
* 重定向的特點(diǎn): redirect
1. 跳轉(zhuǎn)后地址欄發(fā)生變化:
2. 重定向可以訪問項(xiàng)目外其他站點(diǎn)(服務(wù)器)的資源
3. 重定向是兩次請求。前后Servlet下的request對象不能共享數(shù)據(jù)
* 轉(zhuǎn)發(fā)的特點(diǎn):forward
1. 轉(zhuǎn)發(fā)地址欄路徑不變 :
2. 轉(zhuǎn)發(fā)只能訪問當(dāng)前服務(wù)器下的資源
3. 轉(zhuǎn)發(fā)是一次請求,可以使用request對象來共享數(shù)據(jù)
跳轉(zhuǎn)資源需要確定跳轉(zhuǎn)后資源的路徑,分為相對路徑和絕對路徑。
通過相對路徑不可以確定唯一資源,使用相對路徑需要找到當(dāng)前資源和目標(biāo)資源之間的相對位置關(guān)系,比如:./ 表示當(dāng)前目錄,../ 表示后退一級目錄;
通過絕對路徑可以確定唯一資源,絕對路徑以/開頭的路徑,比如:/day15/responseDemo2即http://localhost/day15/responseDemo2。 如果資源跳轉(zhuǎn)的請求是由客戶端瀏覽器發(fā)出比如:***,則項(xiàng)目的絕對路徑需要加虛擬目錄;如果是由服務(wù)器端發(fā)出比如:sendRedirect(),則可加可不加。
/**運(yùn)行前提:
*1.備好responseTest2資源
*2.
*/
/**responseTest1:重定向資源跳轉(zhuǎn):使訪問responseTest1資源會自動跳轉(zhuǎn)到responseTest2資源
* 1.動態(tài)獲取虛擬目錄
* 2.通過request對象設(shè)置共享數(shù)據(jù)到request域中
* 3.調(diào)用以跳轉(zhuǎn)資源為參數(shù)的重定向方法
*/
@WebServlet("/responseTest1")
public class ResponseDemo1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.
String contextPath = request.getContextPath();
//2.
request.serAttribute("msg","hello Test2, I come from Test1.");
//3.
response.sendRedirect(contextPath+"/responseTest2");
//response.sendRedirect("http://www.baidu.com"); //sendRedirect方法可跳轉(zhuǎn)到項(xiàng)目外的資源
//response.sendRedirect("location","/responseTest2");//以靜態(tài)虛擬目錄作為參數(shù)
/* 初始重定向資源跳轉(zhuǎn)方式:設(shè)置狀態(tài)碼,響應(yīng)頭。又因?yàn)?302""location"取固定值,可直接調(diào)用方sendRedirect(響應(yīng)頭的值)。
response.setStatus(302);//設(shè)置狀態(tài)碼為302
response.setHeader("location","/MyTest/responseTest2");//設(shè)置響應(yīng)頭location = /MyTest/responseTest2
*/
/*響應(yīng)狀態(tài)碼:服務(wù)器告訴客戶端瀏覽器本次請求和響應(yīng)的一個狀態(tài)。
1. 狀態(tài)碼都是3位數(shù)字
2. 分類:
1. 1xx:服務(wù)器接受客戶端消息,但沒有接受完成,等待一段時間后,發(fā)送1xx多狀態(tài)碼
2. 2xx:成功。代表:200
3. 3xx:重定向。代表:302(重定向),304(訪問緩存)
4. 4xx:客戶端錯誤。
* 代表:
* 404(請求路徑?jīng)]有對應(yīng)的資源)
* 405:請求方式?jīng)]有對應(yīng)的doXxx方法
5. 5xx:服務(wù)器端錯誤。代表:500(服務(wù)器內(nèi)部出現(xiàn)異常)
*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
/**ResponseTest2:服務(wù)器輸出字符數(shù)據(jù)到瀏覽器
*1.設(shè)置編碼,并告訴瀏覽器編碼類型
*2.獲取字符輸出流
*3.輸出數(shù)據(jù)
*/
@WebServlet("/responsTset2")
public class ResponseTest1 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.再獲取輸出流之前設(shè)置編碼,并告訴瀏覽器使用的編碼,避免編碼解碼碼表不一致出現(xiàn)亂碼問題
response.setContentType("text/html;charset=utf-8");
//response.setHeader("content-type","text/html;charset=utf-8");通過消息頭"content-type"設(shè)置字符流編碼并告訴瀏覽器
// response.setCharacterEncoding("utf-8"); 僅僅設(shè)置字符流編碼格式,不能保證與瀏覽器默認(rèn)解碼格式統(tǒng)一
//2.獲取字符輸出流,默認(rèn)情況下獲取字符流的編碼是tomcat實(shí)現(xiàn)的ISO-8859-1,所以需要提前設(shè)置
PrintWriter pw = response.getWriter();
//3.輸出數(shù)據(jù)
String msg ="hello Test2, I don't come from Test1."
//String msg = (String)request.getAttribute("msg");錯于重定向是兩次請求,不能通過request共享數(shù)據(jù)
//
pw.write(msg);
//pw.write("<h1>msg</h1>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
/**ResponseTest3:
* 1.設(shè)置編碼,并告訴瀏覽器編碼類型
* 2.獲取字節(jié)輸出流
* 3.輸出數(shù)據(jù)
*/
@WebServlet("/responseTest3")
public class ResponseTest3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.
response.setContentType("text/html;charset=utf-8");
//2.
ServletOutputStream sos = response.getOutputStream();
//3.
sos.write("你好".getBytes("utf-8"));
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
基于response的驗(yàn)證碼的案例代碼:
/**CheckCodeServlet:驗(yàn)證碼
*1.在內(nèi)存中創(chuàng)建圖片(驗(yàn)證碼圖片對象)
*2.美化圖片
* 2.1 填充背景圖
* 2.2 畫指定顏色邊框
* 2.3 生成驗(yàn)證碼
* 2.4 畫干擾線
*3.將圖片輸出到頁面展示
*/
@WebServlet("/checkCodeServlet")
public class CheckCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int width = 100;
int height = 50;
//1.在內(nèi)存中創(chuàng)建一個指定寬高,背景色為黑色的圖片對象
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
//BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_ARGB);創(chuàng)建一個帶透明色的對象,最后一個參數(shù)調(diào)節(jié)灰度
//2.用代碼畫圖的步驟邏輯和“畫圖工具”鼠標(biāo)點(diǎn)擊畫圖一樣
//2.1 代碼“獲取畫筆--設(shè)置畫筆顏色--填充圖片背景色”相當(dāng)于鼠標(biāo)“點(diǎn)擊畫筆--選擇畫筆顏色--點(diǎn)擊圖片填充顏色”
Graphics g = image.getGraphics();//獲得畫筆
g.setColor(Color.PINK);//設(shè)置畫筆顏色
g.fillRect(0,0,width,height); //填充圖片顏色
//2.2代碼“設(shè)置邊框顏色--設(shè)置邊框起始位置和長寬幅度”相當(dāng)于鼠標(biāo)“點(diǎn)擊選擇邊框起始位置--拖動鼠標(biāo)確定最終邊框大小”
g.setColor(Color.BLUE); //設(shè)置邊框顏色
g.drawRect(0,0,width - 1,height - 1); //設(shè)置邊框起始坐標(biāo)和長寬像素,注意邊框占一個像素
//2.3
String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789";
Random ran = new Random();
for (int i = 1; i <= 4; i++) {
int index = ran.nextInt(str.length());//生成隨機(jī)角標(biāo)
char ch = str.charAt(index);//獲取隨機(jī)角標(biāo)對應(yīng)的字符
g.drawString(ch+"",width/5*i,height/2);//在圖片上寫驗(yàn)證碼
}
//2.4
g.setColor(Color.GREEN);
//
//隨機(jī)生成干擾線的始末點(diǎn)坐標(biāo),并連接成干擾線
for (int i = 0; i < 10; 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.將圖片響應(yīng)給客戶端
ImageIO.write(image,"jpg",response.getOutputStream());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
以上就是關(guān)于“Java web:Response對象概述”介紹,大家如果想了解更多相關(guān)知識,可以關(guān)注一下動力節(jié)點(diǎn)的JavaWeb視頻教程,里面的課程內(nèi)容由淺到深,通俗易懂,適合小白學(xué)習(xí),希望對大家能夠有所幫助。
初級 202925
初級 203221
初級 202629
初級 203743