开发者

Java Web: Detect a URL with a trailing question mark and empty query string

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开发者_如何学JAVA at all would be a null query string. But either way, my web app is getting request.getQueryString() == null.


String url = request.getRequestURL().toString();
if(url.indexOf("?")== -1){//it doesn't}


How about something like this:-

boolean hasTrailingQuestionMark = "GET".equals(request.getMethod()) && request.getParameterNames().hasMoreElements();

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.

UPDATE

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜