Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused: request.getParameter("userName") returns null

 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

i have a jsp in which i validate an input from a FORM parameter ("userName"), I check if the
parameter is NULL:

<%
String uName = request.getParameter("userName");
%>

so that is is NULL i can forward to another JSP:
<%
if (uName == null) {
%>
<jsp:forward page="validateuser.jsp"/>
<%
}
%>

But even if i don't fill in a value in the textbox for the parameter name "userName",
The if statement ALWAYS evalues to "false" like I entered something in the textbox. I'm expecting that when i dont' enter a value in the text box
the request should be forwarded to the jsp "validateuser.jsp" but the opposite is happening.

Please help me understand why this is happening.



Regards,
Siphiwe Madi
[SCJP, __, __]
Next ... scwcd, scjwsd
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you check to see what is being returned? Is it a null? (Hint: no)
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - I printed it out and also printed it's length (which is zero), and as for the value itself its not printing anything.



Output
uName: '' , length=0

Can you let me know what exactly is the parameter value handled? Thanks in advance.

Regards,
Siphiwe Madi
[SCJP, __, __]
Next ... scwcd, scjwsd
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is that a null? No. What is it?
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am i correct in saying its an "empty" space???

If so, am i also correct in then assuming that request.getParameter() never returns a null, but either a value or an "empty" space???


Regards,
Siphiwe Madi
[SCJP, __, __]
Next ... scwcd, scjwsd
 
Devaka Cooray
Marshal
Pie
Posts: 4879
402
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It returns an empty string. Which equals to "".

The getParameter would return a null value, if the given parameter is not found in the request. In other words, if you completely remove that text field from the html (or jsp) page, this method would return a null value.

In practice, it is good to double check the parameter for both values

if(request.getParameter("userName")==null || request.getParameter("userName").equals(""))
 
Siphiwe Madi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Now I understand.

Regards,
Siphiwe Madi
[SCJP, __, __]
Next ... scwcd, scjwsd
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic