java 不推荐使用的 javax.servlet.http.HttpUtils.parseQueryString 的替代方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/248938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:38:06  来源:igfitidea点击:

Alternative to deprecated javax.servlet.http.HttpUtils.parseQueryString?

javaurlparsingquery-string

提问by Mads Hansen

I am looking to parse a URL to obtain a collection of the querystring parameters in Java. To be clear, I need to parse a given URL(or string value of a URL object), not the URL from a servlet request.

我希望解析一个 URL 以获取 Java 中查询字符串参数的集合。明确地说,我需要解析给定的 URL(或 URL 对象的字符串值),而不是来自 servlet 请求的 URL。

It looks as if the javax.servlet.http.HttpUtils.parseQueryStringmethod would be the obvious choice, but it has been deprecated.

看起来该javax.servlet.http.HttpUtils.parseQueryString方法是显而易见的选择,但它已被弃用。

Is there an alternative method that I am missing, or has it just been deprecated without an equivalent replacement/enhanced function?

是否有我缺少的替代方法,或者它是否只是在没有等效替换/增强功能的情况下被弃用?

采纳答案by gizmo

Well, as you mention that the URL does not come from a servlet request, the right answer is, as usual, it depends.

好吧,正如您提到的 URL 不是来自 servlet 请求,正确的答案是,像往常一样,这取决于

The problem with query part of an url is that there is no clear specification about how to handle parameters duplication.

url 的查询部分的问题是没有关于如何处理参数重复的明确规范。

For example, consider an url like this one:

例如,考虑这样一个 url:

http://www.example.com?param1=value1&param2=value2&param1=value3

What do you expect as a value for param1? the first value, the last one, an array? The issue is that, according to the specs, all these answers are valid and server vendor are free to support one of these or another. Some use the param1[] notation to indicate that it has to be treated as an array, but again, this is not a unified solution.

您期望 param1 的值是什么?第一个值,最后一个,数组?问题是,根据规范,所有这些答案都是有效的,服务器供应商可以自由地支持其中之一。有些人使用 param1[] 表示法来表示它必须被视为一个数组,但同样,这不是一个统一的解决方案。

So the "best" solution is to know how your destination handle parameters, and mimic the behaviour with a self-made utility class.

所以“最好”的解决方案是知道你的目的地如何处理参数,并用一个自制的实用程序类来模仿行为。

回答by Vincent Ramdhanie

I think the idea is to use the HttpServletRequest instead. There is the getParameterMap(), getParameterNames() and getParameterValues() methods to start.

我认为这个想法是使用 HttpServletRequest 代替。有 getParameterMap()、getParameterNames() 和 getParameterValues() 方法可以启动。

There is also the getParameter(String paramname) method to get the value of a specific method.

还有 getParameter(String paramname) 方法来获取特定方法的值。

These make no distinction between querystring parameters and form parameters though so if your intention was to look for a querystring in aparticular then I guess this wouldn't help.

这些在查询字符串参数和表单参数之间没有区别,所以如果您打算特别查找查询字符串,那么我想这无济于事。

回答by Richard

For all Kotlin lovers i came up with this:

对于所有 Kotlin 爱好者,我想出了这个:

fun splitQuery(url: URL): Map<String, List<String?>> = url.query?.let {
        it.split("&").map {
            it.split("=").let {
                Pair(it[0], it.getOrNull(1))
            }
        }.groupBy({ it.first }, {
            it.second
        })
    } ?: emptyMap()

回答by Hyman Leow

As far as I know, there isn't one.

据我所知,没有。

It shouldn't be too difficult to write one yourself though. The hardest part, I imagine, would be decoding the URL name/values (which really isn't that hard, if you think about it), and you can use java.net.URLDecoder#decodeURL(String,String)for that.

不过自己写一个应该不会太难。我想,最难的部分是解码 URL 名称/值(如果你仔细想想,这真的没那么难),你可以使用java.net.URLDecoder#decodeURL(String,String)它。