侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 748 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

JavaWeb(5)之请求对象HttpServletRequest

zze
zze
2017-05-30 / 0 评论 / 0 点赞 / 742 阅读 / 3386 字

获取请求相关数据

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 进行解码了

0

评论区