大綱:
一、response重定向
二、response相對路徑和絕對路徑
三、response輸出數據
四、response驗證碼
五、ServletContext對象
六、文件下載
response對象:
**功能:**設置響應消息
1.設置響應行
(1)格式:HTTP/1.1 200 OK
(2)設置狀態碼:setStatus(int sc)
2.設置響應頭
setHeader(String name,String value)
3.設置響應體
使用步驟:
(1)獲取輸出流
①字符輸出流:PrintWriter getWriter()
②字節輸出流:ServletOutputStream getOutputStream()
(2)使用輸出流,將數據輸出到客戶端瀏覽器
一、response重定向
1.完成重定向
(1)告訴瀏覽器重定向,狀態碼302
(2)告訴瀏覽器B資源的路徑,響應頭lcation:B資源的路徑
第一種
//1設置狀態嗎爲302
resp.setStatus(302);
//2設置響應頭location
resp.setHeader("location","pro2/responsedemo2");
第二種
resp.sendRedirect("/pro2/responsedemo2");
重定向特點:(redirect)
1.地址欄發生變化
2.重定向可以訪問其他站點的資源
3.重定向是兩次請求。不能使用requst對象共享數據
轉發的特點:(forward)
1.地址欄路徑不變
2.轉發只能訪問當前服務器下的資源
3.轉發是一次請求,可以使用requst對象共享數據
二、response相對路徑和絕對路徑
1.路徑分類:
(1)相對路徑:通過相對路徑不能確定唯一資源(以.開頭的路徑)
規則:找到當前資源和目標資源之間的相對關係
./ 表示當前目錄
../表示後退一級目錄
(2)絕對路徑:通過絕對路徑可以確定唯一資源(以/開頭的路徑)
規則:判斷定義的路徑是給誰用的,
①給客戶端瀏覽器使用,需要加虛擬目錄(項目的訪問路徑)
a.建議虛擬目錄動態獲取:request.getContextPath()
b.<a>,<form>重定向
②給服務器使用:不需要加虛擬目錄
轉發路徑
三、response輸出數據
1.服務器輸出字符數據到瀏覽器
(1)獲取字符輸出流
(2)輸出數據
//獲取字符輸出流
PrintWriter pw = resp.getWriter();//獲取的流默認編碼是ISO-8859-1
//輸出數據
pw.write("hello...");
注意
中文出現亂碼:
原因:編解碼使用的字符集不一致
方法1:
//告訴瀏覽器,服務器發送的消息體數據的編碼,建議瀏覽器使用該編碼解碼
response.setHeader("content-type","text/html;charset=utf-8");
方法2:簡化
//告訴瀏覽器,服務器發送的消息體數據的編碼,建議瀏覽器使用該編碼解碼
response.setHeader("content-type","text/html;charset=utf-8");
2.服務器輸出字節數據到瀏覽器
//獲取字節輸出流
ServletOutputStream sos = resp.getOutputStream();
//輸出數據
sos.write("hello".getBytes());
方法
resp.setContentType("text/html;charset=utf-8");
//獲取字節輸出流
ServletOutputStream sos = resp.getOutputStream();
//輸出數據
sos.write("hello".getBytes("utf-8"));
四、response驗證碼
分析
驗證碼案例分析:
本質:圖片
目的:防止惡意表單註冊
步驟:
1創建對象,在內存圖片(驗證碼圖片對象)
2.美化圖片
3.將圖片輸出到頁面展示
代碼:
@Override
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 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);
String str= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
//生成隨機角標
Random ran = new Random();
for (int i = 1; i <= 4; i++) {
int index = ran.nextInt(str.length());
//獲取字符
char ch = str.charAt(index);
//2.3寫驗證碼
g.drawString(ch+"",width/5*i,height/2);
}
//畫干擾線
g.setColor(Color.GREEN);
//隨機生成座標點
for (int i = 0; i < 8; 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);
}
/*
這是固定的要隨機的
//2.3寫驗證碼
g.drawString("A",20,25);
g.drawString("B",40,25);
g.drawString("C",60,25);
g.drawString("D",80,25);
*/
//3.將圖片輸出到頁面展示
ImageIO.write(image,"jpg",resp.getOutputStream());
}
在使用時需要看不清的時候需要改變它,看頁面 和js交互
<head>
<meta charset="UTF-8" >
<title>regist</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 = "/pro3/checkcode?"+date
}
}
</script>
</head>
<body>
<img id = "checkCode" src = "/pro3/checkcode"/>
<a id = "change" href = "">看不清 換一張</a>
</body>
效果
五、ServletContext對象
1.概念
代表整個web應用,可以和程序的容器(服務器)來通信
2.獲取
(1)通過request對象獲取request.getServletContext();
(2)通過HttpServlet獲取this.getServletContext();
代碼示例
//servletcontext對象的獲取
//(1)通過request對象獲取`request.getServletContext();`
ServletContext servletContext1 = req.getServletContext();
//(2)通過HttpServlet獲取`this.getServletContext();`
ServletContext servletContext2 = this.getServletContext();
System.out.println(servletContext1);//org.apache.catalina.core.ApplicationContextFacade@de3306e
System.out.println(servletContext2);//org.apache.catalina.core.ApplicationContextFacade@de3306e
System.out.println(servletContext1 == servletContext2);//true
3.功能:
(1)獲取MIME類型:
①MIME類型: 在互聯網通信過程中定義的一種文件數據類型
格式:大類型/小類型 text/html image/jpeg
②獲取:String getMimeType(String file);
代碼示例
//servletcontext功能
//獲取:String getMimeType(String file);
//1.通過HttpServlet獲取`this.getServletContext();`
ServletContext servletContext2 = this.getServletContext();
//2.定義文件名稱
String fileName = "a.jpg";
//3.獲取MIME類型
String mimeType = servletContext2.getMimeType(fileName);
System.out.println(mimeType);
(2)域對象:共享數據
1.setAttribute(String name,Object value)
2.getAttribute(String name);
3.removeAttribute(String name)
servletContext對象範圍:所有用戶請求的數據
servletcontextdemo3
//1.通過HttpServlet獲取`this.getServletContext();`
ServletContext servletContext2 = this.getServletContext();
//3.獲取MIME類型
servletContext2.setAttribute("msg","haha");
servletcontextdemo4
//1.通過HttpServlet獲取`this.getServletContext();`
ServletContext servletContext2 = this.getServletContext();
Object msg = servletContext2.getAttribute("msg");
System.out.println(msg);
效果:
(3)獲取文件的真實(服務器)路徑
方法:String getRealPath(String path)
代碼示例
public class ServletContextDemo5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.通過HttpServlet獲取`this.getServletContext();`
ServletContext servletContext2 = this.getServletContext();
//獲取文件的真實路徑
String realPath = servletContext2.getRealPath("/b.txt");//web目錄下資源訪問
System.out.println(realPath);
//File file = new File(realPath);
String realPath1 = servletContext2.getRealPath("/WEB-INF/c.txt");//WEB-INF目錄下資源訪問
System.out.println(realPath1);
String realPath2 = servletContext2.getRealPath("/WEB-INF/classes/a.txt");//src目錄下資源訪問
System.out.println(realPath2);
}
文件存放位置
打印結果
六、文件下載
文件下載需求:
1.頁面顯示超鏈接
2.點擊超鏈接後彈出下載提示框
3.完成圖片下載
分析:
1.超鏈接指向的資源如果能夠被瀏覽器解析,則在瀏覽器中展示,如果不能解析,則彈出下載提示框,不滿足需求
2.任何資源都必須彈出下載提示框
3.使用響應設置資源的打開方式
content-disposition:attachment,filename = xxx
步驟
1.定義頁面,編輯超鏈接href屬性,指向servlet,傳遞資源名稱filename
2.定義servlet
(1)獲取文件名稱
(2)使用字節輸入流加載文件進內存
(3)指定response的響應頭,content-disposition:attachment;filename=xxx
(4)將數據寫出到response輸出流
問題:中文文件問題
解決思路:
1.獲取客戶端使用的瀏覽器版本信息
2.根據不同的版本信息,設置filename的編碼方式不同