Forums Register Login

About SvltRequest.getParameter(..) Can this be?

+Pie Number of slices to send: Send
In my .jsp, I have this:
<FORM ACTION="/servlet/myservlets.ServletX">
FromCity <input type="text" name="fCity">
<input type="submit" value="Submit" >

In ServletX I have this:

if (request.getParameter("fCity") != null) {
System.out.println("fCity = " + param.toString() + "end of line");

If I do not enter a value for parameter fCity, the console displays this:
fCity = end of line

If I change the name of the parameter passed from the jsp from "fCity" to "xxx", the console displays this:
fCity = nullend of line

My problem: I need to test if the user has entered a value in the jsp for "fCity". I've tried these:

if (request.getParameter("fCity") != null) {
System.out.println("fCity = " + param.toString() + "end of line");

if (request.getParameter("fCity") != "") {
System.out.println("fCity = " + param.toString() + "end of line");

if (request.getParameter("fCity") != "") {
System.out.println("fCity = " + param.toString() + "end of line");

In all 3 cases, the console shows "fCity = end of the line" It seems that the value of "fCity" is neither null
nor a blank nor is it nothing at all. What is it? How do I test its value? Have I lost my mind?
I'm running J2EE 1.4 with j2sdk1.4.2_07.

Get this- I am using Eclipse 3.0.1 with MyEclipse 3.8.4. In the MyEclipse Application Developer Guide, there is the MyEclipse J2EE Enterprise Application Projects - Quickstart. In that they have a sample EJB application called TraderX. In the web portion of it is EXACTLY the problem I describe above. I've run it in debug mode and seen it fail. My mouth fell open. Again, how does a person test for what request.getParameter("parmX") returns when parmX is a parameter but the user entered no value for it in the html or jsp??

THANK YOU in advance-
+Pie Number of slices to send: Send
Where are you assigning the value from getParameter to the "param" variable?
+Pie Number of slices to send: Send
Sorry about that. Assume that in the jsp that invokes the servlet, fCity is an existing name of a real parameter i.e. in my jsp I have:
<input type="text" name="fCity">
Here's my actual servlet code:
public void populate(HttpServletRequest theRequest) {
String param = null;
System.out.println("param = " + param); //prints out param = null
param = theRequest.getParameter("fCity");

//the following will print out fCity =
if (param != null)
System.out.println("fCity = " + param.toString());

//the following will also print out fCity =
if (theRequest.getParameter("fCity") != null)
System.out.println("fCity = " + theRequest.getParameter("fCity"));
+Pie Number of slices to send: Send
Just for kicks, try this right at the top of your doPost method:


and tell me what that prints.
+Pie Number of slices to send: Send
Thanks for helping me out-
It printed this-
fCity:

It dawned on me that I could test the field with String's length() method. This does work. After the line you suggested, I added this one:
System.out.println("fCity's length: " + request.getParameter("fCity").length());

It prints out this-
fCity's length: 0

It just seems peculiar to me that:
1) Java doesn't seem to have a 'garden variety' method to test whether a field contains an alphanumeric value or a primitive or a numeric value etc. (But maybe I'm just not aware of it?)
2) The javadoc does not tell you that ServletRequest.getParameter(..) returns null ONLY if the NAME of the parameter does not exist. If it does exist, I'm finding that a String whose length is 0 is returned.

Do you get the same result?

Thanks again, in advance
+Pie Number of slices to send: Send
It makes more sense when you look at what the browser is actually sending in it's request. The last line is the form data.

You can see this information with either a packet sniffer (I use tcpflow) or by installing FireFox and adding the liveHttpHeaders plugin.
http://livehttpheaders.mozdev.org/

All the browser sends is a delimited string with name/value pairs.
When you call getParameter, the servlet checks this string to see if it finds a matching name, if not, it returns null, if so, it returns exactly what is between the "=" and the "&". If there are multiple pairs with the same name then a string array is returned.

That's web programming in a nutshell.



POST /SimpleMVC/simple-mvc HTTP/1.1
Host: localhost:8080
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.3) Gecko/20040922
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:8080/SimpleMVC/simple-mvc
Cookie: JSESSIONID=D45A77BAE8FE419277B0398222AEB1D4
Content-Type: application/x-www-form-urlencoded
Content-Length: 93
first_name=Ben&last_name=Souther&email=ben%40souther.us&phone=444.444.4444&enter_button=Enter


[ April 06, 2005: Message edited by: Ben Souther ]
+Pie Number of slices to send: Send
 

Originally posted by Jeff Wachhorst:


if (request.getParameter("fCity") != null) {
System.out.println("fCity = " + param.toString() + "end of line");

if (request.getParameter("fCity") != "") {
System.out.println("fCity = " + param.toString() + "end of line");



Instead of the above 2 if conditions, you should use

if(request.getParameter("fCity")!=null && !request.getParameter("fCity").equals(""))
{
//if it comes here it cant be null and also cant be blank string.
}
[ April 06, 2005: Message edited by: Sripathi Krishnamurthy ]
Don't count your weasels before they've popped. And now for a mulberry bush related tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 816 times.
Similar Threads
If anybody can answer these two questions?
request.getParameter() returns a null value in the servlet
Session Value Persistence
Doubt in Form Bean population
Session code doesn't get executed in jsp
Request parameter is missing
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:39:16.