2

I want to create an object which represents an html document which is suppose to be submitted to the web server through programming. I have to set various parameters also in that document.

Scenario : I have read a web page having username and password fields in it. Now i want to set username and password through desktop application into that html page. After that i want to submit that page to the webserver.

Now the confusion i am facing over here is how to construct an object representing an HTML document through this response which i got from the website.

ATR
  • 2,160
  • 4
  • 22
  • 43
  • Are you talking about something like HttpClient: http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/HttpClient.html – Bhesh Gurung Dec 20 '11 at 22:59
  • No. I have raw html in string variable. I want to construct an object of "javax.swing.text.html.HTMLDocument". So that i can set the request parameters manually and can post the response of the page to the webserver. – ATR Dec 20 '11 at 23:04
  • or are you asking about parsing the "string of HTML" into an "HTML Document object"? → http://stackoverflow.com/questions/3152138/what-are-the-pros-and-cons-of-the-leading-java-html-parsers – BRPocock Dec 20 '11 at 23:05
  • @BRPocock: No the link is not about what i want exactly. – ATR Dec 20 '11 at 23:10
  • Why are you trying to create a document when you only need to send a post request with parameters, do you display that in some swing component too? – Bhesh Gurung Dec 20 '11 at 23:15
  • No. I am not displaying it in any swing component but i have to set the parameters in the html and have to post it. – ATR Dec 20 '11 at 23:21

1 Answers1

1

You should be able to use a tool called 'Velocity' to do this. If you scroll down to the 'Tutorial' section of http://velocity.apache.org/engine/devel/webapps.html you can get an example of how it works.

You should read up on it a little as I haven't used it in some time.

First, you should create an HTML template using the format shown through the examples.

Then, the gist of what needs to be done is the following (after you have created a template):

VelocityEngine ve = new VelocityEngine();
Template template = ve.getTemplate("name_of_template");
VelocityContext context = new VelocityContext();
StringWriter writer = new StringWriter();

context.put("username", yourUsernameString);
context.put("password", yourPasswordString);

template.merge(context, writer);

// 'writer' now holds the output!
// try using 'writer.toString()' to get a string version.

You should be left with the 'writer' object holding your HTML with your variables now inside of it!

Hope this helps.

Lemmings19
  • 1,383
  • 3
  • 21
  • 34