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);
        };
    }
  • 相关阅读:
    el-select remote 远程搜索 多个共享一个options,options改变时输入框值不显示名称的问题
    vue 中数据共享的方式
    关于AI本质的思考
    人工智能——一场精妙的商业炒作
    相关下载链接
    只用两个问题通关《极限挑战皇家宝藏》最后一关
    常见图片格式详解
    改写《python基础教程》中的一个例子
    介绍四款windows下的神器
    实现windows批处理下的计时功能
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14909884.html
Copyright © 2011-2022 走看看