tags:

views:

50

answers:

3

I want to be able to generate html pages where some parts of the html should depend on some parameters.
My idea is that I could have a template like OneMailTemplate.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>MailTemplate</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Bla bla bla</p>
    <someComponent>
    <p>Bla bla</p>
  </body>
</html>

and then get some method to replace the tag

<someComponent>

with some generated html code. Is there a way to do this in java without the use of StringTokenizer?
I am also open to suggestions about how I can do this in another way.

A: 

It's kind of overkill to use a Java program to do this...

But the proper way in Java would be to use DOM functions to find the tag, create your replacement subtree, and output the result.

If your replacement is static, you should use XSLT instead. If the replacement is dynamic, consider using JSP instead of a custom preprocessor - JSP is designed for this case.

Borealid
Can I use JSP if I as I mentioned above want to get a html file which I can send via javaMail. I need to get the source from the generated html page and send it via mail without the user seeing this page.
AnAmuser
You can save the generated HTML page, or even send it automatically.
Borealid
A: 

As much as I like XSLT, I think it may be a bit heavy for this purpose. Freemarker is made for this kind of thing. It can take an HTML template with parameters like ${name} in the html and create your output without being dependent upon a jsp.

Robert Diana
A: 

I use Velocity for my mail templates.

Maurice Perry