Servlet Usage

Introduction

Thanks to the clear interfaces, it is very easy to create servlets providing chart images (or tables). In your servlet's doGet() or doPost() method:

  1. Read in your Chart XML:
      ChartXMLDocumentHandler XMLDocHandler =
        new ChartXMLDocumentHandler(contextURL, reader);
    
  2. Create Chart:
      Chart chart = XMLDocHandler.createChart();
    
  3. Produce output:
      response.setContentType(chart.getContentType());
      chart.putContent(response.getOutputStream());
    

Deploying Servlet

The steps to get your Chart servlet up and running are listed below. If you are unfamiliar with servlet technology, you may find Java Servlet Pages helpful. However, with these instructions and using the Davisor Chart web application (inside the Davisor Chart demo download package) as an example, you should be able to create an own Chart servlet.

  1. Write a servlet, like the one described in this page, and compile it. You may want to change the way the input XML is selected...
  2. Copy the compiled XMLServlet.class to "WEB-INF/classes" folder.
  3. Ensure that there is davisorchart.jar in "WEB-INF/lib" folder.
  4. Define the servlet in the "WEB-INF/web.xml" file, by adding the following section into it:
      <servlet>
        <servlet-name>xmlServlet</servlet-name>
        <display-name>XML example servlet</display-name>
        <servlet-class>XMLServlet</servlet-class>
      </servlet>
    
    This maps the servlet class to some servlet name. Note that servlet definitions must be placed after context parameter, filter, and listener elements in "web.xml". Consult your web server documentation for the details of web.xml deployment description file, or modify the "web.xml" inside the "chart.war" for your needs.
  5. Define the servlet mapping in the "WEB-INF/web.xml" file, by adding the following section into it:
      <servlet-mapping>
        <servlet-name>xmlServlet</servlet-name>
        <url-pattern>/xmlServlet</url-pattern>
      </servlet-mapping>
    
    This maps a url to the named servlet. Servlet mappings lie in "web.xml" after servlet definitions and before session config and MIME mappings.
  6. Create a .war file containing the "WEB-INF" directory, necessary Chart XML files (if needed), and possible HTML pages. For example, let's assume that we have compiled the unmodified XMLServlet, and our web.xml, davisorchart.jar, and examples folder are copied straight from chart.war. Then we can create a new web application by command:
    jar cf mychart.war WEB-INF index.html examples
    The content of index.html is in the most simplistic form:
      <html><body>
      <a href="xmlServlet?i=0">Example 1</a><br />
      <a href="xmlServlet?i=1">Example 2</a>
      </body></html>
    
  7. Deploy the new .war into your web server and surf into http://localhost:8080/mychart for testing. Deploying is made for example for Tomcat by copying the mychart.war file into "$TOMCAT_HOME/webapps" -folder.

Examples

See the whole Java code for the servlet used to produce the examples below.

A bar chart created by a servlet from XML.
Pie chart and legend, created by a servlet from XML and CSV.