The discussion on this topic has been very helpfull to generate many possible
solutions.  After my first project I went with a template style HTML generator
also and have used it ever since.  I have a shared object that when instiated
reads from .html files to get a header, footer and navigation.  These are only
read the first time and then they are cached.  I pass the shared object a body
and title and I get back my finished page.  This works fanatasicly well because
the logic and HTML generation are separated and it is very effiecent and fast
since one object serves up the HTML it take up very little memory.  I still
however have to generate the HTML with println() and a print block here would
cut down on time.  I can do without the println but if we where making a wish
list it would be on mine and that is what got this whole thing started :o)  I
call the implementation I use with templates Object Orientated Page Design
(OOPD) because I "inherit" a template and "override" the body and title.  I see
that most of you also have come up with your own ways of accomplishing the same
thing.  The benefit of this method is that flat HTML files are used so HTML
coders have access to the code which was important for my company since they
make changes often to the pages and did not want all changes to have to go
through a Java programmer since it is such an ineffiecnt use of their time.  I
am including an example.

public class Template {
     private Template() { }
     private static Template instance = new CSSTemplate();
     public static Template getInstance() { return instance; }
     public String generateHTML (String body, String title) {
     sb.append("<HTML><HEAD><TITLE>" + title + "</TITLE></HEAD><BODY>");
     appendIncludeFile(sb, "header file path");
     appendIncludeFile(sb, "navigation file path");
     sb.append(body);
     appendIncludeFile(sb, "footer file path");
     sb.append("</BODY></HTML>");
     }
     public StringBuffer appendIncludeFile( StringBuffer myStringBuffer, String
myFileName ) {
          //String html = null;
          try {
               File f = new File( myFileName );
               FileReader in = new FileReader(f);
               int size = ( int ) f.length();
               char[] data = new char[size];
               int chars_read = 0;
               while ( chars_read < size ) {
                    chars_read += in.read(data, chars_read, size-chars_read);
                    myStringBuffer.append(data);
               }
               in.close();
          } catch (FileNotFoundException e) {
               System.out.println("File Not Found " + e);
          } catch (IOException e) {System.out.println (
               "Error reading input file " + e);
          }
          return myStringBuffer;
     }
}

I access this in any Servlet and pass it the parameters by:

//Get and Keep a reference to the shared CSSTemplate instance or the garabage
collector will collect it, keep it outside the Get and Post statements
CSSTemplate csst = CSSTemplate.getInstance();

out.println(csst.generateCart("The Body String", "The Title String");

Luther Andal

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to