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

回眸只為那壹抹淺笑 提交于 2019-12-12 05:45:27

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/4878274/java-web-detect-a-url-with-a-trailing-question-mark-and-empty-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!