http请求响应头

1 篇文章 0 订阅
用于HTTP请求中的常用头
Accept: text/html,image/*    			客户机支持的数据类型
Accept-Charset: ISO-8859-1			客户机采用编码
Accept-Encoding: gzip,compress		客户机支持的数据压缩格式
Accept-Language: en-us,zh-cn 		客户机的语言环境
Host: www.it315.org:80				客户机访问服务器主机名
If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT		缓存相关。资源缓存时间
Referer: http://www.it315.org/index.jsp				从哪个资源访问服务器的,用在防盗链上
User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)		客户机的软件环境
Cookie		客户机可以通过该头,向服务器提交数据
Connection: close/Keep-Alive   
Date: Tue, 11 Jul 2000 18:23:51 GMT
HTTP请求中的常用响应头
Location: http://www.it315.org/index.jsp 			这个头配合302状态码使用,用于通知客户机重新请求的路径
Server:apache tomcat						服务器通过这个头告诉浏览器服务器类型
Content-Encoding: gzip 						服务器数据压缩格式
Content-Length: 80 						告诉浏览器回送数据长度
Content-Language: zh-cn 					语言环境
Content-Type: text/html; charset=GB2312 		回送数据的类型
Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT	告诉浏览器当前资源缓存时间
Refresh: 1;url=http://www.it315.org				通知浏览器多久刷新一次,单位秒
Content-Disposition: attachment; filename=aaa.zip		告诉浏览器以下载方式打开文件
Transfer-Encoding: chunked  					告诉浏览器数据的传送格式(chunked,以块发送)
Set-Cookie:SS=Q0=5Lb_nQ; path=/search
ETag									缓存相关,这个可以做到实时更新
Expires: -1								缓存相关,资源缓存多长时间,-1或0不缓存
Cache-Control: no-cache  					通知浏览器不要缓存	
Pragma: no-cache   						通知浏览器不要缓存	
Connection: close/Keep-Alive   
Date: Tue, 11 Jul 2000 18:23:51 GMT
响应头测试代码
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPOutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public HelloServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		test5(response);
	}
	//下载文件
	private void test5(HttpServletResponse response) throws IOException {
		InputStream inputStream = this.getServletContext().getResourceAsStream("/abc.jpg");
		response.setHeader("Content-Disposition", "attachment; filename=abc.jpg");
		int len=0;
		byte[] b=new byte[1024];
		while((len=inputStream.read(b))!=-1){
			response.getOutputStream().write(b,0,len);
		}
	}
	//定时刷新
	private void test4(HttpServletResponse response) throws IOException {
		//response.setHeader("Refresh", "5");//不写url则刷新本页面
		response.setHeader("Refresh", "5;url='http://www.baidu.com'");//写url则过时间后跳转到别的页面
		response.getOutputStream().write("aaaaa".getBytes());
	}

	//通过Content-Type通知浏览器以哪种方式处理资源
	private void test3(HttpServletResponse response) throws IOException {
		InputStream inputStream = this.getServletContext().getResourceAsStream("/abc.jpg");
		response.setHeader("Content-Type", "image/jpeg");
		int len=0;
		byte[] b=new byte[1024];
		while((len=inputStream.read(b))!=-1){
			response.getOutputStream().write(b,0,len);
		}
	}

	//实现数据压缩后输出
	private void test2(HttpServletResponse response) throws IOException {
		String data="这里是文章内容!";
		System.out.println("压缩前数据大小:"+data.getBytes().length);
		ByteArrayOutputStream bout=new ByteArrayOutputStream();
		GZIPOutputStream gout=new GZIPOutputStream(bout);
		gout.write(data.getBytes());
		gout.close();//关闭流
		byte[] byteArray = bout.toByteArray();//得到压缩的数据
		
		System.out.println("压缩后数据大小:"+byteArray.length);
		response.addHeader("Content-Encoding", "gzip");
		response.addHeader("Content-Length", ""+byteArray.length);
		response.getOutputStream().write(byteArray);
	}

	//用location和302实现重定向
	private void test1(HttpServletResponse response) {
		response.setStatus(302);
		response.addHeader("location", "/JavaWeb/index.jsp");
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值