CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   Testing Value of QueryString (http://www.codingforums.com/showthread.php?t=274482)

billatl 09-27-2012 11:12 PM

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

Fou-Lu 09-27-2012 11:27 PM

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.

billatl 09-27-2012 11:42 PM

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

Fou-Lu 09-28-2012 04:07 AM

Quote:

Originally Posted by billatl (Post 1274246)
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.


All times are GMT +1. The time now is 04:52 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.