Hello all,

I have a very stupid problem... unfortunately i can not find any way to solve it..

I am using JDeveloper to design & implement JSP page..

I have an input field "text field" where the user enter his name..

I wanna check if the text field is empty or not when the user click the submit button..

I am using this condition:
if(request.getParameter("UserName") !=null)
{
.......
}
else
out.println("Enter your name");

the problem is that it always enter the IF even if the text field is empty. i have tried saving the value into a variable I defined but i got the same output..

how can i fix this!

Recommended Answers

All 13 Replies

Can you show me the code of the page where your text field is located.
Have you set the default value for your text field to a blank string i. e. ("").
In that case the check for (!= null ) would fail even if your text field was blank. You would have to check for blank string in that case like this :-

if((! request.getParameter("UserName").equals(""))) {

I have not set any default value for the text field...

i have tried your statement

if (! request.getParameter("UserName").equals("")

but it got me this error ( this error also shown when i changed my condition to check the length of the text)

java.lang.NullPointerException

here is my form:

<form method ="get" action="TEST.jsp">
        <strong>
            <font size="3" color="#940000">*</font>
            <font size="3" color="#000084">
                <u>User Name</u>
            </font>
        </strong>
        
        <input type="text" name="UsrName" maxlength="15" size="15"/>
        
        <p></p>
        <input type="submit" name="SUBMIT" value="Submit" style="background-color:rgb(127,157,193); font-size:xsmall;"/>

</form>

Thats strange the NullPointerException would be thrown only in case request.getParameter("UserName") returned NULL, but in your first post you had said request.getParameter("UserName") != null evaluated to true for all cases.

Try the following condition:-

if ((request.getParameter("UserName")!=null ) && (! request.getParameter("UserName").equals(""))) {

It should accommodate both conditions when you get a blank as well as when you get a null.

request.getParameter("UserName") always return null even if there is an input text..

and it always enter the condition as it is True..

i have tried your code.. and the same things happened.. i entered my name, it got inside the if stm as it is true..
when i tried to print the text field value it print "null"

??

There is your problem

<input type="text" name="UsrName" maxlength="15" size="15"/>

Check the Spelling of the name given to the field, you have give "UsrName" it should be "UserName".

But it should not enter the if block as the condition ( (request.getParameter("UserName")!=null ) )still evaluates to false.

eventhought.... same thing!

i am really confused!

eventhought.... same thing!

i am really confused!

Have you tried by changing the name of the field from "UsrName" to "UserName". Next show us your servlet code and why you feel that the code inside the "if" block is getting executed.
Note when pasting your code use for java code and [code=html] for HTML, it provides syntax highlighting.[code=java] for java code and for HTML, it provides syntax highlighting.[code=html] for HTML, it provides syntax highlighting.

yes, i changed the name and tried it...nothing changed!

this is the whole page of the untitled.jsp page

<%@ page contentType="text/html;charset=windows-1252"%>


<% if (request.getParameter("UserName")!=null)
{
    out.println("IN");
    out.println(request.getParameter("UserName"));
           
}
    %>
    
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
    <title> </title>
  </head>
  <body>

    
  <form method ="get" action="untitled1.jsp">
    <p>
    <strong>
                                    
    <font size="3" color="#940000">*</font>
                                    
    <font size="3" color="#000084">
           <u>User Name</u>
    </font>
    </strong><input type="text" name="UserName" maxlength="15" size="15"/>
    </p>
    <p>
    <strong>
                                    
    <font size="3" color="#940000"> *</font>
                                    
    <font size="3" color="#000084">
           <u> Employee Number</u>
    </font>
    </strong><input type="text" name="EmpNum" maxlength="10" size="10"/>
    </p>
     <p>
        &nbsp;
     </p>
      
    <input type="submit" name="SUBMIT" value="Submit" style="background-color:rgb(127,157,193); font-size:xsmall;"/>
    </form>
    
    </body>
</html>

in the run time, i have test the following values:
1.username=test
IN test was printed
2.username=blank
IN was printed

Ok.. What was the output when you replaced

if (request.getParameter("UserName")!=null)

with the following condition:-

if ((request.getParameter("UserName")!=null) && !(request.getParameter("UserName").equals(""))) {

It works fine
username:test
output: IN test

username:blank
output:blank

but i dont understand why do we need to check if it is not equal to "" !!
can not i use one of them only?!

In Java null and "" are not the same thing.

null is the actual universal NULL, i. e. nothing.
On the other hand "" is a blank String object.

If an object is "null" calling any method on it would give you a NullPointerException, hence we need to first check for null and only after that we check if the String is "blank".

"null" I assume is returned when the parameter is not present at all in the request (which is not your case), but you had mentioned encountering a NullPointerException earlier, thats why I recommended both the conditions for your case.

well if the problem has been solved it would be appreciated if you mark this thread as solved.

Thanks alot stephen :icon_cheesygrin:

Thanks for your help...it is so clear now
I really appreciated that!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.