首页 > 其他分享 >Request_获取请求头数据与Request_获取请求体数据

Request_获取请求头数据与Request_获取请求体数据

时间:2023-01-30 13:24:47浏览次数:43  
标签:请求 Request request 获取 jakarta import servlet

Request_获取请求头数据

      获取请求头数据

          方法:String getHeader(String name):通过请求头的名称获取请求头的值

            Enumeration<String>  getHeaderNames();:获取所有的请求头名称 

 

1.演示获取请求头数据   
package jinghai.xueqiang.web.request;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.util.Enumeration;

@WebServlet(name = "RequestDemo2", value = "/RequestDemo2")
public class RequestDemo2 extends HttpServlet {
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //演示获取请求头数据

            //1.获取所有请求名称
        Enumeration<String> h1 = request.getHeaderNames();
            //2.遍历
        while(h1.hasMoreElements()){
            String name = h1.nextElement();
            //根据名称获取请求头的值
            String header = request.getHeader(name);
            System.out.println(name+"----"+header);
        }
    }
}
 2.演示获取请求头数据:user-agent
package jinghai.xueqiang.web.request;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.Enumeration;

@WebServlet("RequestDemo3")
public class RequestDemo3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         //演示获取请求头数据:user-agent

        String header = request.getHeader("user-agent");
        //判断agent的浏览器版本
        if (header.contains("Chrome")){
            //谷歌
            System.out.println("谷歌来了.....");
        }else if (header.contains("Ficcfox")){
            //火狐
            System.out.println("火狐来了.....");
        }
    }
}

3.演示获取请求头数据:referer

package jinghai.xueqiang.web.request;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("RequestDemo4")
public class RequestDemo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //演示获取请求头数据:referer


        String referer = request.getHeader("referer");
        System.out.println(referer);    //http://localhost/day14/login.html



            //防盗链
        if (referer !=null){
            if (referer.contains("day14")){
                //正常访问
                //System.out.println("播放电影");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("播放电影");
            }else {
                //盗版
                //System.out.println("想看电影嘛来bilibili吧.....");
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("想看电影嘛来bilibili吧.....");
            }
        }
    }
}

 

Request_获取请求体数据    

      获取请求体数据

          请求体:只有POST请求方式,才有请求体,在请求体中封装了POST请求的请求参数

          步骤:

              1.获取流对象

                  BuffererdReader    getReader():获取字符输入流,只能操作字符串数据

                  ServletInputStream  getInputStream():获取字节输入流,可以操作所有类型数据

              2.再从流对象中拿数据   

 RequestDemo5

package jinghai.xueqiang.web.request;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.BufferedReader;
import java.io.IOException;

@WebServlet("RequestDemo5")
public class RequestDemo5 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取请求消息体 -- 请求参数
                 //1.获取字符流
        BufferedReader br = request.getReader();
                //2.读取数据
        String ling = null;
        while ((ling=br.readLine())!=null){
            System.out.println(ling);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

regist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
</head>
<body>
    <form action="RequestDemo5.java" method="post"></form>
            <input type="text" placeholder="请输入账号" name="username"><br>
            <input type="text" placeholder="请输入密码" name="password"><br>
            <input type="submit"value="注册">
</body>
</html>

 

标签:请求,Request,request,获取,jakarta,import,servlet
From: https://www.cnblogs.com/x3449/p/17074904.html

相关文章