| ||
[Back to Index] | ||
This document is a short but effective tutorial on how to write your own servlets for the Tynamo™-TINI server. 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 are two tools, besides the JDK and TINI SDK, that the build process uses. These are Ant and TiniAnt. As of this writing, the latest versions of these tools are Ant 1.6.5 and TiniAnt 1.2.0. 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 TiniAnt.jar and commons-net.jar (it may be named commons-net-1.4.1.jar or something similar) libraries need to be placed into Ant's lib/ subdirectory. One additional point is that the Tynamo™ distribution, by default, includes some example configuration files. These can be safely modified to conform to this tutorial. [Top] 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-tini-1.0.4, 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 the Tynamo™ server complies with the Servlet 2.2 specification, and not 2.3 or above. import com.dalsemi.onewire.*; import com.dalsemi.onewire.adapter.DSPortAdapter; import com.dalsemi.onewire.container.OneWireContainer; import java.io.IOException; import java.util.Enumeration; 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 lists all 1-Wire devices found on all adapters. * * @author Shawn Silverman */ public class OneWireListServlet extends HttpServlet { /** * Process a GET request. */ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); ServletOutputStream out = resp.getOutputStream(); out.print("<html><head><title>1-Wire List Servlet</title></head><body>"); out.print("<h1>1-Wire List</h1>"); out.print("<p>This servlet lists all 1-Wire devices on all the adapters.</p>"); // Iterate through the adapters Enumeration adapters = OneWireAccessProvider.enumerateAllAdapters(); while (adapters.hasMoreElements()) { DSPortAdapter a = (DSPortAdapter)adapters.nextElement(); out.print("<h2>Adapter: "); out.print(a.getAdapterName()); out.print("</h2>"); // Print all 1-Wire containers on this adapter try { OneWireContainer c = a.getFirstDeviceContainer(); if (c != null) { out.print("<p>1-Wire devices found:</p><ul>"); do { out.print("<li>"); out.print(c.getAddressAsString()); out.write(' '); out.print(c.getName()); out.print("</li>"); c = a.getNextDeviceContainer(); } while (c != null); out.print("</ul>"); } else { out.print("No 1-Wire devices found."); } } catch (OneWireException ex) { out.print("<strong>Error while accessing adapter: "); out.print(ex.toString()); out.print("</strong>"); } } out.print("</body></html>"); } } Save this code as OneWireListServlet.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 tini.path=/Java/tini1.17 Note: if you use backslashes (\) as the file separator, then you must use them in pairs (eg. tini.path=C:\\Java\\tini1.17). This applies to all of the build properties.
Next, change src.paths=/projects/myapp/src
Personally, I like to place a src/ directory underneath the
root of the Tynamo™ installation. If you have done this, then
src.paths=/tynamo-tini-1.0.4/src Note also that the path can be relative if you choose.
After this, add an Ant-style list of source files to the
src.files=OneWireListServlet.java
Finally, if you are using TINI firmware 1.10 or later, then add your
full servlet classnames to reflect.classes=OneWireListServlet More
| ||
Tynamo™-TINI/1.0.4 Email: shawn@pobox.com © 2002-2006 Shawn Silverman |