Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

String url = request.getRequestURL().toString();
if(url.indexOf("?")== -1){//it doesn't}
share|improve this answer
1  
Or even url.endsWith("?") –  Johan Sjöberg Feb 2 '11 at 18:45
1  
@Johan or even contains("?"); –  Jigar Joshi Feb 2 '11 at 18:46
    
"getRequestURL(): Reconstructs the URL the client used to make the request. The returned URL contains ..., but it does not include query string parameters." See the API Docs –  George Bailey Feb 2 '11 at 18:56
    
I just tried it and it did not include a question mark no matter if the query string was blank, non existent or non blank. (Java EE 1.4 on a Java EE 5 testing server) –  George Bailey Feb 2 '11 at 18:58
    
ah... I see , Currently I can only see JavaScript, Interesting requirement , I would like to know why you want to check for ? –  Jigar Joshi Feb 2 '11 at 19:08

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.

share|improve this answer
    
Sorry, it returns false, even when I have a trailing question mark. Good idea though. –  George Bailey Feb 2 '11 at 19:13
    
Do you have parameters after the question mark, like this: http://server/bla?param=1 ? Or without parameters, like this: http://server/bla? ? –  limc Feb 2 '11 at 19:16
    
Without parameters. I am attempting to detect when there is a trailing question mark that happens to be the only question mark in the whole URL. –  George Bailey Feb 2 '11 at 19:55
    
I doubt you are able to achieve your goal here. A trailing ? without parameters is equivalent to not having the ?, thus the request API might just omit the ? out if no parameter exists. –  limc Feb 2 '11 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.