zoukankan      html  css  js  c++  java
  • 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
    }

    }

  • 相关阅读:
    MySQL server version for the right syntax to use near ‘USING BTREE
    随笔
    [python]自问自答:python -m参数?
    [linux]查看linux下端口占用
    [linux]scp指令
    [编程题目]泥塑课
    How can I learn to program?
    [python]在场景中理解装饰器
    [前端]分享一个Bootstrap可视化布局的网站
    [python]python元类
  • 原文地址:https://www.cnblogs.com/zhuawang/p/3737951.html
Copyright © 2011-2022 走看看