zoukankan      html  css  js  c++  java
  • 4月16日学习日志

    今天学习了HTTP响应头和请求头。

    通过Location实现页面重定向

    实现代码

    package com.jay.http.test;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ServletOne extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            //告诉浏览器响应码,以及重定向页面
            resp.setStatus(302);
            resp.setHeader("Location", "http://www.baidu.com");
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            this.doGet(req, resp);
        }
    }

    通过Content-Encoding告诉浏览器数据的压缩格式

    实现代码:

    package com.jay.http.test;
    
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    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 ServletTwo extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            String data = "Fresh air and sunshine can have an amazing effect on our feelings. "
                    + "Sometimes when we are feeling down, all that we need to do is simply to go "
                    + "outside and breathe. Movement and exercise is also a fantastic way to feel better. "
                    + "Positive emotions can be generated by motion. So if we start to feel down,"
                    + " take some deep breathes, go outside, feel the fresh air, "
                    + "let the sun hit our face, go for a hike, a walk, a bike ride, "
                    + "a swim, a run, whatever. We will feel better if we do this.";
            System.out.println("原始数据长度:" + data.getBytes().length);
            // 对数据进行压缩:
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            GZIPOutputStream gout = new GZIPOutputStream(bout);
            gout.write(data.getBytes());
            gout.close();
            // 得到压缩后的数据
            byte gdata[] = bout.toByteArray();
            resp.setHeader("Content-Encoding", "gzip");
            resp.setHeader("Content-Length", gdata.length + "");
            resp.getOutputStream().write(gdata);
    
        }
        protected void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            doGet(req, resp);
        };
    }

    通过content-type,设置返回的数据类型

    package com.jay.http.test;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class ServletThree extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                throws ServletException, IOException {
            resp.setHeader("content-type", "application/pdf");
            InputStream in = this.getServletContext().getResourceAsStream("/file/android编码规范.pdf");
            byte buffer[] = new byte[1024];
            int len = 0;
            OutputStream out = resp.getOutputStream();
            while((len = in.read(buffer)) > 0)
            {
                out.write(buffer,0,len);
            }
        }
        
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws
           ServletException ,IOException 
        {
            doGet(req, resp);
        };
    }
  • 相关阅读:
    angularjs html事件(ng-click,ng-hide,ng-show)
    《javascript设计模式与开放实践》学习(二)对象方法的借用
    《javascript设计模式与开放实践》学习(二)Function.prototype.bind
    《javascript设计模式与开放实践》学习(一)javascript实现多态2
    《javascript设计模式与开放实践》学习(一)javascript实现多态1
    关于BFC
    css三角形的绘制
    ER图
    如何使用powerDesigner 生成 html文件
    spring aop 会根据实际情况(有无接口)自动选择 两种 动态代理(jdk和cglib)之一
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14909884.html
Copyright © 2011-2022 走看看