| ||
[Back to Index] | ||
This document is a short but effective tutorial on how to write your own servlets for Tynamo™-CDC. Below are steps that describe the entire process, from downloading the tools, to accessing your servlet or servlets from an HTTP client. More details can be found in the Building a Web Server document. Table of Steps
0. Pre-SetupBefore beginning this sequence of steps, it is important to have all the necessary tools and utilities installed. Thus, this step "0" is here to provide instruction on this initial setup, if needed. There is one tool, besides the JDK and device SDK, that the build process uses. This is the Ant utility from The Jakarta Project. As of this writing, the latest version of this tool is Ant 1.6.1. Ant also requires the Jakarta Commons/Net library for deployment using FTP. Briefly, Ant needs the ANT_HOME and JAVA_HOME environment variables set correctly, and the bin subdirectory of the Ant installation added to the path. As well, the commons-net.jar (it may be named commons-net-1.1.0.jar or something similar) library needs to be placed into Ant's lib/ subdirectory. 1. Download Tynamo™Download the latest version of Tynamo™ from tynamo.qindesign.com. (This is the current homepage.) The easiest way to install the package is to create a directory, such as 'tynamo-cdc-1.0', and unzip the file into this location. [Top] 2. Write ServletsThis step demonstrates how to write a simple servlet. There is much literature on this subject, for example in the Documentation section of the Servlets home page, so a relatively small example is presented here. Please remember, however, that Tynamo™ complies with the Servlet 2.2 specification, and not 2.3 or above. import java.io.IOException; import java.util.Date; import java.util.Enumeration; import javax.servlet.ServletContext; import javax.servlet.ServletOutputStream; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Simple servlet that shows information about the server. * * @author Shawn Silverman */ public class ServerInfoServlet extends HttpServlet { /** * Process a GET request. * * @param req the request object * @param resp the response object */ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // Set up the response resp.setContentType("text/html"); ServletOutputStream out = resp.getOutputStream(); out.print("<html><head><title>Server Info Servlet</title>"); out.print("<style>body{font-family: Verdana,sans-serif}</style></head><body>"); out.print("<h1>Server Information</h1>"); out.print("<p>This servlet shows information about the server.</p>"); // Get the servlet context ServletContext context = getServletContext(); out.print("<table border=\"0\">"); // Server info out.print("<tr><td>Server is</td><td><strong>"); out.print(context.getServerInfo()); out.print("</strong></td></tr>"); // Server uptime out.print("<tr><td>Server uptime is</td><td>"); Long startTime = (Long)getServletContext().getAttribute( "com.qindesign.http.startTime"); if (startTime != null) { out.print("<strong>"); printElapsedTime(out, System.currentTimeMillis() - startTime.longValue()); out.print("</strong>"); } else { out.print("<unknown>"); } out.print("</td></tr>"); out.print("<tr><td bgcolor=\"Gray\" height=\"2\" colspan=\"2\"></td></tr>"); // Attribute names out.print("<tr valign=\"top\"><td>Context attributes:</td><td>"); Enumeration e = context.getAttributeNames(); if (e.hasMoreElements()) { out.print("<ul style=\"margin-left: 1.5em\">"); do { String name = (String)e.nextElement(); out.print("<li><code>"); out.print(name); out.print('='); out.print(String.valueOf(context.getAttribute(name))); out.print("</code></li>"); } while (e.hasMoreElements()); out.print("</ul>"); } else { out.print("<none>"); } out.print("</td></tr>"); out.print("</table>"); // Print a footer with the current time out.print("<hr /><address>This page was generated on <strong>"); out.print(new Date().toString()); out.print("</strong>.</address>"); out.print("</body></html>"); } /** * Prints the elapsed time in "d'd', HH:mm:ss" format. * * @param out the output stream * @param time the elapsed time, in ms */ private static void printElapsedTime(ServletOutputStream out, long time) throws IOException { // Days out.print(time / (1000*60*60*24)); time %= 1000*60*60*24; out.print("d, "); // Hours long l = time / (1000*60*60); if (l < 10) out.write('0'); out.print(l); time %= 1000*60*60; out.write(':'); // Minutes l = time / (1000*60); if (l < 10) out.write('0'); out.print(l); time %= 1000*60; out.write(':'); // Seconds l = time / 1000; if (l < 10) out.write('0'); out.print(time / 1000); } } Save this code as ServerInfoServlet.java in a directory named src underneath the Tynamo™ installation. [Top] 3. Update the Build Configuration
Open the build.properties file in the root of your
Tynamo™ installation. The first thing you need to do is change
the src.paths=/projects/myapp/src Note: if you use backslashes (\) as the file separator, then you must use them in pairs (eg. src.paths=C:\\Projects\\myapp\\src). This applies to all of the build properties.
Personally, I like to place a src/ directory underneath the
root of the Tynamo™ installation. If you have done this, then
src.paths=/tynamo-cdc-1.0/src
After this, add an Ant-style list of source files to the
src.files=ServerInfoServlet.java Finally, set the correct path to the platform API. For example, the classes for the EJC platform are located in a location that looks like: cdc.classes=/path/to/ejc-1.0.2/stubs/lib
Note that by default, a jar named myclasses.jar will be created
that contains all classes compiled in this step. To use a different name,
set the target.jar=myservlets.jar More
| ||
Tynamo™-CDC/1.0-RC1 Email: shawn@pobox.com © 2002-2004 Shawn Silverman |