Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-27-2012, 11:12 PM   PM User | #1
billatl
New Coder

 
Join Date: Oct 2009
Posts: 51
Thanks: 6
Thanked 0 Times in 0 Posts
billatl is an unknown quantity at this point
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
billatl is offline   Reply With Quote
Old 09-27-2012, 11:27 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
billatl (09-27-2012)
Old 09-27-2012, 11:42 PM   PM User | #3
billatl
New Coder

 
Join Date: Oct 2009
Posts: 51
Thanks: 6
Thanked 0 Times in 0 Posts
billatl is an unknown quantity at this point
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
billatl is offline   Reply With Quote
Old 09-28-2012, 04:07 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:56 AM.


Advertisement
Log in to turn off these ads.