Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sending information from a servlet to form

 
Andrew Boris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Andrew Boris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
{
host/classifier/text/text/

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
text=hello%2C+joe&useDefaults=true&unigrams=0&bigrams=0&stems=0&emoWords=0&slang=0&emoticons=0
}

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?
Thanks!





 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic