Java Server Pages

Available only in PopChart Server Pro and PopChart Server Enterprise.

When you embed a PopChart image in a Java Server Page (JSP), you will use either the Java PopChart Embedder or the JavaBean PopChart Embedder. This section assumes you are using the Java PopChart Embedder. The next section will discuss the use of the JavaBean PopChart Embedder.

For the most part, the code that you will use for embedding a PopChart image into a Java Server Page with Java 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.1 at the end of this section shows you how the web page from Example 4.7 would look as a Java Server Page.

Note: This documentation assumes that if you want to embed a PopChart image in a Java Server Pages, you have already set up a Java-extensible web application server, and that you already know how to create a basic Java Server Page. 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 Server Page, 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 Java Server Page.

<%@ page import="com.corda.pcis.PopChartEmbedder" %>

This instructs the web application to load the PopChartEmbedder class.

Delimiting the Code For Your PoPChart Image

You delimit Java code for your PopChart image with <% and %> (open angle bracket-percent sign, percent sign-closing angle bracket), as in the following code segment:

<%

myPopChart.appearanceFile = "apfiles/line.pcxml";

myPopChart.loadPCXML("http://myserver.com/?graph1");

%>

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 place the following code segment at the location in your Java Server Page where you want the PopChart image to appear.

<%= myPopChart.getEmbeddingHTML() %>

Note that it is opened with a <%= instead of just <%. The equals sign instructs the application server to write a string to the web page, which in this case is myPopChart.getEmbeddingHTML(). There should be nothing else between the opening and closing angle brackets, as the web application server expects only a string, not a code segment.

Note: WebObjects® Users: You should bind this return value to a WOString. In your .wod file you must make sure that you include the following line in your WOString declaration: escapeHTML = NO. Otherwise the HTML codes will be converted to escape characters.

Complete Example Code

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

Example 5.1 Embedding a PopChart Image in a Java Server Page

<%@ page language="java" contentType="text/html" %> 

<%@ page import="com.corda.pcis.PopChartEmbedder" %> 

<% 

   PopChartEmbedder myPopChart = new PopChartEmbedder(); 

   myPopChart.externalServerAddress = "localhost:2001"; 

   myPopChart.internalCommPortAddress = "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"); 

%> 

 

<html> 

<head> 

   <title>My First Embedded PopChart</title> 

</head> 

<body> 

<h1>This is a PopChart Image</h1> 

<hr> 

 

<!-- insert PopChart image here --> 

<%= myPopChart.getEmbeddingHTML() %> 

 

<hr> 

<p>If you see an image above, congratulations. You have successfully embedded your first PopChart image.</p> 

</body> 

</html>