Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将请求参数提取到名称值集合中_Java_Parsing_Http Request Parameters - Fatal编程技术网

Java 如何将请求参数提取到名称值集合中

Java 如何将请求参数提取到名称值集合中,java,parsing,http-request-parameters,Java,Parsing,Http Request Parameters,我有一个请求参数,比如 usrInfo:fname=firstname&lname=lastname&company=&addressLine1=wewqe&addressLine2=wqewqe&city=qweqwe&country=美国+美国 我想提取每个名称的值 我已经写了下面的方法,但当对应的名称对没有值时,它就失败了 private String getRequestParamavalue(SlingHttpServletRequest request, String request

我有一个请求参数,比如

usrInfo:fname=firstname&lname=lastname&company=&addressLine1=wewqe&addressLine2=wqewqe&city=qweqwe&country=美国+美国

我想提取每个名称的值

我已经写了下面的方法,但当对应的名称对没有值时,它就失败了

private String getRequestParamavalue(SlingHttpServletRequest request, String requestParameter, String requestParamName) {

        String reqParamValue = null;

        if (StringUtils.isNotBlank(request.getParameter(requestParameter))) {

            String[] reqParams = request.getParameter(requestParameter).split("&");
            Map<String, String> requestParamMap = new HashMap<>();
            String value;
            for (String param : reqParams) {
                String name = param.split("=")[0];
                value = StringUtils.isEmpty(param.split("=")[1]) ? "" : param.split("=")[1];
                requestParamMap.put(name, value);
            }

            reqParamValue = requestParamMap.get(requestParamName);

        }

        return reqParamValue;

    }
私有字符串getRequestParamavalue(SlingHttpServletRequest请求,字符串requestParameter,字符串requestParamName){
字符串reqParamValue=null;
if(StringUtils.isNotBlank(request.getParameter(requestParameter))){
字符串[]reqParams=request.getParameter(requestParameter.split(“&”);
Map requestParamMap=新HashMap();
字符串值;
for(字符串参数:reqParams){
字符串名称=参数拆分(“=”[0];
value=StringUtils.isEmpty(param.split(“=”[1])?“”:param.split(“=”[1]);
requestParamMap.put(名称、值);
}
reqParamValue=requestParamMap.get(requestParamName);
}
返回值;
}
请给我一个建议。谢谢。

@Kali试试这个

        for (String param : reqParams) {
            String name = param.split("=")[0];
            value = param.split("=").length == 1 ? "" : param.split("=")[1];
            requestParamMap.put(name, value);
        }

为什么不简单地执行request.getParameter(requestParamName),HTTPServlet父类会为您进行解析。

当没有对应的名称对时。检查param.split(“=”)的长度。的可能重复项