jQuery in Action, 2nd edition*
The moose likes Servlets and the fly likes sending information from a servlet to form Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "sending information from a servlet to form" Watch "sending information from a servlet to form" New topic

sending information from a servlet to form

Andrew Boris

Joined: Mar 20, 2009
Posts: 2
Okay, this probably seems a little backwards. I know tons of people have posted about, and information is readily available about, how to read data from forms onto your servlet.

But, I have a different problem. I have a servlet that I'm running. Elsewhere on the web, there is a website that has a little box where you can enter a sentence for parsing. It parses the sentence, and then returns the parsed sentence. I need to parse a sentence, and that website is perfect for the job. The problem is, the servlet for that webapp is written in python.

In java, it'd be relatively easy. The text field on the form for this other website is named "theText". So, if it were in java, I'd simply get my servlet, open a urlConnection with the website's address and append to that address: "?theText=whateverSentenceIWantToParse" to it, and send it on it's way. Then, I'd parse what it sent back for the sentence I wanted.

But, I can't seem to do that with this python-based website. When I try to do that, it expects the field "theText" to be in a dictionary, not as a single parameter floating out there in space. So, how do I fill out that form information, or, how do I format the URL?

Hope the question is clear, and thanks for the help
Frank Eichfelder
Ranch Hand

Joined: Aug 25, 2003
Posts: 33
If I were you, I would check how the HTML-page of the application is built and what parameters etc. are sent (by the way, did you URL-encode the URL you are calling in your application?).
In addition, you can check how the HTTP-communication is structured (e. g. with the LiveHTTPHeaders-Plugin in Firefox) when you call the target application in your web browser. This should give you insights about what parameters are sent and how they are transmitted to the target system.
Afterwards you should try to rebuild this structure manually with your HttpUrlConnection.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42276
I'm not sure what you mean by "dictionary" - HTTP has no such notion, it's all plain strings (possibly structured internally).

I second LiveHttpHeaders as a very useful tool. Also, instead of using the UrlConnection class, I'd go with the Apache Commons HttpClient library.

Ping & DNS - my free Android networking tools app
Andrew Boris

Joined: Mar 20, 2009
Posts: 2
Thanks for the input. I downloaded LiveHTTPHeaders, when to the website, entered the text "Hello, joe" into the form, and hit the submit button. LiveHTTPHeaders tracked the request as:

(host is the host name of the website)

POST /classifier/text/text/ HTTP/1.1
HOST: host
UserAgent: Mozilla/5.0 (Mac...)
Accept: text/html,application/xhtml+xml,application/sml;q=0.9,*/*;q=0.8
Accept-Language: en-us;en=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Referer: host/classifier/text
Content-Type: application/x-www-form-urlencoded
Content-Length: 94

The form that I entered the sentence into looked like:
<form method = 'post' action = '/classifier/text/text/>
<textarea name = "text"></textarea><input type= "submit" value = "Classify">

then it goes on with some optional fields that I don't care about before closing the form.

So, the task seems simple enough. I just want to POST data to the website 'host/classifier/text/text/', giving the parameter "text" the value of the sentence I want it to parse. But, I'm not sure how to do this. I looked at HttpURLConnection, but I don't see any methods in it for actually sending data.

Any advice from here?

I agree. Here's the link: http://aspose.com/file-tools
subject: sending information from a servlet to form