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. SQL Server AlwaysOn articles

    Troubleshooting SQL Server AlwaysOn http://download.microsoft.com/download/0/F/B/0FBFAA46-2BFD-478F- ...

  2. 自然语言处理3.3——使用Unicode进行文字处理

    全世界有多种语言,经常需要应用程序处理不同的语言和字符集.下面将介绍如何利用Unicode处理使用非ASCII字符集文字. 1.什么是Unicode Unicode支持一百万种以上的字符,每一个字符分 ...

  3. OpenJudge计算概论-矩阵归零消减序列和

    矩阵归零消减序列和 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个n*n的矩阵( <= n <= ,元素的值都是非负整数).通过n-1次实施下述过程,可把这个矩阵转 ...

  4. 15条变量&方法命名的最佳实践【转】

    原文地址:15 Best Practices of Variable & Method Naming 不同的代码段采用不同的命名长度.通常来说,循环计数器(loop counters)采用1位 ...

  5. Rest Client(Rest接口调试工具,有保存功配置功能) chrome浏览器插件

    Rest Client(Rest接口调试工具,有保存功配置功能) chrome浏览器插件 下载地址 插件的操作很简单,下面是一些简单的实例. 1.安装 在谷歌应用商城搜索postman,如下图1-1所 ...

  6. C++中的空类与空结构体大小

    今天面试遇到了一个很有意思的问题,即空结构体在C++中所占的内存大小是多少?参见如下代码: #include <iostream> struct S0 { }; int main() { ...

  7. jsonp get 和 post

    原文地址:http://blog.sina.com.cn/s/blog_4a7e719d0100zqzh.html jsonp获取服务器的数据,有两种一,跨域二,不跨域如果跨域js的写法有两种1,&l ...

  8. 在mui中遇到的内容覆盖导航栏的问题

    一.问题描述: 公司项目中为了让内容以页面的形式显示,并要格式化页面内容,采用了百度的UEditor编辑器来显示内容,但是遇到了一个问题就是当下拉页面到一定距离之后,页面上方的导航栏会被内容遮盖. 二 ...

  9. 关于css中列表(ul ol)存在默认间距的问题

    一.总结: 有时候我们要给列表(ul ol 本身就是属于块级元素)的上表框或下边框设置颜色,如下: 但是在给内联块级元素(inline-block)的上表框或下边框设置颜色的时候,就没有这么简单: 在 ...

  10. 关于今天mysql数据库的一系列问题

    首先,字符集的问题: mysql> show variables like 'character%';+--------------------------+------------------ ...