If statement is not validating properly in JSP

Advertisement

If statement is not validating properly in JSP
Actually i'm new to Java..
The problem is when i take the parameter from another file which is an empty text box and i submit that. In the next jsp file i'am taking this value using getParameter("xxxx").
if i send the empty or some value to this parameter, the if statement is not validating properly.
here is my code in jsp....
<%
String bgcolor = request.getParameter("bgColor");
out.println("Background Color = " + bgcolor);
if (bgcolor == null)
out.println("Hello Null");
else
out.println("Hello Not Null");
%>
Thanks in advance
Kumar
Repaly
Hey there, you should know that Strings in Java are
different from other types like int and double for
example. Strings are objects so you cant check em
with == but with the standard method .equals This is true but is not the problem here.
@OP null and an empty length string are NOT the same thing. You probably want a check like
if(bgColor.length()<1)And you might want to trim the String first to get rid of whitespace.
Read More: The other 6 answers