954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with text field.. help

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!

pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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(""))) {
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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>
pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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"

??

pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

eventhought.... same thing!

i am really confused!

pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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[code=java] for java code and [code=html] for HTML, it provides syntax highlighting.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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

pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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(""))) {
stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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

pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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.

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

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

stephen84s
Nearly a Posting Virtuoso
1,443 posts since Jul 2007
Reputation Points: 668
Solved Threads: 154
 

Thanks alot stephen :icon_cheesygrin:

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

pokahantos
Newbie Poster
22 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You