java EE : http 协议响应头部信息验证
一 location :***** 302 重定向
private void doWork(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应 code
resp.setStatus(302);
// 设置响应 header 参数 ,通知浏览器进行重定向
resp.setHeader("location", "index.jsp");
}
二 refresh :3;url=index.jsp 浏览器自动刷新
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置响应 header 参数,通知浏览器 3 秒后进行自动刷新,重定向至 url 指定地址
resp.setHeader("refresh","3;url=index.jsp");
}
三 content-type: image/png 告知浏览器调用哪个模块打开,比如:显示图片、显示 html 信息
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 设置 header 参数 ,告知浏览器调用何种模块打开
resp.setHeader("content-type", "image/png");
// 使用 IO 流返回图片字节信息
InputStream inputStream = new FileInputStream("E:\\trunck\\chapter02\\servletTest2\\web\\test.png");
int len = -1;
byte[] bytes = new byte[1024];
while ((len = inputStream.read(bytes)) != -1) {
resp.getOutputStream().write(bytes, 0, len);
}
inputStream.close(); }
四 Content-Disposition :attachment;filename=文件名 通知浏览器下载该文件
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
File file=new File("E:\\trunck\\chapter02\\servletTest2\\web\\test.png");
// 设置 header 参数,通知浏览器下载该文件
resp.setHeader("Content-Disposition","attachment;filename="+file.getName());
InputStream inputStream=new FileInputStream(file);
int len = -1 ;
byte[] bytes=new byte[1024];
while ((len=inputStream.read(bytes))!=-1){
resp.getOutputStream().write(bytes,0,len);
}
inputStream.close();
}
五 content-encoding : gzip(服务器端响应过来的压缩模式) content-length :字节长度
Accept-Encoding: gzip, deflate (客户端通知服务端,浏览器所支持的压缩模式)
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Headers/Content-Encoding
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println("字符串长度:" + content.length());
System.out.println("字符串转数组长度:" + content.getBytes().length);
// IO 流中 ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
gzipOutputStream.write(content.getBytes());
gzipOutputStream.close(); byte[] bytes = byteArrayOutputStream.toByteArray();
System.out.println("压缩后数组长度:" + bytes.length); resp.setHeader("content-encoding", "gzip");
resp.setHeader("content-length", bytes.length + "");
resp.getOutputStream().write(bytes); }
六 expires :-1(HTTP1.1的客户端和缓存必须将其他非法的日期格式(包括0)看作已经过期) cache-control:no-cache pragma:no-cache 告知浏览器不进行缓存
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setHeader("expires", "-1");
resp.setHeader("cache-control", "no-cache");
resp.setHeader("pragma", "no-cache"); resp.getWriter().write("abccc");
}
java EE : http 协议响应头部信息验证的更多相关文章
- Java Web ——http协议响应报文
HTTP 响应报文 HTTP 响应报文由状态行.响应头部.空行 和 响应包体 4 个部分组成,如下图所示: 下面对响应报文格式进行简单的分析: 状态行:状态行由 HTTP 协议版本字段.状态码和状态码 ...
- java EE : http 协议之请求报文、响应报文
1 HTTP协议特点 1)客户端->服务端(请求request)有三部份 a)请求行 b)请求头 c)请求的内容,如果没有,就是空白字符 2)服务端->客户端(响应response)有三部 ...
- Ajax获取服务器响应头部信息
$.ajax({ type: 'HEAD', // 获取头信息,type=HEAD即可 url : window.location.href, complete: function( xhr,data ...
- Java EE.Servlet.生成响应
Servlet的核心职责就是根据客户端的请求生成动态响应. 1.编码类型 2.流操作(下载文件) servlet支持两种格式的输入/输出流.一种是字符输入输出流.另一种是字节输入输出流. 3.重定向
- HTTP请求头和响应头部包括的信息有哪些?(转)
转载自:https://www.cnblogs.com/hxc555/p/6506154.html 每个HTTP请求和响应都会带有相应的头部信息.默认情况下,在发送XHR请求的同时,还会发送下列头部信 ...
- HTTP请求头和响应头部包括的信息有哪些?
每个HTTP请求和响应都会带有相应的头部信息.默认情况下,在发送XHR请求的同时,还会发送下列头部信息: Accept:浏览器能够处理的内容类型 Accept-Charset:浏览器能够显示的字符集 ...
- wget/curl查看请求响应头信息
wget / curl 是两个比较方便的测试http功能的命令行工具,大多数情况下,测试http功能主要是查看请求响应 头信息 ,而给这两个工具加上适当的命令行参数即可轻易做到,其实查man手册就能找 ...
- curl/wget 测试http请求的响应头信息
1. wget –debug wget可以使用debug信息来查看信息头,如下: [root@localhost ~]# wget --debug http://192.168.1.101:8080/ ...
- 深入理解ajax系列第三篇——头部信息
前面的话 每个HTTP请求和响应都会带有相应的头部信息,其中有的对开发人员有用.XHR对象提供了操作头部信息的方法.本文将详细介绍HTTP的头部信息 默认信息 默认情况下,在发送XHR请求的同时,还会 ...
随机推荐
- Qt ------ CSS 长度单位
1. css相对长度单位 Ø em 元素的字体高度 Ø ex 字体x的高度 Ø px ...
- Android开发大纲
知识模块 Java Android 计算机网络 算法 书本知识 深入理解Java虚拟机 Java并发编程实战 疯狂Android讲义 Android开发艺术探索 网络知识 面试经验 面试心得与总结-- ...
- 3 ways to download files with PowerShell
Perhaps the greatest strength of PowerShell is it's foundation on the .NET framework. The .NET frame ...
- jni里找不到刚添加的C++函数
使用NDK开发,用到了JNI来连接C++和JAVA. 当C++方增加了一个新函数,jni访问此函数,eclipse会提示找不到改函数,然后前面打个红叉叉表示语法错误,从而阻碍了编译和运行. 当我选择清 ...
- docker操作mysql
Docker操作mysql 查找docker hub上的mysql镜像 Docker search.mysql 拉取官方的镜像标签为5.6 Docker pull mysql:5.6 在本地镜像列表里 ...
- 启动EMQ(emqtt)时报错找不到libsctp.so.1
libsctp.so.1: cannot open shared object file: No such file or directory 发现没有安装sctp [root@localho ...
- 2017北京国庆刷题Day3 morning
期望得分:100+60+0=160 实际得分:100+30+0=130 考场上用的哈希 #include<cstdio> #include<cstring> #include& ...
- 分块+二分 BZOJ 3343
3343: 教主的魔法 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 1312 Solved: 585[Submit][Status][Discus ...
- How to ignore SSL certificate errors in Apache HttpClient 4.4
public static CloseableHttpClient acceptsUntrustedCertsHttpClient() throws KeyStoreException, NoSuch ...
- (一)Hadoop1.2.1安装——单节点方式和单机伪分布方式
Hadoop1.2.1安装——单节点方式和单机伪分布方式 一. 需求部分 在Linux上安装Hadoop之前,需要先安装两个程序: 1)JDK 1.6(或更高版本).Hadoop是用Java编写的 ...