Ankur Trapasiya
Ankur Trapasiya

Reputation: 2200

How to construct an object representing HTML page in java from the existing string data?

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.

Upvotes: 2

Views: 734

Answers (1)

Lemmings19
Lemmings19

Reputation: 1481

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.

Upvotes: 1

Related Questions