一 location :***** 302 重定向 html
private void doWork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 設置響應 code resp.setStatus(302);
// 設置響應 header 參數 ,通知瀏覽器進行重定向 resp.setHeader("location", "index.jsp"); }
二 refresh :3;url=index.jsp 瀏覽器自動刷新web
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 設置響應 header 參數,通知瀏覽器 3 秒後進行自動刷新,重定向至 url 指定地址 resp.setHeader("refresh","3;url=index.jsp"); }
三 content-type: image/png 告知瀏覽器調用哪一個模塊打開,好比:顯示圖片、顯示 html 信息數組
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 設置 header 參數 ,告知瀏覽器調用何種模塊打開 resp.setHeader("content-type", "image/png");
// 使用 IO 流返回圖片字節信息 InputStream inputStream = new FileInputStream("E:\\trunck\\chapter02\\servletTest2\\web\\test.png"); int len = -1; byte[] bytes = new byte[1024]; while ((len = inputStream.read(bytes)) != -1) { resp.getOutputStream().write(bytes, 0, len); } inputStream.close(); }
四 Content-Disposition :attachment;filename=文件名 通知瀏覽器下載該文件瀏覽器
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { File file=new File("E:\\trunck\\chapter02\\servletTest2\\web\\test.png");
// 設置 header 參數,通知瀏覽器下載該文件 resp.setHeader("Content-Disposition","attachment;filename="+file.getName()); InputStream inputStream=new FileInputStream(file); int len = -1 ; byte[] bytes=new byte[1024]; while ((len=inputStream.read(bytes))!=-1){ resp.getOutputStream().write(bytes,0,len); } inputStream.close(); }
五 content-encoding : gzip(服務器端響應過來的壓縮模式) content-length :字節長度緩存
Accept-Encoding: gzip, deflate (客戶端通知服務端,瀏覽器所支持的壓縮模式)
服務器
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Encodingjsp
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; System.out.println("字符串長度:" + content.length()); System.out.println("字符串轉數組長度:" + content.getBytes().length);
// IO 流中 ByteArrayOutputStream ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(content.getBytes()); gzipOutputStream.close(); byte[] bytes = byteArrayOutputStream.toByteArray(); System.out.println("壓縮後數組長度:" + bytes.length); resp.setHeader("content-encoding", "gzip"); resp.setHeader("content-length", bytes.length + ""); resp.getOutputStream().write(bytes); }
六 expires :-1(HTTP1.1的客戶端和緩存必須將其餘非法的日期格式(包括0)看做已通過期) cache-control:no-cache pragma:no-cache 告知瀏覽器不進行緩存ide
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setHeader("expires", "-1"); resp.setHeader("cache-control", "no-cache"); resp.setHeader("pragma", "no-cache"); resp.getWriter().write("abccc"); }