由於response表明響應,因此咱們能夠經過該對象分別設置Http響應的響應行,響應頭和響應體。
//手動設置http響應行的狀態碼 response.setStatus(404);
addHeader(String name, String value) addIntHeader(String name, int value) addDateHeader(String name, long date)//重點 setHeader(String name, String value) setDateHeader(String name, long date) setIntHeader(String name, int value)
Date date = new Date(); //設置響應頭 response.addHeader("name", "zhangsan"); response.addIntHeader("age", 28); response.addDateHeader("hirthday", date.getTime()); //add表明添加,set表明重置 response.addHeader("name", "lisi"); response.setHeader("name", "wangwu");
/*//沒有響應的資源,告知客戶端去重定向到2 //1.設置狀態碼 302 response.setStatus(302); //2.設置響應頭Location response.setHeader("Location","/javaweb/servlet2");*/ //封裝成一個重定向的方法sendRedirect(url) response.sendRedirect("/javaweb/servlet2");
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>註冊頁面</title> <script type="text/javascript"> window.onload = function (ev) { var time = 5; var secondEle = document.getElementById("second"); setInterval(function () { secondEle.innerHTML = time; time--; if(time==0){ clearInterval(timer); location.href="http://www.baidu.com"; } },1000); } </script> </head> <body> 恭喜你註冊成功,<span style="color: red" id="second">5</span>秒後跳轉,如不跳轉點擊<a href="http://www.baidu.com">這裏</a>! </body> </html>
響應體設置文本javascript
設置默認編碼html
//設置response查詢的碼錶 //response.setCharacterEncoding("UTF-8"); //經過一個頭Content-Type告知客戶端使用何種碼錶解碼,寫一種就行 //response.setHeader("Content-Type", "text/html;charset=UTF-8"); response.setContentType("text/html,charset=UTF-8"); //響應體設置文本 PrintWriter writer = response.getWriter(); writer.write("hello response!"); writer.write("中國");
響應頭設置字節java
//使用response得到字節輸出流 ServletOutputStream out = response.getOutputStream(); //得到服務器上的圖片 String realPath = this.getServletContext().getRealPath("img.jpg"); InputStream in = new FileInputStream(realPath); int len = 0; byte[] buffer = new byte[1024]; while ((len=in.read(buffer))>0){ out.write(buffer, 0, len); } in.close(); out.close();
文件下載的實質就是文件拷貝,將文件從服務器端拷貝到瀏覽器端。因此文件下載須要IO技術將服務器端的文件使用InputStream讀取到,在使用ServletOutputStream寫到response緩衝區中。
//得到要下載的文件的名稱 String filename = request.getParameter("filename"); //要下載的這個文件的類型-客戶端經過文件的mime類型去區分類型 response.setContentType(this.getServletContext().getMimeType(filename)); //告訴客戶端文件不是解析,而是已附件形式打開(下載) response.setHeader("Content-Disposition", "attachment;filename="+filename); //獲取文件的絕對路徑 String path = this.getServletContext().getRealPath("/download/" + filename); //得到文件的輸入流 InputStream in = new FileInputStream(path); //得到輸出流-經過response得到的輸出流,用於向客戶端寫內容。 ServletOutputStream out = response.getOutputStream(); //文件拷貝的模板代碼 int len = 0; byte[] buffer = new byte[1024]; while ((len=in.read(buffer))>0){ out.write(buffer,0, len); } in.close(); //out.close();//會自動關閉
可是,若是下載中文文件,頁面在下載時會出現中文亂碼或不能顯示文件名的狀況, 緣由是不一樣的瀏覽器默認對下載文件的編碼方式不一樣,ie是UTF-8編碼方式,而火狐瀏覽器是Base64編碼方式。所裏這裏須要解決瀏覽器兼容性問題,首先要辨別訪問者是ie仍是火狐(其餘),經過Http請求體中的一個屬性能夠辨別。
package net.allidea.content; import sun.misc.BASE64Encoder; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder; @WebServlet(name = "DownloadServlet2") public class DownloadServlet2 extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //******************文件名是中文的******************** //得到要下載的文件的名稱//???.jpg String filename = request.getParameter("filename"); //解決得到中文參數亂碼問題 //美女.jpg //filename = new String(filename.getBytes("ISO8859-1"),"UTF-8"); //得到請求頭中的User-Agent String agent = request.getHeader("User-Agent"); //根據不一樣瀏覽器進行不一樣的編碼 String filenameEncoder = ""; if (agent.contains("MSIE")) { // IE瀏覽器 filenameEncoder = URLEncoder.encode(filename, "utf-8"); filenameEncoder = filenameEncoder.replace("+", " "); } else if (agent.contains("Firefox")) { // 火狐瀏覽器 BASE64Encoder base64Encoder = new BASE64Encoder(); filenameEncoder = "=?utf-8?B?" + base64Encoder.encode(filename.getBytes("utf-8")) + "?="; } else { // 其它瀏覽器 filenameEncoder = URLEncoder.encode(filename, "utf-8"); } //要下載的這個文件的類型-客戶端經過文件的mime類型去區分類型 response.setContentType(this.getServletContext().getMimeType(filename)); //告訴客戶端文件不是解析,而是已附件形式打開(下載)-filename="+filename客戶端解碼 response.setHeader("Content-Disposition", "attachment;filename="+filenameEncoder); //獲取文件的絕對路徑 String path = this.getServletContext().getRealPath("download/" + filename); //得到文件的輸入流 InputStream in = new FileInputStream(path); //得到輸出流-經過response得到的輸出流,用於向客戶端寫內容。 ServletOutputStream out = response.getOutputStream(); //文件拷貝的模板代碼 int len = 0; byte[] buffer = new byte[1024]; while ((len=in.read(buffer))>0){ out.write(buffer,0, len); } in.close(); //out.close(); } }
response細節點:web
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>用戶登陸</title> <script type="text/javascript"> window.onload = function () { } function changeImg(obj) { obj.src="/javaweb/checkImg?time="+new Date().getTime(); } </script> </head> <body> <form action="/login" method="post"> 用戶名:<input type="text" name="username"><br> 密碼:<input type="password" name="password"><br> 驗證碼:<input type="text" name="change"><img onclick="changeImg(this)" src="/javaweb/checkImg"><br> <input type="submit" value="登陸"><br> </form> </body> </html>