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
}

}

时间: 2024-11-05 16:38:03

30天轻松学习javaweb_http头信息实例的相关文章

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

telnet是windows自带的网络连接工具,可以用于连接任何服务器. 通过Telnet连接服务端 Telnet localhost 8080GET /news/1.html HTTP/1.1Host:将获取到如下信息: HTTP/1.1 200 OKServer: Apache-Coyote/1.1Accept-Ranges: bytesETag: W/"4-1400337154000"Last-Modified: Sat, 17 May 2014 14:32:34 GMTCont

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

jar -cvf news.war news 打包成 war 包后复制到webapps下,Tomcat将会解压. 30天轻松学习javaweb_打包web项目成war,布布扣,bubuko.com

30天轻松学习javaweb_模拟tomcat

运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import java.net.*; import java.io.*; public class Server { public static void main(String args[]) throws Exception { System.out.println("start"); ServerSoc

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

https通过非对称加密实现数据安全1.CA机构提供数字证书,其中数字证书包含公钥.2.浏览器自带功能验证数字证书是否是CA机构颁发的.3.根据数字证书包含的公钥对表单数据进行加密.4.公钥提供方再根据自己的私钥对数据进行解密. 30天轻松学习javaweb_https协议的密码学,布布扣,bubuko.com

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

在MyEclipse目录下搜索com.genuitec.eclipse.wizards得到搜索结果 com.genuitec.eclipse.wizards_9.0.0.me201108091322.jar解压出文件夹,修改com.genuitec.eclipse.wizards_9.0.0.me201108091322\templates\Servlet.java 的模板文件使用jar命令将文件夹打包成jar包:1.在dos窗口中进入到解压后的com.genuitec.eclipse.wiza

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

context.xml中增加<WatchedResource>WEB-INF/web.xml</WatchedResource>,Eclipse在修改了web.xml后将自动更新到tomcat服务器中. <?xml version='1.0' encoding='utf-8'?> <Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF

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

1.在server.xml文件的</Host>前面加入.需要重新启动Tomcat才能生效.<!--配置虚拟目录--><Context path="/itcast" docBase="d:\news"></Context>如果是<Context path="" docBase="d:\news"></Context>将覆盖成缺省目录,则路径是 http://

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

通过javac编译java文件1.先导入需要引用的包D:\Program Files (x86)\apache-tomcat-7.0.53\webapps\test\WEB-INF\classes>set classpath=%classpath%;D:\Program Files (x86)\apache-tomcat-7.0.53\lib\servlet-api.jar 2.将java文件按包名称编译到当前文件夹D:\Program Files (x86)\apache-tomcat-7.0

编译安装nginx并修改版本头信息—参考实例

今天做实验的时候,想起我那台yum安装的nginx+php-fpm+mysql服务器上的nginx版本有点低了,并且还要加两个第3方模块,就去nginx官网下载了最新稳定版nginx-1.0.6,好了,废话不多说看教程吧.  系统版本: centos 5.6  ip: 192.168.1.200  需要的软件包:nginx-1.0.6.tar.gz Nginx-accesskey-2.0.3.tar.gz ngx_cache_purge-1.3.tar.gz(这3个包可以自己去下载,我就不提供了