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);
    };
}
原文地址:https://www.cnblogs.com/20193925zxt/p/14909884.html