Java Servlets

Available only in PopChart Server Pro and PopChart Server Enterprise.

For the most part, the code that you will use for embedding a PopChart image into a Java Servlet is exactly the same as the code you created for the JavaScript embedder in Chapter 4. This section will explain the major differences. Example 5.4 at the end of this section shows you how the web page from Example 4.7 would look as a Java Servlet.

Note: This documentation assumes that if you want to embed a PopChart image in a Java Servlet, you have already set up a Java-extensible web application server, and that you already know how to create a basic Java Servlet. If this is not the case, you may wish to set up a web application server and brush up on Servlets before continuing.

Importing the Library

Before you can import the PopChart Embedder library into a Java Servlet, you must first make sure that the PopChartEmbedder.jar file, located in the dev_tools/java_embedder folder, is in the classpath for your web application. If you do not know how to add a Jar file to your web application's classpath, refer to "Including the PopChartEmbedder.jar File in Your Classpath" on page 5-46.

To import the PopChart Embedder library, include the following line at the beginning of your Servlet.

import com.corda.pcis.PopChartEmbedder;

This instructs the Servlet to import the PopChartEmbedder class.

Instantiating a PopChart Embedder Object

To instantiate a PopChart Embedder object, you should use the following line of code (this assumes that you want to name your object myPopChart).

PopChartEmbedder myPopChart = new PopChartEmbedder();

Setting the Server Information

As mentioned in "Setting the Server Information" in Chapter 4, non-JavaScript versions of PopChart Embedder require you to tell them the location of PopChart Server. You need to set two values: the address that the web client will use to access PopChart Server ( externalServerAddress), and the address that PopChart Embedder uses to access PopChart Server ( internalCommPortAddress).

These are not the same addresses. First of all, PopChart Embedder uses a different port to communicate with PopChart Server. This port is known as the comm port, and, unless you change it, is set to 2002. Second of all, if you are using a firewall or a redirector, PopChart Server's address will be different for the web client than for the PopChart Embedder.

For example, if you are running PopChart Server at 10.0.1.1, but your web clients request images from http://popchart.mycompany.com, you need to include the following lines of code in the code that generates your PopChart image:

myPopChart.externalServerAddress = "http://popchart.mycompany.com:2001";

myPopChart.internalCommPortAddress = "10.0.1.1:2002";

Setting the User Agent

When you use the JavaScript PopChart Embedder, it automatically tells PopChart Server what the web client's user agent is. However, server-side PopChart Embedders can only tell PopChart Server a client's user agent if you specifically pass this information on to PopChart Embedder using the userAgent attribute. This information is significant because if PopChart Server knows what browser a client is using, it can return optimized (i.e. shorter and without JavaScript) HTML for embedding the PopChart image.

Server-side PopChart Embedders can only tell PopChart Server a client's user agent if you specifically pass this information on to PopChart Embedder using the userAgent attribute. The following line of code shows how to do this with the Java PopChart Embedder.

myPopChart.userAgent = request.getHeader("USER-AGENT");

It is not necessary to tell PopChart Embedder the client's user agent; however, you will not be able to take advantage of optimized HTML if you do not do this.

Writing the image to Your Web Page

When you are ready to actually write your embedding HTML to the web page, you will need to output the getEmbeddingHTML() method to the web page. For example, this is often times done with a PrintWriter object. If you have instantiated a PrintWriter object named pw, you would use the following line of code to output the embedding HTML

pw.println(myPopChart.getEmbeddingHTML());

Complete Example Code

The following Java Servlet will produce exactly the same results as the page you created in Chapter 4.

Example 5.4 Embedding a PopChart Image in a Java Servlet

import java.text.*; 

import java.io.*; 

import javax.servlet.*; 

import javax.servlet.http.*; 

import com.corda.pcis.PopChartEmbedder; 

 

public class ServletExample1 extends HttpServlet 

 

   public void init(ServletConfig config) 

   throws ServletException 

   { 

      super.init(config); 

   } 

 

   public void doPost(HttpServletRequest request, HttpServletResponse response) 

   throws ServletException, IOException 

   { 

      doGet(request, response); 

   } 

 

   public void doGet(HttpServletRequest request, HttpServletResponse response) 

   throws ServletException, IOException 

   { 

 

      response.setContentType("text/html"); 

      PrintWriter pw = response.getWriter(); 

 

      try 

      { 

         PopChartEmbedder myPopChart = new PopChartEmbedder(); 

         myPopChart.externalServerAddress = "http://localhost:2001"; 

         myPopChart.internalCommPortAddress = "http://localhost:2002"; 

         myPopChart.appearanceFile = "examples/apfiles/bar.pcxml"; 

         myPopChart.imageType = "FLASH"; 

         myPopChart.fallback = "STRICT"; 

         myPopChart.width = 600; 

         myPopChart.height = 400; 

         myPopChart.pcScript = "title.setText(Hello World)"; 

         myPopChart.userAgent = request.getHeader("USER-AGENT"); 

         pw.println(myPopChart.getEmbeddingHTML()); 

      } 

      catch(Exception exc) 

      { 

         pw.println("Error generating image."); 

      } 

   } 

}

Note: This code catches any exceptions that may be generated as you embed the PopChart image. You do not have to do this. The PopChart Embedder itself does not throw any exceptions. However, the PrintWriter or the request object might throw an exception, so you might want to catch it. Besides, it's good coding practice to catch exceptions.