aspose file tools*
The moose likes JSP and the fly likes can we set default values for variables?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "can we set default values for variables??" Watch "can we set default values for variables??" New topic
Author

can we set default values for variables??

sumana ar
Greenhorn

Joined: Aug 06, 2003
Posts: 28
hi,
i have a HTML form which has some text fields in it..the user can enter some or all of the fields and submit it..depending on the values entered..a search has to be made from the database...i captured the non-entered values in another jsp page using
String name = getParameter("<field_name>")
i'm getting an empty string into name.
now need to use the variable name in the SQL statement in the JSP page as follows...
"SELECT * FROM <table_name> WHERE SHEET_NAME = '" +name +"' or '"+name+"' IS NULL AND so on...";
but as name is empty... the condition for checking null is failing...
can we set default vaues for the variables used so that when nothing is selected, the variable has a null value...
can someone help me solve the problem...plz treat this as urgent
thanks
--sumana
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Ok:
1o) Are you expecting an empty value in your name value?
String name = request.getParameter("name")
if you use this piece of code and your form with the name input field was empty, you should be getting null.
maybe I didn't understand your problem.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
In your jsp page use something like this
if(name.trim()=="") name=null;
similarly check for other parameters too.The fire the SQL query.
sumana ar
Greenhorn

Joined: Aug 06, 2003
Posts: 28
i think i'm getting an empty string bcz when i use the form action as get..the url is appended something like this...
URL?name=&id=20

so now do u think i'm getting a null into the name variable..
--sumana
Faisal Khan
Ranch Hand

Joined: Jun 29, 2003
Posts: 285
Sumana,
I believe what may be happening is that in your HTML form you have something like:
<input type="text" name="name" value="">
Therefore what will happen is when you submit the form the value for name will be set to an emptry String as specified by the form. What you can try is to remove the value="" from the form and then if nothing is entered name will be NULL.
The query will still not work as in your SQL you will end uo with:
OR NULL IS NULL and I am not sure if that is valid to do.
hth


The secret to creativity is knowing how to hide your sources.
Idly Vada
Ranch Hand

Joined: Sep 02, 2003
Posts: 135
Originally posted by sumana ar:
i think i'm getting an empty string bcz when i use the form action as get..the url is appended something like this...
URL?name=&id=20
so now do u think i'm getting a null into the name variable..

Even if you use POST also same thing will happen. I think therez no way send null value from form to the jsp. Even if you send NULL, it will be sent as the string null rather than null value.
sumana ar
Greenhorn

Joined: Aug 06, 2003
Posts: 28
thanks for all those sugessions...
actually i havn't given any value attribute to the text field and so an empty string is passed when nothing is selected.
the problem is solved and anyways thanks for the help
sumana
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can we set default values for variables??