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头信息实例的更多相关文章
- 30天轻松学习javaweb_通过telnet连接http服务器
telnet是windows自带的网络连接工具,可以用于连接任何服务器. 通过Telnet连接服务端 Telnet localhost 8080GET /news/1.html HTTP/1.1Hos ...
- 30天轻松学习javaweb_Eclipse在修改了web.xml后将自动更新到tomcat服务器中
context.xml中增加<WatchedResource>WEB-INF/web.xml</WatchedResource>,Eclipse在修改了web.xml后将自动更 ...
- 30天轻松学习javaweb_https协议的密码学
https通过非对称加密实现数据安全1.CA机构提供数字证书,其中数字证书包含公钥.2.浏览器自带功能验证数字证书是否是CA机构颁发的.3.根据数字证书包含的公钥对表单数据进行加密.4.公钥提供方再根 ...
- 30天轻松学习javaweb_打包web项目成war
jar -cvf news.war news 打包成 war 包后复制到webapps下,Tomcat将会解压.
- 30天轻松学习javaweb_tomcat的虚拟目录设置
1.在server.xml文件的</Host>前面加入.需要重新启动Tomcat才能生效.<!--配置虚拟目录--><Context path="/itcast ...
- 30天轻松学习javaweb_模拟tomcat
运行 javac Server.java 编译java文件 执行 java Server 运行程序 在ie中输入 http://localhost:9999/ 打开模拟的服务程序 import jav ...
- 30天轻松学习javaweb_Range实现断点续传
package com.wzh.test.http; import java.io.FileOutputStream; import java.io.IOException; import java. ...
- 30天轻松学习javaweb_修改tomcat的servlet模板
在MyEclipse目录下搜索com.genuitec.eclipse.wizards 得到搜索结果 com.genuitec.eclipse.wizards_9.0.0.me201108091322 ...
- 30天轻松学习javaweb_通过javac编译java文件
通过javac编译java文件1.先导入需要引用的包D:\Program Files (x86)\apache-tomcat-7.0.53\webapps\test\WEB-INF\classes&g ...
随机推荐
- javascript多重继承
function employee(name, job, born) { this.name = name; this.job = job; this.born = born;} function h ...
- radhat 6.4/centos 6.4 下编译安装 最新ruby 2.1.5
#安装编译环境 yum groupinstall "Development tools" 或者 yum install gcc gcc-c++ gcc-g77 flex bison ...
- $.ajax提交,后台接受到的值总是乱码?明天再总结
//首先说明,我的服务器和页面编码都是GBK,所以尝试了很多种GBK的方式前台:function printFunction(){ window.print(); $.ajax({ url : '/t ...
- UserDefault的使用,保存小数据到本地-iOS
//保持到本地数据 NSArray *array=@[@"234",@"sdfe"]; NSUserDefaults *userDefault=[NSUserD ...
- C#学习之LinqtoSql类的简单例子
LinqtoSql类把访问.操作数据库的细节封装了起来,把连接操作数据库变得相当简单.下面是简单的例子. 第一步:添加LinqtoSql类 1.创建一个控制台应用程序项目,下载一个NrothWind ...
- 怎么让OCR文字识别软件转换别的语言文档
ABBYY PDF Transformer+让您可创建或转换希伯来语.意第绪语.日语.中文.泰语.韩语和阿拉伯语的文档.那么如何顺利使用这些复杂语言文字呢?小编教你两步骤轻松快速处理包含以下复杂语言文 ...
- 将Xml或Json生成类的最简单方式
来源于 http://stackoverflow.com/questions/3187444/convert-xml-string-to-object
- 在线网络速度测试JAVA程序(一):思路和控制台主程序【转】
来源:http://hancool.blog.51cto.com/1836252/1352228 事情的缘由 因上级公司的信息化主管部门经常被投诉说是各种业务应用系统反映系统使用慢的问题,而都把问题归 ...
- Python画图形界面
使用QTdesigner 生成.ui文件,输入命令行pyuic4 -o test.py test.ui 在生成的Python文件后面输入下面代码 if __name__=="__main__ ...
- Codeigniter MongoDB类库
安装方法:1.将mongodb.php 放到config目录2.将Mongo_db.php放到library目录 使用方法: $this->mongo_db->where_gte('age ...