request.getParameter problem?

In a servlet, I use request.getParameter to get form data. It works fine and get all data user entered, and store data into an array.

Then I want to validate the data, and to get errorFlag if certain data missing.

In the data array generated with request.getParameter, however, data, if being seperated with a space, such as "15 5th street", will only have the part before the space left. Only "15" left, and "5th street" is gone, so to say.

What happens?

I appreciate if any one can give a clue!

[536 byte] By [guangzhong] at [2007-9-26 1:54:00]
# 1

I have dealt with this problem in the past. We were missing parts of strings in a servlet and I think your problem is the same.It is easily solved but not exactly intuitive.

- Example -

You can think of parameters as being part of a larger URL string that is passed to the servlet. For instance, if I do a search on mapquest for

140 S 15th st

Denver, CO 88866

Mapquest must put together an string without spaces that can be effectively read by the servlet or cgi script.

If you notice in the URL:

http://www.mapquest.com/cgi-bin/ia_find?link=btwn%2Ftwn-map_results&random=565&event=find_search&SNVData=&address=140+20S+15th+st&city=Denver&State=CO&Zip=88822&Find+Map.x=45&Find+Map.y=4

parameters do not include spaces, but instead include '+' where spaces would exist.

To the Servlet different parameters are seperated <b> not </b> by spaces, but by another character '+'.

why is this?

In order to pass parameters that can be uniformly interpreted by a server side process, the origonal web engineers developed a way to put any combination of parameters into a string that could not be mis-intrepted by the web server. If all parameters were seperated by spaces, there would be no way to tell where one parameter ended and another had begin. Thus, URL encoding was the standard used to transmit complex http data to cgi scripts.

How to solve this?

1. Client Side( Applet or Application)

encode the url before transmission to the servlet

String encodedURL = URLEncoder.encode("http://www.yourwebhost.com/yourContext?street=15 15th street&city=San Francisco");

System.out.println(encodedURL);

// Should show:

http://www.yourwebhost.com/yourContext?street=15+15th+street&city=San+Francisco

2. On the server side.

The servlet should return the correct string for the parameter. If not...

String param = request.getParamter(street);

String decoded = URLDecoder.decode(param);

At any rate, it sounds like URL Encoding and decoding is your problem. it was on our project where we had similar missing sections of the strings. If this doesn't work. Post source code and We'll take a stab at that.

Best of luck...

tmonteit at 2007-6-29 3:05:54 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 2

There are three ways you can submit form data from one page to another.

The first two are via form submissions with method="POST" or "GET".

POST -- this method causes the fill-out form contents to be sent to the server in a data body rather than as part of the URL. This method will automatically convert spaces to either '%20' or '+' depending on your browser.

GET -- this is the default method and causes the fill-out form contents to be appended to the URL as if they were a normal query. This method again will automatically convert spaces to either '%20' or '+' depending on your browser.

The other way is to attempt to append a query to the end of a html link i.e. http://www.domain.com/page.cgi?key1=value1&key2=value2. This method will not do any conversion and will chop the line as soon as it reads a space in the URL.

If you are still having problems, please post the details on how the form is being submitted and how the values are being read, this may offer a littel more insight to the problem.

shann0nw at 2007-6-29 3:05:54 > top of Java-index,Archived Forums,New To Java Technology Archive...
# 3
very simple solution is :u use textarea of one row and preferred size in place of textfield in the form ur using.hope this helps u,gudluck
vvenkatramana at 2007-6-29 3:05:54 > top of Java-index,Archived Forums,New To Java Technology Archive...