This is a short tutorial1 on how to get you started with Servlet development with Eclipse IDE to develop XML-RPC services that can be a counterpart for your XWT application. It will guide you through installing the necessary tools and building your first XMLRPC Servlet. There is a separate tutorial available here that focuses on developing the client-side XWT application.
The first step is to install the development environment. In this example our development environment is based on Eclipse IDE and we use Tomcat servlet container. These tools integrate well and are freely available from their vendors.
TOMCAT_HOME/conf/server.xml
as
this will greatly decrease the startup time.To create a new web application, we must first set up a new project in Eclipse.
/xmlrpc
xmlrpc-1.2-b1.jar
(from the
xmlrpc-1.2-b1.zip archive) and place it under Eclipse/workspace/<your
project name>/WEB-INF/lib
xmlrpc-1.2-b1.jar.
WEB-INF/lib
directory,
right click and select RefreshNow you are ready to write your first XmlRPC Servlet. Here we use a fortune server that will serve us useless witticisms.
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import org.apache.xmlrpc.XmlRpcServer;
public class XwtXmlRpcServlet extends HttpServlet {
public static XmlRpcServer xmlrpc = new XmlRpcServer();
public void init (ServletConfig config) throws ServletException
{
xmlrpc = new XmlRpcServer();
xmlrpc.addHandler("fortune", new Fortune());
}
public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
{
byte[] result = xmlrpc.execute(req.getInputStream(), null, null);
res.setContentType("text/xml");
res.setContentLength(result.length);
OutputStream output = res.getOutputStream();
output.write(result);
output.flush();
}
}
public class Fortune {
public String get(int index)
{
return fortunes[index];
}
public String get()
{
return fortunes[random.nextInt() % fortunes.length];
}
private String[] fortunes =
{
"This is an ignore, please test...",
"An unbreakable toy is useful for breaking other toys",
"What is another word for Thesaurus?",
"Rule 1: Don't tell people everything you know. Rule 2: *** intentionally left blank ***",
"Don't test for errors you can't handle.",
"Experience is something you don't get until just after you need it.",
"I think the best sign of intelligent life in the Universe is that no-one has tried to contact us.",
"Old MacDonald had an agricultural real estate tax abatement.",
"It is not possible to both understand and appreciate Intel CPUs.",
"The generation of random numbers is too important to be left to chance.",
"Don't you just hate rhetorical questions?"
};
private java.util.Random random = new java.util.Random();
}
The servlet container requires some configuration before you can
access the application. The configuration is stored in the web.xml
file which is stored in web application's WEB-INF
directory.
Here is the configuration file for the example application created
above. Right click the WEB-INF icon in your project and select
New->File, call the file web.xml and copy & paste the following
xml into it:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web
Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>xmlrpc</servlet-name>
<servlet-class>XwtXmlRpcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xmlrpc</servlet-name>
<url-pattern>/fortune</url-pattern>
</servlet-mapping>
</web-app>
To run the example application the servlet container must be running. This is straightforward, if you have the Tomcat-plugin installed in Eclipse. After the servlet container is up and running you can access the application using the specified URL.
<xwt>
<static>
xwt.theme("xwt.standard", "org.xwt.themes.monopoly");
</static>
<import name="xwt.standard.*"/>
<import name="xwt.standard.widgets.*"/>
<template height="180" width="400" thisbox="frame" orient="vertical">
svc = xwt.origin.replace("fortune.xwar", "fortune/fortune");
refresh = function() {
try {
$fortune.text = xwt.xmlrpc(svc).fortune.get();
} catch(e) {
$fortune.text = e.faultString;
}
}
xwt.thread = refresh;
$refresh._click = function(c) { xwt.thread = refresh; }
<box id="fortune"/>
<button id="refresh" text="Refresh"/>
</template>
</xwt>
Zip this directory up, make sure to
omit the name of the toplevel directory from the zip. Rename your
zipfile to fortune.xwar.
In Eclipse, right-click on your project, select Import..., select File system and click Next. Browse to the location of your
xwar, and check the mark before your xwar. You will see the xwar
appearing in your project. Now, click the Restart Tomcat button, and point your
browser to http://launch.xwt.org/stable/localhost:8080/xmlrpc/fortune.xwar.
Enjoy.