问题
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