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)
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!

8 Years
Discussion Span
Last Post by pokahantos
Featured 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)


here is my form:

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


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!

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)
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"/>
    <title> </title>

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

in the run time, i have test the following values:
IN test was printed
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
output: IN test


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!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.