14.2 Getting Started

Java Servlet Programming, 2nd Edition > 14. The Tea Framework > 14.2 Getting Started

 
< BACKCONTINUE >

14.2 Getting Started

To get familiar with Tea, let's start simple by looking at a few standalone templates. These templates aren't going to take advantage of any supporting "application" Java classes and thus won't be able to do very much. Here is our first template:

<% template SimplePage() %> This is a simple page that does nothing special.

This template simply outputs "This is a simple page that does nothing special." to everyone who accesses it. Templates are composed of code and text regions. Code regions are delimited by <% and %> (no other special delimiters are required). Text outside a code region is output by the template as is, which is why this template prints the simple statement.

To run the template, you must first save it to a file named SimplePage.tea. Similar to Java classes, the name of the file must match the name of the template as declared by the constructor (case sensitive), and the file must end with a .tea suffix. The location in which to save the template is configurable; we recommend a directory under WEB-INF such as WEB-INF/templates.

The next step to running the template is installing Tea itself. Download the distribution from http://opensource.go.com and follow their instructions for install: place the TeaServlet.jar file from the distribution into the web server's classpath. Then edit your web application's web.xml deployment descriptor to register the TeaServlet under the name tea with an init parameter specifying a location for configuration information. Also set up a prefix mapping rule that /tea/* invokes the tea servlet. An example web.xml addition is shown in Example 14-1.

Example 14-1. Installing TeaServlet
    <!-- ... -->     <servlet>         <servlet-name>             tea         </servlet-name>         <servlet-class>             com.go.teaservlet.TeaServlet         </servlet-class>         <init-param>             <param-name>                 properties.file             </param-name>             <param-value>                 <!-- Edit this to be an absolute path to your prop file -->                 /tomcat/webapps/teatime/WEB-INF/TeaServlet.properties             </param-value>         </init-param>     </servlet>     <servlet-mapping>         <servlet-name>             tea         </servlet-name>         <url-pattern>             /tea/*         </url-pattern>     </servlet-mapping>     <!-- ... -->

The only thing you must customize is the path to the TeaServlet.properties file. It should be an absolute path. The TeaServlet.properties file configures the behavior of the TeaServlet. It can be quite long, which is why the web.xml file points to an external file instead of including the configuration information directly. We'll postpone a full discussion of the properties file for now and use the most simple TeaServlet.properties file possible:

# TeaServlet.properties # # Specify the location of Tea templates template.path = /tomcat/webapps/teatime/WEB-INF/templates

Make sure to save the properties file at the location stated in the web.xml file. The final step is to restart your server so it reads the new web.xml file (with some servers this isn't necessary).

Once TeaServlet has been properly installed, the SimplePage can be accessed at a location such as http://localhost:8080/teatime/tea/SimplePage. The /teatime portion of the path specifies the web application being accessed. The following /tea portion matches the prefix mapping given in the teatime web.xml, causing the TeaServlet to be invoked to handle the request. Templates can be placed in subdirectories under template.path, in which case they're invoked using a path that includes the subdirectory name such as /teatime/tea/subdir/SimplePage.


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