- 什么是Responese對象?
- 怎么使用Response對象?
- 重定向
- ServletContext對象
- 文件下載
1. 什么是Responese對象?
Web服務(wù)器收到客戶端的http請求,會針對每一次請求,分別創(chuàng)建一個用于代表請求的request對象、和代表響應(yīng)的response對象。
服務(wù)器-->客戶端 的返回的響應(yīng)就是response對象。
2. 怎么使用Response對象?
2.1 設(shè)置響應(yīng)內(nèi)容
@WebServlet("/failServlet")
public class FailServlet 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 {
//設(shè)置編碼
resp.setContentType("text/html;charset=utf-8");
//獲取存放數(shù)據(jù)
//String data =(String) req.getAttribute("shareData");
//設(shè)置響應(yīng)內(nèi)容
resp.getWriter().write("登錄失敗,用戶名或密碼錯誤");
}
}
3. 重定向
重定向之前頁面設(shè)置
@WebServlet("/demo")
public class ServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendRedirect("/JavaWEB/servletDemo2");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
重定向完成后頁面設(shè)置
@WebServlet("/servletDemo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//設(shè)置響應(yīng)內(nèi)容
response.getWriter().write("歡迎,demo2");
}
}
效果展示

image.png
處理中文亂碼設(shè)置
//設(shè)置響應(yīng)編碼格式
response.setContentType("text/html;charset=utf-8");

image.png
4.ServletConetext對象
4.1 什么是ServletConetext對象?
ServletContext用來存放全局變量,每個Java虛擬機每個Web項目只有一個ServletContext,這個ServletContext是由Web服務(wù)器創(chuàng)建的。所以ServletContext的作用范圍是整個應(yīng)用。
4.2 怎么使用ServletConetext對象?
示例代碼
ServletDemo1
@WebServlet("/demo1")
public class ServletDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設(shè)置響應(yīng)編碼
resp.setContentType("text/html;charset=utf-8");
//設(shè)置servletContext對象
ServletContext servletContext = this.getServletContext();
//共享數(shù)據(jù)
servletContext.setAttribute("code","999");
//設(shè)置提示信息
resp.getWriter().write("demo1的code信息已經(jīng)寫入");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
ServletDemo2
@WebServlet("/demo2")
public class ServletDemo2 extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//設(shè)置編碼格式
resp.setContentType("text/html;charset=utf-8");
ServletContext servletContext = this.getServletContext();
String code = (String)servletContext.getAttribute("code");
//設(shè)置響應(yīng)內(nèi)容
resp.getWriter().write("demo中的code信息為:"+code);
}
}
運行結(jié)果

image.png

image.png
4.3 ServletConetext有什么功能?
- 多個Servlet通過ServletContext對象實現(xiàn)數(shù)據(jù)共享
- 實現(xiàn)Servlet的請求轉(zhuǎn)發(fā)
- 利用ServletContext對象讀取資源文件
String getRealPath(String path)
String b = context.getRealPath("/b.txt");//web目錄下資源訪問
System.out.println(b);
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);
5.文件下載
文件下載需求:
- 頁面顯示超鏈接
- 點擊超鏈接后彈出下載提示框
- 完成圖片文件下載
分析:
- 超鏈接指向的資源如果能夠被瀏覽器解析,則在瀏覽器中展示,如果不能解析,則彈出下載提示框。不滿足需求
- 任何資源都必須彈出下載提示框
- 使用響應(yīng)頭設(shè)置資源的打開方式:
content-disposition:attachment;filename=xxx
步驟:
- 定義頁面,編輯超鏈接href屬性,指向Servlet,傳遞資源名稱filename
- 定義Servlet:
- 獲取文件名稱
- 使用字節(jié)輸入流加載文件進內(nèi)存
- 指定response的響應(yīng)頭: content-disposition:attachment;filename=xxx
- 將數(shù)據(jù)寫出到response輸出流
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>youbao三歲</title>
</head>
<body>
<a href="/Web/download?filename=2019.jpg">圖片</a>
<a href="/Web/download?filename=2018.mp4">視頻</a>
</body>
</html>
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//1.獲取請求參數(shù),文件名稱
String filename = req.getParameter("filename");
//2.使用字節(jié)輸入流加載文件進內(nèi)存
//2.1找到文件服務(wù)器路徑
ServletContext servletContext = this.getServletContext();
String realPath = servletContext.getRealPath("/WEB-INF/classes/" + filename);
//2.2用字節(jié)流關(guān)聯(lián)
FileInputStream fis = new FileInputStream(realPath);
//3.設(shè)置response的響應(yīng)頭
//3.1設(shè)置響應(yīng)頭類型:content-type
String mimeType = servletContext.getMimeType(filename);//獲取文件的mime類型
resp.setHeader("content-type",mimeType);
//3.2設(shè)置響應(yīng)頭打開方式:content-disposition
resp.setHeader("content-disposition","attachment;filename="+filename);
//4.將輸入流的數(shù)據(jù)寫出到輸出流中
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();
}
}