Java Server Page with JavaBeans

Available only in PopChart Server Pro and PopChart Server Enterprise.

When you use the PopChart Embedder with JavaBeans, the code for embedding a PopChart image into a Java Server Page (JSP) looks dramatically different. However, it is actually very similar to the Java code. PopChart Embedder attributes and methods are simply converted to JavaBean setter and getter properties.

This section will help you learn how to use the JavaBean PopChart Embedder. Example 5.3 at the end of this section shows you how the web page from Example 4.7 would look using JavaBeans in a Java Server Page.

Although we will show you how to convert some of the more common attributes and methods to a JavaBean syntax (and we hope that the conversion process is intuitive), if you need to find the JavaBean equivalent of any statement, you can look at the JavaBean Syntax under the corresponding attribute or method description in Chapter 4 of the PopChart Server Reference manual.

Note: This documentation assumes that if you want to embed a PopChart image with JavaBeans, you have already set up a Java-extensible web application server, and that you already know how to use JavaBeans. If this is not the case, you may wish to set up a web application server and learn JavaBeans before continuing.

Converting the PopChart Embedder to a JavaBean Syntax

One of the key differences between the JavaBean PopChart Embedder is the syntax. Because the syntax is so different, you will be unable to simply cut and paste most of the example code from this documentation into a JSP with JavaBeans. Fortunately, this subsection will help you learn how to convert the examples to the syntax.

Setter Properties

Any PopChart Embedder attribute can be converted to a JavaBean setter property. To convert the attribute, create a JSP:setProperty tag. Assign the name attribute of this tag to the name of your PopChart Embedder object. Assign the property attribute of this tag to the PopChart Embedder attribute that you want to set. Finally, assign the value attribute of this tag to the value that you want to give the PopChart Embedder attribute.

For example, consider the appearanceFile attribute, which would normally be used like this:

myPopChart.appearanceFile = "pie.pcxml";

This attribute becomes the following setter property in the JavaBean:

<JSP:setProperty name="myPopChart" property="appearanceFile" value="pie.pcxml" />

Any PopChart Embedder that accepts zero or one parameters can be converted to setter properties in the same fashion. If the method accepts one parameter, that parameter becomes the JSP:setProperty tag's value attribute. Otherwise, the value attribute should be ignored.

For example, consider the loadPCXML(String) method, which would normally be used like this:

myPopChart.loadPCXML(data/may.pcxml);

This method becomes the following setter property in the JavaBean:

<JSP:setProperty name="myPopChart" property="loadPCXML" value="data/may.pcxml" />

Getter Properties

Any PopChart Embedder method that returns a value can be converted to a getter method. To convert the method, create a JSP:getProperty tag. Assign the name attribute of this tag to the name of your PopChart Embedder object. Assign the property attribute of this tag to the PopChart Embedder method that you want to use, except that you should remove the get from the method's name.

For example, consider the getEmbeddingHTML() method, which would normally be used like this:

String pcHTML = myPopChart.getEmbeddingHTML();

This method becomes the following getter property in the JavaBean:

<JSP:getProperty name="myPopChart" property="embeddingHTML" />

Importing the Library

You do not need to import the PopChart Embedder library into a Java Server Page if you are using JavaBeans. however, you must 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.

Delimiting the Code For Your PoPChart Image

JavaBean commands are designed to look like regular HTML tags. Because of this, there is no special way of delimiting the code that embeds your PopChart image. You simply throw the JavaBean code directly into the JSP.

Instantiating a PopChart Embedder Object

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

<jsp:useBean id="myPopChart" scope="page" class="com.corda.bean.PopChartEmbedderBean" />

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 tags:

<jsp:setProperty name="myPopChart" property="externalServerAddress" value="http://popchart.mycompany.com:2001" />

<jsp:setProperty name="myPopChart" property="internalCommPortAddress" value="10.0.1.1:2002" />

Setting the User Agent

When you use the JavaScript PopChart Embedder, it automatically told PopChart Server what the web client's user agent was. 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.

The following line of code shows how to do this with the JavaBean PopChart Embedder.

<jsp:setProperty name="myPopChart" property="userAgent" value="<%=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.

Miscellaneous Differences

There are certain Java methods that are unavailable in a JavaBean syntax. These are as follows:

You can still take advantage of these functions, but you must use Java code, as described in the previous section, "Java Server Pages." The name that you gave the object (e.g. myPopChart) when you initialized it will be the same as the name you use to access its Java methods.

Example 5.2 shows how JavaBean and Java code can be mixed together.

Example 5.2 Mixed JavaBean and Java Code

<jsp:useBean id="myPopChart" scope="page" class="com.corda.bean.PopChartEmbedderBean" /> 

<jsp:setProperty name="myPopChart" property="externalServerAddress" value="localhost:2001" /> 

<jsp:setProperty name="myPopChart" property="internalCommPortAddress" value="localhost:2002" /> 

<jsp:setProperty name="myPopChart" property="appearanceFile" value="examples/apfiles/bar.pcxml" /> 

<%  

   myPopChart.loadData("graph", "data/data1.csv"); 

   myPopChart.addHTMLTable("graph"); 

%> 

<jsp:getProperty name="myPopChart" property="embeddingHTML" />

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.3 Embedding a PopChart Image with JavaBeans

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

 

<html> 

<head> 

   <title>My First Embedded PopChart</title> 

</head> 

<body> 

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

<hr> 

 

<!-- insert PopChart image here --> 

   <jsp:useBean id="myPopChart" scope="page" class="com.corda.bean.PopChartEmbedderBean" /> 

   <jsp:setProperty name="myPopChart" property="externalServerAddress" value="localhost:2001" /> 

   <jsp:setProperty name="myPopChart" property="internalCommPortAddress" value="localhost:2002" /> 

   <jsp:setProperty name="myPopChart" property="appearanceFile" value="examples/apfiles/bar.pcxml" /> 

   <jsp:setProperty name="myPopChart" property="imageType" value="FLASH" /> 

   <jsp:setProperty name="myPopChart" property="fallback" value="STRICT" /> 

   <jsp:setProperty name="myPopChart" property="imageType" value="FLASH" /> 

   <jsp:setProperty name="myPopChart" property="width" value="600" /> 

   <jsp:setProperty name="myPopChart" property="height" value="400" /> 

   <jsp:setProperty name="myPopChart" property="pcScript" value="title.setText(Hello World)" /> 

   <jsp:setProperty name="myPopChart" property="userAgent" value="<%=request.getHeader(\"user-agent\")%>" /> 

   <jsp:getProperty name="myPopChart" property="embeddingHTML" /> 

 

<hr> 

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

</body> 

</html>