30天轻松学习javaweb_http头信息实例

package com.wzh.test.http;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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 ServletDemo1 extends HttpServlet {

/**
* Constructor of the object.
*/
public ServletDemo1() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

/*response.addHeader("content-disposition", "attachment;filename=3.jpg");

InputStream in=this.getServletContext().getResourceAsStream("/hadoop.jpg");
int len=0;
byte buffer[]=new byte[1024];
OutputStream out=response.getOutputStream();
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}*/

// test1(response);
// test2(response);
// test3(response);
test4(response);
}

//定时刷新
private void test4(HttpServletResponse response) throws IOException {
response.setHeader("refresh", "2");
String data="aaaa123rws";
response.getOutputStream().write(data.getBytes());
}

//通过content-type头,通知浏览器以何种方式解析文件
public void test3(HttpServletResponse response) throws IOException {
response.addHeader("content-type", "image/jpeg");
InputStream in=this.getServletContext().getResourceAsStream("/hadoop.jpg");
int len=0;
byte buffer[]=new byte[1024];
OutputStream out=response.getOutputStream();
while((len=in.read(buffer))>0){
out.write(buffer,0,len);
}
}

//数据压缩
public void test2(HttpServletResponse response) throws IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
System.out.println("原始数据大小:"+data.getBytes().length);
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout);
gout.write(data.getBytes());
gout.close();//关闭包装流,数据就到底层流

byte gzis[] = bout.toByteArray();// 得到压缩后的数据
System.out.println("压缩后大小:"+gzis.length);
response.setHeader("content-Encoding", "gzip");
response.setHeader("Content-Length", gzis.length+"");
response.getOutputStream().write(gzis);
}

//通过发送302状态码,让客户端跳转
public void test1(HttpServletResponse response) {
response.setStatus(302);
response.addHeader("location", "http://www.baidu.com");
}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

30天轻松学习javaweb_http头信息实例,布布扣,bubuko.com

30天轻松学习javaweb_http头信息实例

上一篇:在C语言控制台程序中播放MP3音乐


下一篇:用纯 CSS 创建一个三角形