概述
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不能同时调用
最后
以上就是土豪曲奇为你收集整理的Response的用法的全部内容,希望文章能够帮你解决Response的用法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复