欢迎来到萧静默的博客

书山有路勤为径,学海无涯苦作舟。

java-servlet:response/request

获取请求request的一些方法

 

 

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h4>请在下面输入用户名及密码</h4>
<form action="/WEBTEST2/FormDemo" method = "post">
用户名:<input type = "text" name = "username"><br>
密码:<input type = "password" name = "password"><br>
<input type = "checkbox" name = "zq" value = "zuqiu">足球
<input type = "checkbox" name = "Lq" value = "lanqiu">蓝球
<input type = "checkbox" name = "PPq" value = "pingpangqiu">乒乓球
<input type = "submit" value = "提交"><br>
</form>

</body>
</html>
package com.king.demo;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FormDemo extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、获得请求方式
        String method = request.getMethod();
        // 2、获得请求的资源相关内容
        String URI = request.getRequestURI();
        StringBuffer URL = request.getRequestURL();
        String query = request.getQueryString();
        String HEADER = request.getHeader("User-Agent");//取指定头名称
        String username = request.getParameter("username");//指定请求体
        String password = request.getParameter("password");
        System.out.println(username + "...." + password);
        response.getWriter().write(username + "...." + password + "...." + method + "...." + URI + "...." + URL + ".."
                + query + ".." + HEADER);
        System.out.println(query);
        Map<String, String[]> parameterMap = request.getParameterMap();//以键值对获得请求体
        for (Map.Entry<String, String[]> entry: parameterMap.entrySet())
        {
            System.out.println(entry.getKey());
            for (String str  : entry.getValue())
            {
                System.out.println(str);
            }
        }
        System.out.println("-------------------");
        Enumeration<String> headername = request.getHeaderNames();//以枚举获取请求头
        while (headername.hasMoreElements())
        {
            String headname = headername.nextElement();
            String namevalue = request.getHeader(headname);
            response.getWriter().write(headname + ":" + namevalue);
            System.out.println(headname + ":" + namevalue);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}

控制台输出如下

1....2
null
username
1
password
2
zq
zuqiu
Lq
lanqiu
-------------------
host:localhost:8080
connection:keep-alive
content-length:40
cache-control:max-age=0
origin:http://localhost:6356
upgrade-insecure-requests:1
content-type:application/x-www-form-urlencoded
user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
referer:http://localhost:6356/WEBTEST2/form.html
accept-encoding:gzip, deflate, br

posted @ 2021-02-04 13:55  萧静默  阅读(61)  评论(0编辑  收藏  举报