aspose file tools*
The moose likes Servlets and the fly likes null String vs String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "null String vs String "null"" Watch "null String vs String "null"" New topic
Author

null String vs String "null"

Ben Zhang
Greenhorn

Joined: Sep 24, 2008
Posts: 12
Hi,
I searched many places but could not find anything related to my problem.

Here is the problem:
A jsp has a parameter named, say, power, in its form.


Upon POST, a servlet gets the value via


However, if the parameter "power" happens to be a null string (as depicted in above code), then mypower is not null, but it is a true String with value "null".

This is odd, what if the power is indeed "null"? How do I know which is null and which is "null"? (Unless I did something wrong, of course.)

Thank you,
Ben
Gopikrishna Kunisetty
Ranch Hand

Joined: Jun 12, 2008
Posts: 35
Hi Ben Zhang ,

null is a key word used to initialize variables. In your case as you have declared the power variable and initialized it to null, mypower will have the same value. The difference between null and "null" is the later is a specific value similar to"xyz" and the earlier means it has no value, simply blank. If you try to access the earlier one (null) you will get a "null pointer exception". But there will be no issue in accessing the later one ("null"). To distinguish between these two simply compare them with null.

ex: String s1 = null;
String s2 = "null";
if(s1==null)
{ do something }
Hope this will clarify you.


- Krishna<br /> SCJP 1.4 SCWCD 5
Ben Zhang
Greenhorn

Joined: Sep 24, 2008
Posts: 12
Hi Krishna,

In my case, I want null, but I get "null".

Thanks,
Ben
Gopikrishna Kunisetty
Ranch Hand

Joined: Jun 12, 2008
Posts: 35
Hi Ben,

Here is what actually happens. When you assign your variable(powerVal) value to the hidden parameter, whatever the value it has, the hidden type parameter treats it as value. Though you have declared powerVal and initialized it to null, it becomes a proper non-null string value for the hidden parameter.
Instead, what you can do is, simply put your variable into the request scope using setAttribute method and retrieve it wherever you want. This will give you the desired result.
[ September 24, 2008: Message edited by: Gopikrishna Kunisetty ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

All HTTP request parameters values are strings. If you look at the HTML source that is being displayed in the browser for your form, you will see that the value of the hidden element (in addition to not being properly quoted) is the string null.

The only time you'll get a true null from getParameter() is when the named element does not exist.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Zhang
Greenhorn

Joined: Sep 24, 2008
Posts: 12
Thanks Krishna, using Attribute is the solution.

Thanks Bear for the explanation.

Just my opinion, the behaviour that JSP/Servlet secretly change something under the hood makes debugging quite difficult. Could just error out if null hidden values are not allowed. Could it be container dependent? I am also quite surprised that I could not find people talking about this on the net.

Thanks guys,
Ben
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Originally posted by Ben Zhang:
Just my opinion, the behaviour that JSP/Servlet secretly change something under the hood makes debugging quite difficult.
Nothing got "changed under the hood". It's doing exactly what you told it to do. And it has nothing to do with JSP or servlets.

Could just error out if null hidden values are not allowed.
Any text value is allowed. How would it "error out" on something that's completely valid? How do you expect a true null to be placed in an HTML page? How do you expect it to be expressed in a text-oriented protocol like HTTP?

I am also quite surprised that I could not find people talking about this on the net.
There's nothing to talk about. The first T in HTTP stands for text. No one expects it to do anything differently.
[ September 24, 2008: Message edited by: Bear Bibeault ]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Just so you have a reference to see where this all comes from... The JSP uses a JSPWriter to output data. In the API for the JSPWriter#print(String) method it tells you that any null value will be translated to "null". So this is specified in the API, though it would take some search to know it.


Steve
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

One could argue that having a JSP scriptlet expression that evaluates to null emit the string "null" is sorta stupid. And I'd probably agree. The EL fixes this issue by emitting nothing.

So in this example, using the EL you'd get back the empty string. But there's still no way to get back a Java null -- that's simply not a concept that's expressible using HTTP.

If you want a null, don't create the element in the first place.
Ben Zhang
Greenhorn

Joined: Sep 24, 2008
Posts: 12
Thanks Steve for the reference, which clears the issue with official source.

I don't like the way it is now, but at least I have some black magic to work with, Attribute (Krishna) and EL+empty string (Bear).

Thanks for the help,
Ben
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

I don't really care for the attribute solution. By using it, your data is coming from two sources: the form, and the stored session scoped variable (the correct term for "attribute"). This is architecturally fragile.

I'd prefer to see you use the form as the only data conduit. You can either omit the hidden variable when the value would be null, and hence getParameter() would return the Java null, or use the EL (much preferably to putting Java code in JSPs) and interpret the empty string accordingly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null String vs String "null"