Request获取请求头数据和Request获取请求体数据

获取请求头数据

  • 方法:
    • String getHeader(String name):通过请求头的名称获取请求头的值
    • getHeaderNames():获取所有的请求头名称
package com.ailyt.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;

@WebServlet(value = "/r2")
public class RequestStudy2 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*        //演示获取请求头数据

        //获取所有请求头名称
        Enumeration<String> headerNames = req.getHeaderNames();
        //遍历
        while(headerNames.hasMoreElements()){
            String name = headerNames.nextElement();
            //根据名称获取请求头的值
            String value = req.getHeader(name);
            System.out.println(name + "---" + value);
        }*/

/*        //演示获取请求头数据:user-agent
        String agent = req.getHeader("user-agent");
        //判断agent的浏览器版本
        if (agent.contains("Chrome")){
            //谷歌
            System.out.println("谷歌来了");
        }else if(agent.contains("Firefox")){
            //火狐
            System.out.println("火狐来了");
        }*/

        //演示获取请求头数据:referer
        String referer = req.getHeader("referer");
        System.out.println(referer);//http://localhost:8080/login.html
        //防盗链
        if (referer != null) {
            if (referer.contains("/day")) {
                //正常访问
//                System.out.println("播放电影...");
                resp.setContentType("text/html;charset=utf-8");
                resp.getWriter().println("播放电影...");
            } else {
                //盗链
//                System.out.println("想看电影么?来优酷吧");
                resp.getWriter().println("想看电影么?来优酷吧");
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

Request获取请求体数据

  • 获取请求体数据:
    • 请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数
    • 步骤:
      • 获取流对象
        • BufferedReader getReader():获取字符输入流,只能操作字符数据
        • ServletInputStream getInputStream():获取字节输入流,可以操作所有类型数据
          • 再文件上传知识点后讲解
      • 再从流对象中拿数据\
package com.ailyt.request;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;

@WebServlet(value = "/r3")
public class RequestStudy3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求消息体--请求参数

        //获取字符流
        BufferedReader reader = req.getReader();
        //读取数据
        String line = null;
        while ((line = reader.readLine()) != null) {
            System.out.println(line);

        }
    }
}

posted @ 2022-08-09 13:53  我滴妈老弟  阅读(402)  评论(0编辑  收藏  举报