1. 程式人生 > >Response的用法

Response的用法

1. 設定響應行的狀態碼

setStatus(int value)

response.setStatus(302);

2.設定響應頭

addHeader(String name,String value)
addIntHeader(String name,int value)
addDateHeader(String name,long value)

public class HeaderServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 Date date = new Date();
		 response.addHeader("name", "xiaodingdang");
		 response.addHeader("name", "daxiong");
		 response.addIntHeader("age", 15);
		 response.addDateHeader("birthday", date.getTime());
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

這裡寫圖片描述

setHeader(String name,String value)
setIntHeader(String name ,int value)
setDateHeader(String name, long value)

public class HeaderServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 Date date = new Date();
		 response.setHeader("name", "xiaodingdang");
		 response.setHeader("name", "daxiong");
		 response.setIntHeader("age", 15);
		 response.setDateHeader("birthday", date.getTime());
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

這裡寫圖片描述

注意了 要注意setHeader和addHeader的不同 一個是設定 一個是新增 設定只能是設定最後一個 但是新增可以新增很多個

3.response實現重定向

方法一:

response.setStatus(302); //設定響應行的狀態碼為302 重定向
response.setHeader("Location", "/Servlet/servlet2"); //設定響應頭的屬性 跳轉到Servlet2

方法二:

response.sendRedirect("/Servlet/servlet2"); //利用response中的sendRedirect屬性完成重定向

4.response實現自動重新整理跳轉

public class RefreshServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("refresh", "5;url=http://www.baidu.com"); //設定頭 引數 數值5為
		//秒 中間用分號間隔 url為要跳轉的網址
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

使用JS來完成自動跳轉

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	window.onload= function(){
		var time = 3;
		var second = document.getElementById("second");
		timer = setInterval(function(){
			second.innerHTML = time;
			time--;
			if(time==0){
				location.href = "http://www.baidu.com";
				clearInterval(timer);
			}
		},1000);
	}
</script>
</head>
<body>
	恭喜你,登入成功,<span id="second" style="color: red">3</span>秒後將跳轉,若不跳轉 請點選<a href="http://www.baidu.com">這裡</a>
</body>
</html>

5.解決response中文亂碼問題

//方法一:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setHeader("content-Type", "text/html;charset=utf-8");
		response.getWriter().write("小叮噹");
		
	}
//方法二:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.setCharacterEncoding("UTF-8");
//		response.setHeader("content-Type", "text/html;charset=utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.getWriter().write("中國");
		
	}
//通常使用第二種方法解決亂碼問題

6.上傳圖片資源

public class TestServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletOutputStream outputStream = response.getOutputStream(); //獲得位元組輸出流
		String realPath = this.getServletContext().getRealPath("1.jpg"); //獲得圖片的真實路徑
		InputStream in = new FileInputStream(realPath); //獲得伺服器上的圖片
		
		//使用高效的方法寫入客戶端圖片
		int len = 0;
		byte[] buffer = new byte[1024];
		while((len=in.read(buffer))>0) {
			outputStream.write(buffer, 0, len);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}

Response中應注意的細節

1.response獲得的流OutputStream不需要手動關閉,Tomcat容器會幫助我們關閉
但是InputStream需要手動關閉
2.getWriter和getOutputStream不能同時呼叫