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头信息实例的更多相关文章

  1. 30天轻松学习javaweb_通过telnet连接http服务器

    telnet是windows自带的网络连接工具,可以用于连接任何服务器. 通过Telnet连接服务端 Telnet localhost 8080GET /news/1.html HTTP/1.1Hos ...

  2. 30天轻松学习javaweb_Eclipse在修改了web.xml后将自动更新到tomcat服务器中

    context.xml中增加<WatchedResource>WEB-INF/web.xml</WatchedResource>,Eclipse在修改了web.xml后将自动更 ...

  3. 30天轻松学习javaweb_https协议的密码学

    https通过非对称加密实现数据安全1.CA机构提供数字证书,其中数字证书包含公钥.2.浏览器自带功能验证数字证书是否是CA机构颁发的.3.根据数字证书包含的公钥对表单数据进行加密.4.公钥提供方再根 ...

  4. 30天轻松学习javaweb_打包web项目成war

    jar -cvf news.war news 打包成 war 包后复制到webapps下,Tomcat将会解压.

  5. 30天轻松学习javaweb_tomcat的虚拟目录设置

    1.在server.xml文件的</Host>前面加入.需要重新启动Tomcat才能生效.<!--配置虚拟目录--><Context path="/itcast ...

  6. 30天轻松学习javaweb_模拟tomcat

    运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import jav ...

  7. 30天轻松学习javaweb_Range实现断点续传

    package com.wzh.test.http; import java.io.FileOutputStream; import java.io.IOException; import java. ...

  8. 30天轻松学习javaweb_修改tomcat的servlet模板

    在MyEclipse目录下搜索com.genuitec.eclipse.wizards 得到搜索结果 com.genuitec.eclipse.wizards_9.0.0.me201108091322 ...

  9. 30天轻松学习javaweb_通过javac编译java文件

    通过javac编译java文件1.先导入需要引用的包D:\Program Files (x86)\apache-tomcat-7.0.53\webapps\test\WEB-INF\classes&g ...

随机推荐

  1. Html-双斜杠//开头的URL(依赖协议的URL)

    今天看京东HTTPS,发现链接都是这种 <a href="//chaoshi.jd.com">京东超市</a> URL是以双斜杠"//" ...

  2. Retrofit2文件上传下载及其进度显示

    序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...

  3. AndroidStudio导入项目常见问题

    问题一: 解决:少了依赖的路径 问题二: 解决:把runProguard 改成minfyEnable 来源:http://blog.csdn.net/pengkv/article/details/44 ...

  4. linux ascii艺术与ansi艺术

    Linux终端下的ASCII艺术 http://zh.wikipedia.org/zh-tw/%E9%9B%BB%E5%AD%90%E9%81%8A%E6%88%B2%E5%8F%B2 电子游戏史 h ...

  5. unity 合并skinnedMeshRenderer中遇到的一个大坑

    将多个skinnedMeshRenderer合并成一个skinnedMeshRenderer,主要涉及的mesh合并.骨骼列表合并.重定向顶点骨骼索引.其中重定向顶点骨骼索引只是通过加偏值即可完成,所 ...

  6. python join与split函数的用法举例

    python join 和 split方法: join用来连接字符串,split恰好相反,拆分字符串的. 来看有关join.split方法的例子 1,join用法的例子 复制代码 代码示例: > ...

  7. MySQL 体系结构以及各种文件类型学习汇总 (转)

    1,mysql体系结构 由数据库和数据库实例组成,是单进场多线程架构. 数据库:物理操作系统文件或者其它文件的集合,在mysql中,数据库文件可以是frm.myd.myi.ibd等结尾的文件,当使用n ...

  8. 【oracle】 linux 下oracle 启动监听错误

    Message 1070 not found; No message file for product=network, facility=TNSTNS-12545: Message 12545 no ...

  9. shopex用户登陆错误提示在nginx下乱码问题

    http://www.test.cn/passport-aHR0cDosLHd3dy54eTAwNy5jbixwYXNzcG9ydC1hSFIwY0Rvc0xIZDNkeTU0ZVRBd055NWpi ...

  10. Asp.Net MVC 路由 - Asp.Net 编程 - 张子阳

    http://cache.baiducontent.com/c?m=9d78d513d98316fa03acd2294d01d6165909c7256b96c4523f8a9c12d522195646 ...