JavaWeb学习总结(六)—HttpServletResponse
Response概述:
response是Servlet.service方法的一个参数,类型为javax.servlet.http.HttpServletResponse。在客户端发出每个请求时,服务器都会创建一个response对象,并传入给Servlet.service()方法。response对象是用来对客户端进行响应的,这说明在service()方法中使response对象可以完成对客户端的响应工作。
response对象的功能分为以下四种:
- 设置响应头信息;
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAycAAAC2CAIAAABMGWWFAAAcCElEQVR4nO2dzZnjqBaGics5dBqOZhZedAAdhRcTQ2fgBO6yMtBdVJVHBZzDB0IIy+/71FODEJwfhOCz1K4Jy2R8fHz8+fPn6CgAAAAAOhM+AAAAAGB/wp/J+Pj4ODoEAAAAgP6Eox+2xXzwhhEAAADOCKoLAAAAYASoLgAAAIARoLoAAAAARoDqAgAAABjBYNX1uF0ut8e65n4NP2pQXQAAAHBKjn/W9bhdUV0AAABwelBdb0EIx19ogCJMVAA4Nz3XuPs1hHC5PR63SwiX22N53C4hfNatz381/z67+Q1juP79/HHqn+Vsy+08Lf/z7/96OeoVbXYn23t7C9/s6iXy1ezuTJv9yJFfe+xorZcpAIDZ6LvAfT23etxu92V53C7X+7r6R2m5Xz/PdnnWZamTdX2kwLaYLTbupe32U11jGOB67cIqvyGD0y+60+N58wsHAOem9wJ3v17vX3pqWtXltBfNFhvPo7qO3cMGq67BrmfmdVVXbWMAgBei++q2/pZi7g1jCCF8y63L97uQ1TvGrOqy3tlZ7w2z9Y4qshorfqNK0f72vKLKX78feVPJBpZ9AxVVrg+dU0WK3iMv2Za1O3r4SVQf+RKb6SEp3f04s1HVMmzkrQHckuaWxAEAZma61S1VXYpy2qOcHip9dbXXPc780z7hUVC2rFc6pLts1mZqP+vRd+T4cppZcW4Jye+eVq5/Wy1rGTDyVTPEl1bKtQMAOAHTrW7WG8b91IxlP9ssaqn70h93Ndj5fNaVGU1NdT1rsttw2lh8EGJ5VxzVqq5izL41UaOIIelDZ6muNb4vJQYrpGFpZq05aaK6AOCsTLe6OW8Y14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bwf0pxypgfqqU1AAAAAElFTkSuQmCC" alt="" />
- 发送状态码;
> sendError(int sc) --> 发送错误状态码,例如404、500
> sendError(int sc, String msg) --> 也是发送错误状态码,还可以带一个错误信息!
> setStatus(int sc) --> 发送成功的状态码,可以用来发送302
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAb4AAAAxCAIAAAAqflsHAAAET0lEQVR4nO2c0XWrMAxAPVcGYh5GyBSZ4m3AEmxAP5qeR4wlS8aAaO89/UiMkIXBtzg0TUsY5nl+Pp9XVwEAUCfNAADgJD3DMM/z1SUAAJhIV9/2/mdmwQ4ANwF1AgC4QZ0AAG5QJwCAG9QJAODmNHVO4+MxTuuW15A+WlAnANyFK+86p3FAnQBwR1DnxaQU4jOTIGUA3IU+E+Y1pJQe4zSNj5Qe47RM4yOl77b19nf4z9YTF+xp+Lf+OagXL1thXagw7Algp9dsed9BTuP4WpZpfAyvdfPHq+U1fG894q6zaMZ1YxbgNWlf8zbb6gjNoU4AO/1my2sYXm8pRlanfVOXeC3VDlUdpDnsCWCk41RZP0MvLdhTSunHme8F+8eSvahOaaG9bcwiM8FJLXq8Jf/6rV5n3sWnp35GJGUt6xfrsCxYohgsZUCdAEYCTZWtOqWFdtsC3KjUhvwNdS6Cp4o+3W4yOq64i5IHdQIYCTRV9LvOYqN0l6f00qDaZnVK9b832dRZfa3jurVEnQBGAk0V5bNOr++UXoypsjW4Hq/fXYqNp6jTtS/qBDASaKoct2C/Kr6u1NofJ+1Up2vBjjcB7ASaLZI6q2v2LI/rWU0xfrtLdQ1ejNf7XTbmyp7eZE9ypIdICsrjIJ4RAewh0Gzh20QXEqQMgLsQaML8TXUCwB2Jq87tEvh3/Fw4wgDQi7jqBAAIC+oEAHCDOgEA3KBOAAA3qDMW3v/ucXQlFxZwL7oM15mnXu+Fs18l0Oigzmh/o355Afdi53Cd/80u1LmHQKODOtvU2esSr34r9CD2fB8/FB3VeQRXnd/fSqCxQ51tlzLqDALq/FMEGjvUuchXc/FTMOWDUddnplJwKn2VXqnH24WxfkuSLKZtHLJdjMdbLdXeoyXP+q2xCyV5MYmS1j6eep1Skm3+7Qspft0iHYUyFJaD+khljDsB1PlN8Xzb31oCpH71StZXpyt/3/rt8Q3j4D3eLuMjhen9St258kvH4q1H6XRbp/d8SQdrid/Trw7qDIr0K7Eq1mKLt8diS3Zp2n9R6wHS1ubj9TrFUoklf/P4SL0blW3Eq8Iu11VxF2l8Oo6/pb1ajw7qjItRAdWr09ud1NKsJL0eqf5qGZYA1zgcNHWNxFfn4r+uqifXkr+XOo35jaDOQLRNxeqJb77K+6qhmtYY4I2vtnt3/MvqdAVIMd4LsqM6LfFGUGcg2i6FZpXoezXXU818qDrXNxQN41DUk0VhO/sthkVT587xrOaxtB9xN6q0K8eIOgORPpE2STta8lgKyJLob43zx1X/dpfi1mL8emvbOHjHf9uvHq/0aOm37aCyUreHuR3AbReurvVgV35Xnobj0usRD1A//jNBnQCwparpSwhUE+oEgAzjTe75BCoIdQLAXUCdAABuUCcAgBvUCQDgBnUCALhJMwAAOEnPMMzzfHUJAAAmWLADALhBnQAAbr4AWdWpW2F4mXgAAAAASUVORK5CYII=" alt="" />
3. 设置响应正文;
4. 重定向;
具体应用:
1.重定向
package zy.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Aservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 设置location响应头 ,实现请求重定向
*/
//response.setHeader("Location","/JavaWeb/Bservlet");
//设置服务器的响应状态码
//response.setStatus(302); //快捷的重定向方式
//response.sendRedirect(""http://www.baidu.com"");
response.sendRedirect("/JavaWeb/Bservlet");
} }
抓包可以看见:
服务器返回一个302状态码告诉浏览器,你要的资源我没有,但是我通过Location响应头告诉你哪里有,而浏览器解析响应头Location后知道要跳转到/JavaWeb/Bservlet页面,所以就会自动跳转到Bservlet。
2.设置refresh响应头,让浏览器定时刷新
package zy.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 设置location响应头 ,实现请求重定向
*/
public class Cservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
response.getWriter().print("5s中后将跳转到百度主页");
/*
* 设置refresh响应头,让浏览器每隔3秒定时刷新
* response.setHeader("refresh", "3");
*/ //设置refresh响应头,让浏览器3秒后跳转到http://www.baidu.com
response.setHeader("Refresh","5;URL=http://www.baidu.com");
/*
* 5秒跳转到Csevlet
response.setHeader("Refresh","5;URL=/JavaWeb/Cservlet");
*/
}
}
访问Cservlet,5秒将会跳转
3.禁用浏览器缓存
开发项目的时候浏览器的缓存会很讨厌,这时我们可以禁用它,可以参考indexjsp.
package zy.servlet; import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 禁用浏览器缓存
*/
public class Dservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
/*
* 这三个响应头的设置可以禁用所有浏览器的缓存
*/
response.setHeader("cache-control","no-cache");
response.setHeader("pragma","no-cache");
response.setDateHeader("expires",-1); response.getWriter().print("hello world"); }
}
访问:
4.设置页面缓存
对于不经常变化的数据,在servlet中可以为其设置合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能。例如:
package zy.servlet; import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class Eservlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "helloworld";
/**
* 设置数据合理的缓存时间值,以避免浏览器频繁向服务器发送请求,提升服务器的性能
* 这里是将数据的缓存时间设置为1天
*/
response.setDateHeader("expires",System.currentTimeMillis() + 24 * 3600 * 1000);
response.getOutputStream().write(data.getBytes());
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { this.doGet(request, response);
} }
访问页面,并查看响应头:
有8个小时的时差。
另外也可以在“C:\Users\zydev\AppData\Local\Microsoft\Windows\Temporary Internet Files”找到该文件,注意要打开隐藏文件夹选项才看得见。
Response的响应正文
response是响应对象,向客户端输出响应正文(响应体)可以使用response的响应流,repsonse一共提供了两个响应流对象:
1. PrintWriter out = response.getWriter():获取字符流;
2. ServletOutputStream out = response.getOutputStream():获取字节流
OutputStream流是字节流,可以处理任意类型的数据,而PrintWriter流是字符流,只能处理字符数据,如果用字符流处理字节数据,会导致数据丢失。
A. 相应图片字节流
package zy.servlet; import java.io.FileInputStream;
import java.io.IOException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils;
/*
* 演示响应字节数据
*/
public class Fservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = "D:/demo.jpg";
FileInputStream in = new FileInputStream(path);
//读取输入流的字节到字节数组中,IOUtis是Apache提供的一个工具jar包
byte[] bytes = IOUtils.toByteArray(in);
response.getOutputStream().write(bytes);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
} }
访问结果:
B. 下载图片
package com.web.study; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 文件下载
*/
public class Bresponse extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
downloadFileByOutputStream(response);//下载文件,通过OutputStream流
} /**
* 下载文件,通过OutputStream流
* @param response
* @throws FileNotFoundException
* @throws IOException
*/
private void downloadFileByOutputStream(HttpServletResponse response)
throws FileNotFoundException, IOException {
//1.获取要下载的文件的绝对路径
String realPath = this.getServletContext().getRealPath("/download/demo.jpg");
//2.获取要下载的文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\")+1);
//3.设置content-disposition响应头控制浏览器以下载的形式打开文件
response.setHeader("content-disposition", "attachment;filename="+fileName);
//4.获取要下载的文件输入流
InputStream in = new FileInputStream(realPath);
int len = 0;
//5.创建数据缓冲区
byte[] buffer = new byte[1024];
//6.通过response对象获取OutputStream流
OutputStream out = response.getOutputStream();
//7.将FileInputStream流写入到buffer缓冲区
while ((len = in.read(buffer)) > 0) {
//8.使用OutputStream将缓冲区的数据输出到客户端浏览器
out.write(buffer,0,len);
}
in.close();
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
访问:
如果文件名是中文的:设置content-disposition响应头控制浏览器以下载的形式打开文件,中文文件名要使用URLEncoder.encode方法进行编码,否则会出现文件名乱码
response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
JavaWeb学习总结(六)—HttpServletResponse的更多相关文章
- javaweb学习总结(六)——Servlet开发(二)
一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文件web.xml中,可以使用一个或多个<init-param>标签为servlet配置一些 ...
- javaweb学习总结(六)——Servlet开发(二)(转)
转载自 http://www.cnblogs.com/xdp-gacl/p/3763559.html 一.ServletConfig讲解 1.1.配置Servlet初始化参数 在Servlet的配置文 ...
- javaweb学习总结(八)——HttpServletResponse对象(二)
一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedImage类,
- javaweb学习总结(八)——HttpServletResponse对象(二)(转)
(每天都会更新至少一篇以上,有兴趣的可以关注)转载自孤傲苍狼 一.HttpServletResponse常见应用——生成验证码 1.1.生成随机图片用作验证码 生成图片主要用到了一个BufferedI ...
- javaweb学习总结(七)——HttpServletResponse对象(一)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...
- javaweb学习总结(七)——HttpServletResponse对象(一)(转)
Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象. request和response对象即然代表请求和响应,那我们 ...
- javaweb学习总结(七)——HttpServletResponse对象(一)(转)
转载自 http://www.cnblogs.com/xdp-gacl/p/3789624.html Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对 ...
- JavaWeb学习笔记六 JSP
JSP技术 JSP全称Java Server Pages,是一种动态网页开发技术.它使用JSP标签在HTML网页中插入Java代码.标签通常以<%开头以%>结束. JSP是一种Java s ...
- JavaWeb学习 (十六)————JSP中的九个内置对象
一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...
- javaweb学习总结六(泛型)
一:泛型的概念 泛型用来现在集合中的存储类型,防止取出时强制转换发生错误. 1:没有使用泛型时,如下: @Test public void test1() { List list = new Arra ...
随机推荐
- js中RHS与LHS区别
为什么区分RHS与LHS是一件重要的事情? 因为在变量没有声明(在任何作用域都找不到该变量的情况下),这两种查询的行为是不一样的. function foo (a) { console.log(a + ...
- 【转】Eclipse插件大全介绍及下载地址
转载地址:http://developer.51cto.com/art/200906/127169.htm 尚未一一验证. eclipse插件大全介绍,以及下载地址 Eclipse及其插件下载网址大全 ...
- LightOJ1002 分类: 比赛 最短路 2015-08-08 15:57 15人阅读 评论(0) 收藏
I am going to my home. There are many cities and many bi-directional roads between them. The cities ...
- Sequence 分类: 栈和队列 2015-08-05 10:10 2人阅读 评论(0) 收藏
Sequence Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 8277 Accepted: 2708 Description ...
- cmake用法(转)
转自:http://blog.csdn.net/dbzhang800/article/details/6314073 新工作中使用到了cmake,所以找点资料学习一下,这篇讲的确实不错,转过来保存一下 ...
- Kernel启动时 驱动是如何加载的module_init,加载的次序如何;略见本文
Init.h中有相关initcall的启动次序,在system.map中可看出具体的__initcall指针的前后次序 #define pure_initcall(fn) __define_initc ...
- gulp 建立一个简单的自动化
前端项目需要的功能: 1.图片(压缩图片支持jpg.png.gif) 2.样式 (支持sass 同时支持合并.压缩.重命名) 3.javascript (检查.合并.压缩.重命名) 4.html (压 ...
- DataSet 关系
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- 3.linux man手册
(12) man作用:查询man手册,获得帮助信息man 1 ls 1表示查询的是linux命令man 2 xxx 2表示查询的是linux apiman 3 xxx 3表示查询的是C库函数注意:在m ...
- C#For循环
for循环基本格式 for(初始条件;循环条件;状态改变) { 循环体 } 1.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元).要把100元整好花 ...