16.1 Page Components as Objects

Java Servlet Programming, 2nd Edition > 16. Element Construction Set > 16.1 Page Components as Objects

 
< BACKCONTINUE >

16.1 Page Components as Objects

ECS includes classes for all HTML 4.0 constructs. Example 16-1 shows how a simple HTML page can be constructed with ECS.

Example 16-1. A Page as a Set of Objects
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import org.apache.ecs.*; import org.apache.ecs.html.*; public class ECSHello extends HttpServlet {   public void doGet(HttpServletRequest req, HttpServletResponse res)                                throws ServletException, IOException {     res.setContentType("text/html");     PrintWriter out = res.getWriter();     Document doc = new Document();     doc.appendTitle("Testing ECS");     doc.appendBody(new Big("Hello!"))        .appendBody(new P())        .appendBody("The current time is " + new Date());     doc.output(out);   } }

Notice how all the HTML tags have been replaced with objects. This servlet creates a new Document object that represents the web page it will return. Then, it adds a "Hello World" title to the page and appends to the page body a big "Hello!" plus a paragraph break and a printing of the current time. Finally, the servlet outputs the page to its PrintWriter. That's how object-oriented HTML generation works: get a Document object, add component objects to it, and send it to the client.

To execute this servlet, you need to install ECS by putting the ECS JAR file into your server's classpath (or into the WEB-INF/lib directory of your web application). For ECS 1.3.3 the JAR file is named ecs-1.3.3.jar. You may have to restart your server for it to find the new JAR. With ECS installed you can invoke the servlet as usual, and the servlet will generate output like this:

<html><head><title>Testing ECS</title></head><body><big>Hello!</big><p>The current time is Thu Aug 03 17:58:37 PDT 2000</body></html>

By default all the ECS output appears on one line with no indentation or carriage returns. This saves bandwidth when communicating with a browser client. If you want more human-readable content, edit the ecs.properties file that comes with the distribution and change the pretty_print value to true. Then the tricky part: make sure your ecs.properties file is found in the server's classpath before the ECS JAR file, so that your edited file overrides the ecs.properties file included in the JAR. The ecs.properties file is searched for as org/apache/ecs/ecs.properties so the file needs to be placed not directly in the classpath but in an org/apache/ecs subdirectory of a directory within the classpath (like WEB-INF/classes/org/apache/ecs/ecs.properties).

The servlet imports two ECS packages: org.apache.ecs for the core ECS classes and org.apache.ecs.html for the HTML-specific classes. (There are other packages for XML, WML, and RTF.)[1] The org.apache.ecs.html package contains nearly a hundred classes representing all the HTML 4.0 components. Most of the HTML classes are named to match their HTML tag names: Big, Small, P, Table, TR, TD, TH, H1, H2, H3, Frame, A, Head, Body, and so on. Each HTML class has methods for configuring the component. For example, the TD class has a setBackground(String url) method that sets the background of that table cell. The Body class has a similar method as well, to set the background of the entire page. No other ECS component has a setBackground( ) method, because no other component can set its background, and that allows ECS to ensure that documents programatically created are always well-formed HTML.

[1] If you're interested in programatically creating XML from Java, you'll be better off using JDOM (http://jdom.org) because JDOM has better integration with XML technologies.

To append the body elements, the servlet makes use of method chaining, in which several methods are invoked on the same object. You see this a lot in ECS. For example, to construct a table:

Table tab = new Table()             .setCellPadding(0)             .setCellSpacing(0);

The whitespace is irrelevant. The previous code is equivalent to:

Table tab = new Table().setCellPadding(0).setCellSpacing(0);

This chaining is possible because each set and append method returns a reference to the object on which it was invoked that reference is used to invoke the next method. This trick comes in handy when using ECS.


Last updated on 3/20/2003
Java Servlet Programming, 2nd Edition, © 2001 O'Reilly

< BACKCONTINUE >


Java servlet programming
Java Servlet Programming (Java Series)
ISBN: 0596000405
EAN: 2147483647
Year: 2000
Pages: 223

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net