Java Web:检测带有尾随问号和空查询字符串的 URL [英] Java Web: Detect a URL with a trailing question mark and empty query string
问题描述
有什么方法可以判断 URL 中是否有尾随问号?理论上,这将是一个空的非空查询字符串,而根本没有问号将是一个空查询字符串.但无论哪种方式,我的网络应用程序都得到 request.getQueryString() == null
.
Is there any way I can tell if there is a trailing question mark in a URL? This would theoretically be an empty non null query string while no question mark at all would be a null query string. But either way, my web app is getting request.getQueryString() == null
.
推荐答案
这样的事情怎么样:-
boolean hasTrailingQuestionMark = "GET".equals(request.getMethod()) && request.getParameterNames().hasMoreElements();
我可能是错的,但如果请求是 GET 并且它有参数,那么我认为我们可以安全地假设 URI 后面有一个尾随问号.
I could be wrong, but if the request is a GET and it has parameters, then I think we can safely assume there is a trailing question mark after the URI.
更新
我刚刚测试了代码,这种方法只有在你有参数时才有效:http://server/bla?param=1
.但是,如果您只有 http://server/bla?
,则此条件将失败.不知道你是不是想抓取后面的网址签名.
I just tested the code, this approach works only if you have parameters: http://server/bla?param=1
. However, if you have just http://server/bla?
, this condition will fail. I don't know if you are trying to capture the latter URL signature.
这篇关于Java Web:检测带有尾随问号和空查询字符串的 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!