This example requires an installation of Tomcat (I'm using 5.5) and a Firefox browser client, version 2.0+. Disclaimer: The code is very sparse for the purposes of clarity (and sure, because that's all I have time for right now). There is no exception handling implemented.
1) Make this folder structure in the /webapps folder of your tomcat installation:
| iguanaSoa | ||
| |- | WEB-INF | |
| |- | lib | |
| |- | classes |
2) Download the HttpClient jar and its dependency jars:
HttpClient
dependencies
Place these jars in the /lib folder that was created in step 1.
3) Make the web.xml file for this war and place it in WEB-INF:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<display-name>iguanaChart SOA Demo</display-name>
<servlet>
<servlet-name>soaDemo</servlet-name>
<servlet-class>IguanaSoaDemo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>soaDemo</servlet-name>
<url-pattern>/demo.xhtml</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
</web-app>
4) Make the index.htm file and put it in /iguanaSoa:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title>iguanaChart SOA Demo - Start</title> </head> <body> <p>Click <a href="./demo.xhtml>here</a> to get things started...</p> </body> </html>
5) Make the soaDemo.jsp file and put it in /iguanaSoa:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>iguanaChart SOA Demo</title>
</head>
<body>
<h1>iguanaChart SOA Demo</h1>
<table width="95%">
<tr>
<td width="400">
<b>Cool things</b>
<ul>
<li>Quality sandwiches</li>
<li>Television</li>
<li>Iguanas</li>
<li>Carnivorous trees</li>
</ul>
</td>
<td width="400">
<!-- svg content -->
<%= request.getAttribute("chart") %>
</td>
</tr>
<tr>
<td>More content here...</td>
<td>Whatever you want here...</td>
</tr>
</table>
</body>
</html>
6) Complile this java servlet class (below). The HttpClient jar and Tomcat servlet jars must be in the compile classpath. Put the IguanaSoaDemo.class file in /classes
import java.io.IOException;
import java.net.URL;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.HttpStatus;
public class IguanaSoaDemo extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String chartData;
String svgData;
String postUrl = "http://localhost:8080/soa-sample/postXml";
/**
* You would query the database or do whatever processing necessary to gather
* the chart data here, and then build and XML string like the one below.
**/
chartData = "<chart><cfg><title>Pets that Jenny Wants</title>" +
"<type>bar</type><w>400</w><h>300</h>" +
"<link>http://www.iguanatronics.com/soa-sample/clickChart</link></cfg>" +
"<catg><name>Pet Type</name><val>Dog:Cat:Horse:Bird</val></catg>" +
"<dss><name>Pets</name><valLbl>Count</valLbl>" +
"<ds><name>Pet Count</name><val>3:5:2:1</val></ds></dss></chart>";
response.setContentType("application/xhtml+xml");
HttpClient httpClient = new HttpClient();
HttpMethod method = new GetMethod();
NameValuePair[] params = new NameValuePair[1];
params[0] = new NameValuePair("data", chartData) ;
method.setPath(postUrl);
method.setQueryString(params);
int responseCode;
responseCode = httpClient.executeMethod(method);
svgData = null;
if (responseCode == HttpStatus.SC_OK) {
svgData = method.getResponseBodyAsString();
}
else {
throw new ServletException("Problem: " + responseCode);
}
request.setAttribute("chart", svgData);
RequestDispatcher dispatcher;
dispatcher = getServletContext().getRequestDispatcher("/soaDemo.jsp");
dispatcher.include(request, response);
}
}
7) Start up tomcat and access the webapp from this URL (assuming tomcat is running on port 8080):
http://localhost:8080/iguanaSoa
Click on the here link. You should see a web page with html and SVG content.
[ : iguanaChart home : : SOA : ]