1. 程式人生 > >JavaWeb學習篇之----HTTP協議詳解

JavaWeb學習篇之----HTTP協議詳解

package com.http.demo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

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

public class HttpServletDemo extends HttpServlet {

	private static final long serialVersionUID = -7234797658264000867L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		//測試
		try{
			//test1(resp);//location響應頭
			//test2(resp);//Content-Encoding響應頭
			//test3(resp);//Content-Type響應頭
			test4(resp);//refresh響應頭
			//test5(resp);//content-disposition
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)throws ServletException, IOException {
		doGet(req,resp);
	}
	
	//使用響應頭location和302狀態碼實現請求重定向(位址列中的地址發生改變)
	public void test1(HttpServletResponse response){
		response.setStatus(302);
		response.setHeader("Location", "/aa/3.html");
	}
	
	//使用響應頭Content-Encoding實現資料的壓縮輸出
	public void test2(HttpServletResponse resp) throws Exception{
		//壓縮資料很大的時候壓縮效率才有體現,如果資料很小的話反而更大
		
		//壓縮資源:提高網頁的訪問效能,電信按照出口流量收錢的。
		String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";//壓縮資料大點還有效果
		System.out.println("壓縮前的資料大小:"+data.length());
		ByteArrayOutputStream bout = new ByteArrayOutputStream();//底層流
		GZIPOutputStream gout = new GZIPOutputStream(bout);//包裝流一般有緩衝,沒有把緩衝區寫滿,不會寫到底層流
		gout.write(data.getBytes());
		gout.close();//等於重新整理操作,將包裝流中的資訊重新整理
		
		byte gzip[] = bout.toByteArray();//得到壓縮後的資料
		
		System.out.println("壓縮後的資料大小:"+gzip.length);
		
		//通知瀏覽器資料採用壓縮格式
		resp.setHeader("Content-Encoding", "gzip");
		resp.setHeader("Content-Length", gzip.length+"");//表明長度
		resp.getOutputStream().write(gzip);//壓縮資料寫給瀏覽器
	}
	
	//使用響應頭content-type設定伺服器返回給client的資料型別
	public void test3(HttpServletResponse resp) throws Exception{
		//具體可以檢視tomcat目錄中的web.xml檔案
		resp.setHeader("Content-Type","image/jpeg");
		InputStream ins = this.getServletContext().getResourceAsStream("/1.jpg");
		int len = 0;
		byte[] buffer = new byte[1024];
		OutputStream ops = resp.getOutputStream();
		while((len = ins.read(buffer))!=-1){
			ops.write(buffer, 0, len);
		}
		ops.close();
	}
	
	//使用響應頭refresh實現頁面的定時重新整理
	public void test4(HttpServletResponse resp) throws Exception{
		//股票,聊天室
		
		//填充的值為:3;url="http://www.baidu.com"表示3s之後跳轉到http://www.baidu.com頁面
		//如果沒有http://www.baidu.com的話,只是在該頁面進行重新整理
		resp.setHeader("refresh", "3;url=\"http://www.baidu.com\"");
		String data = "aaaaaaaaaaaaaa";
		resp.getOutputStream().write(data.getBytes());
	}
	
	//使用響應頭content-disposition實現客戶機用下載的方式開啟資料資源
	public void test5(HttpServletResponse resp) throws Exception{
		resp.setHeader("content-disposition","attachment;filename=1.jpg");
		InputStream ins = this.getServletContext().getResourceAsStream("/1.jpg");
		int len = 0;
		byte[] buffer = new byte[1024];
		OutputStream ops = resp.getOutputStream();
		while((len = ins.read(buffer))!=-1){
			ops.write(buffer, 0, len);
		}
	}
	
}