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);
	}
}

Response的用法_第1张图片

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);
	}
}

Response的用法_第2张图片

注意了 要注意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来完成自动跳转





Insert title here



	恭喜你,登录成功,3秒后将跳转,若不跳转 请点击这里


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不能同时调用

你可能感兴趣的:(JavaWeb,Servlet,Response)