本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名原文链接~~~

JavaWeb(5)之请求对象HttpServletRequest

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:


获取请求相关数据

package com.zze.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

public class TestSerlvet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp){
        // 获取请求头
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            String headerValue = req.getHeader(headerName);
            System.out.println(String.format("%s:%s", headerName, headerValue));
        }
        // cache-control:no-cache
        // postman-token:7b0b1d95-d1bd-4282-b516-40ea14910741
        // user-agent:PostmanRuntime/7.3.0
        // accept:*/*
        // host:localhost:8080
        // cookie:JSESSIONID=BB60472100AF2B9054BC9F61515203A8
        // accept-encoding:gzip, deflate
        // connection:keep-alive

        // 获取请求参数
        Map<String, String[]> parameterMap = req.getParameterMap();
        Iterator<String> keys = parameterMap.keySet().iterator();
        while (keys.hasNext()) {
            String key = keys.next();
            String[] values = parameterMap.get(key);
            System.out.println(String.format("%s:%s", key, values[0]));
        }
        // name:zhangsan
    }
}

解决中文请求参数乱码

方法一:手动编码再解码

package com.zze.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;

public class TestSerlvet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        String value = req.getParameter("name");
        // 因为我们提交参数时编码是"UTF-8" 而req.getParameter 默认使用 "ISO-8859-1" 进行解码,
        // 所以我们可以先用 "ISO-8859-1" 将其编码回字节数组,然后使用"UTF-8"解码就可正确显示内容
        String valueStr = new String(value.getBytes("ISO-8859-1"), "UTF-8");
        System.out.println(String.format("%s:%s", "name", valueStr));
        // name:张三
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 设置以什么编码解析请求体,对 GET 方式无用
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("name");
        System.out.println(name);
        // 张三
    }
}

方法二:修改 Tomcat 配置

在 tomcat 目录下的 conf/server.xmlConnector 节下加上 URIEncoding 属性:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8" />

之后 req.getParameter 就会使用 UTF-8 进行解码了

# JavaWeb  

如果这篇文章对您有帮助,可点击下方链接分享给你的朋友们😋,如果遇到问题欢迎评论、留言~~~😇

评论

公众号:zze_coding

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×