File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes request.getParameter == null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "request.getParameter == null" Watch "request.getParameter == null" New topic

request.getParameter == null

Jeff Dube
Ranch Hand

Joined: Jun 14, 2001
Posts: 44
My form is validated by a jsp and i try to get the parameter of a field but i have a problem.
Here it is.
if (request.getParameter("BegDay") == null)
is not a valid way to check if the field is empty caus i always get outside the loop, even if the field is empty.
on the other hand:
if (request.getParameter("BegDay") == "")
is neither good
What is the way to check if the field is empty???
These fields are text field and my syntax is okay but i need to evaluate the content of the field. I cannot use javascript because this field is not an essentiel one so the user is free to enter data or not.
Thank you
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13035

if (request.getParameter("BegDay") == "")
Tch tch! You know you should never compare Strings with ==
Try this
String tmp = request.getParameter("BegDay");
if( tmp == null | | tmp.trim().length() == 0 ){
// no entry or just spaces
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Nice to see someone else using trim()
I agree. Here's the link:
subject: request.getParameter == null
It's not a secret anymore!