1

I have noticed some problem when performing server side servlet validation given the form, i need to validate the firstname text field

     <form action="Test" method="POST">
        <input type="text" name="firstname" />
        <input type="submit" value="submit" />
    </form>

Servlet validation code that does not work for me. it always see firstname with length=0 not null

if(request.getParameter("firstname")==null)
    {
        out.println("Error");
    }`

but after modifying the form adding enctype="multipart/form-data" to be

<form action="Test" method="POST" enctype="multipart/form-data" >
        <input type="text" name="firstname" />
        <input type="submit" value="submit" />
    </form>

the validation code works ,,,

the question here is what is the function of enctype="multipart/form-data" ? also why request.getParameter("firstname") doesnot return null for empty field ? it returns empty string

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ahmed Abobakr
  • 1,618
  • 18
  • 26
  • 1
    Please check http://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean – Ketan Bhavsar May 06 '13 at 12:11
  • 1
    i am confused because all tutorial i reed do the validation with the above way with out enctype="multipart/form-data" why it does not work ? – Ahmed Abobakr May 06 '13 at 12:49
  • If you want to make validation so add onsubmit="return functionName();" in form tag like
    In script
    – Ruju May 06 '13 at 13:25
  • 2
    http://stackoverflow.com/questions/3136232/are-empty-fields-in-form-of-jsp-null-or/3136455#3136455 http://stackoverflow.com/questions/15028349/missing-elements-in-http-request-null-or-empty/15028538#15028538 – BalusC May 06 '13 at 13:26
  • @Rujvendra yes your way is true but it's client side validation using javaScrip but i am talking about server side validation to avoid the case that the javaScript is disabled on client browser – Ahmed Abobakr May 06 '13 at 18:10
  • It is not the good idea to make server side validation,client side is good as per performance issue.If you want to make server side validation then try to use any framework like struts or spring so they will handle it automatically with less performance issue. – Ruju May 07 '13 at 04:49
  • @Rujvendra, client side is good but i am handling the case if the java script is disabled so to be in safe side server side validation is required – Ahmed Abobakr May 09 '13 at 01:12

1 Answers1

2
if(request.getParameter("firstname")==null)

This checks if the reference of the String points to null, which is not what you want to do I guess. If you want to check for an empty String, do:

if(request.getParameter("firstname")==null 
     || request.getParameter("firstname").isEmpty())

When using enctype="multipart/form-data", all parameters are encoded in the request body. That means that request.getParameter(...) will return null for all posted parameters then.

Uooo
  • 6,204
  • 8
  • 36
  • 63
  • checking request.getParameter("firstname").isEmpty() will give Null Pointer Exception with the second form which has enctype="multipart/form-data" – Ahmed Abobakr May 06 '13 at 12:42
  • @ahmedabobakr No it will not. It will get only evaluated if `request.getParameter("firstname")==null` evaluates to `false`. – Uooo May 06 '13 at 12:44
  • why request.getParameter("firstname") does not return null for the empty field ?? it returns empty string – Ahmed Abobakr May 06 '13 at 13:20
  • Because the input field is been **submitted** with an empty value. If the input field was not submitted at all, it returns `null`. This also totally explains the behavior when changing the form encoding in such way that `getParameter()` don't work anymore (try to fill in the input value, you'll see that it still returns `null`!) – BalusC May 06 '13 at 13:27
  • @w4rumy: as to the last paragraph, you're confusing/mixing the POST method with the encoding. Even without the encoding type explicitly specified, the parameters are still in the request body. I do by the way also not understand why you boldfaced the word "reference". This is of no importance here. – BalusC May 06 '13 at 13:28
  • @BalusC yes u are right still returns null, so i will write the fom without encoding and validate it using request.getParameter(fieldname).isEmpty and hope that request.getParameter(fieldname) will not return null – Ahmed Abobakr May 06 '13 at 18:18
  • Uh, "hope"? Just do the both checks exactly as answered by w4rumy? `if (name == null || name.isEmpty())` – BalusC May 06 '13 at 18:19