Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Question Testing Value of QueryString

    Hello,

    I'm trying to get the full URL of a webpage and am running into a problem testing the query string. Here's my code so far:

    Code:
    String requestURL = request.getRequestURL().toString();
    String queryString = request.getQueryString();
    
    <%= requestURL %>+<%= queryString %>
    
    if (queryString.equals("")) {
       out.println("is null"); 
    } else {
    	out.println("not null");
    This code works fine so long as there is a query string; if not I get a runtime error on the IF statement. I tried changing queryString definition to request.getQueryString().toString; - but it also fails if there's no queryString.

    Perhaps a better question is, how do I properly test for null

    Thanks

  2. #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    I don't do JSP at all, but you cannot compare queryString.equals if request.getQueryString returns null (this would be common in just regular java of course, but I don't remember if it applies at the JSP level as well). So if the request.getQueryString() can return null, then you simply check that as well: if (querystring != null) instead of .equals(""). If its not null, then you can then check the .equals("") to see if its empty if desired in the else block.

  3. Users who have thanked Fou-Lu for this post:

    billatl (09-27-2012)

  4. #3
    New Coder
    Join Date
    Oct 2009
    Posts
    51
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Yea, that work. I'd originally tried if (queryString = null) { but was getting a String vs. Boolean mismatch. Hadn't thought of using not instead.

    Thanks

  5. #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    17,027
    Thanks
    4
    Thanked 2,668 Times in 2,637 Posts
    Quote Originally Posted by billatl View Post
    Yea, that work. I'd originally tried if (queryString = null) { but was getting a String vs. Boolean mismatch. Hadn't thought of using not instead.

    Thanks
    This happens since the result of a string assignment is the value of the string, not a boolean result (even if the value of the string is null, that would then be equatable to if ((stringvar = null) == null)). What would work is if (querystring == null), as that would return true if it is null and false otherwise.


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •