Mixer (source, doc, jar, everything) is a helper-class that is easy to use for Servlet programmers that enable a complete separation of Servlet- and HTML-code. Licence: GNU GPL.
Example | Template | Code | Result |
Non repeated | HTML | Java | HTML |
Repeated | HTML | Java | HTML |
Combined | HTML | Java | HTML |
Multiple repeated | HTML | Java | HTML |
Adding HTML |
HTML1 HTML2 |
Java | HTML |
Mixer does not use any new language and is very efficient. The template HTML-document is valid HTML that has two types of markers. Context-markers are markers, in the form of a HTML-comment, that are placed around parts of HTML-code that should be repeated:
<!--===xxx===-->
<tr>
<td>
===name===
</td>
<td>
<a href="mailto:===email===">===email===</a>
</td>
</tr>
<!--===xxx===-->
Variable-markers are markers that should be substituted for values:
<p>
My name is ===name=== and my email is <a href="mailto:===email===">===email===</a>.
</p>
For efficiency use Mixer in a Servlet like this:
Define a class-variable that holds a template HTML-document:
private static String htmlTemplate = null;
In method init, load content from a template HTML-file into this class-variable:
if (htmlTemplate == null) {
htmlTemplate = Mixer.getContent(new File(getServletContext().getRealPath("x.html")));
}In metod doGet and/or doPost:
Mixer m = new Mixer(htmlTemplate);
m.add("===email===", "pierre@dsv.su.se");
...
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println(m.getMix());
Accordingly, costly file operations only take place during initialization of the Servlet.
Mixer also adds functionality to mix different parts of different HTML-documents. This is done by preloading any number of template HTML-documents in the init-method and then selecting different parts of the template HTML-documents to build a new HTML-document. The resulting document can in turn be used as a template for further processing.
Please report bugs here.
Updated 2004-06-12 by Pierre A. I. Wijkman.