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请求的同时,还会 ...
随机推荐
- VC++的debug与release版本
因为在Debug中有ASSERT断言保护,所以要崩溃,而在Release优化中就会删掉ASSERT,所以会出现正常运行. void func() { char b[2]={0}; strc ...
- python基础--文件操作实现全文或单行替换
python修改文件时,使用w模式会将原本的文件清空/覆盖.可以先用读(r)的方式打开,写到内存中,然后再用写(w)的方式打开. 替换文本中的taste 为 tasting Yesterday whe ...
- 3 ways to download files with PowerShell
Perhaps the greatest strength of PowerShell is it's foundation on the .NET framework. The .NET frame ...
- python---基础知识回顾(四)(模块sys,os,random,hashlib,re,序列化json和pickle,xml,shutil,configparser,logging,datetime和time,其他)
前提:dir,__all__,help,__doc__,__file__ dir:可以用来查看模块中的所有特性(函数,类,变量等) >>> import copy >>& ...
- 【机器学习】K-邻近算法的python 实现
#!/usr/bin/python # -*- coding: utf-8 -*- from numpy import * import operator def createDataSet(): ' ...
- php输出日志的实现
php输出日志的实现 思想:在想要输出log日志的地方,使用php的写入文件函数,把数据写入到事先定义好的文件中. php代码如下: //输出日志 public function outputLog( ...
- Nodejs文件监控chokidar
最近有个需求是扫描用例,用例是放在svn上,如果每次扫描都去遍历目录的话会有占用太多的io,所以想着用文件监控,有文件变化时只对该文件进行操作. Nodejs里的 chokidar 模块可以更好的对文 ...
- 常用Path路径
正三角形(左):<Path Data="M40,0 L0,30 40,60 z" Stretch="Uniform"/> 正三角形(上):<P ...
- 【LibreOJ】#6259. 「CodePlus 2017 12 月赛」白金元首与独舞
[题目]给定n行m列的矩阵,每个位置有一个指示方向(上下左右)或没有指示方向(任意选择),要求给未定格(没有指示方向的位置)确定方向,使得从任意一个开始走都可以都出矩阵,求方案数.n,m<=20 ...
- BestCoder Round #40 解题报告
这场是第一场没有米的BC... 大概也是想震一震那些一听说没米了就不打BC的人吧 这次的题目质量比以往高了许多 (然而我并没有打这一场BC 但是今天下午到现在做的过程中真的学到了不少知识呢 A题略水. ...