To embed a Corda® image in a Java* Servlet, use the Java Corda Embedder.
Embedding a Corda image into a Java Servlet can be broken down into the following steps:
The example code at the end of this section shows how to embed a Corda image in a Java Servlet.
Note: This documentation assumes that you are familiar with Java Servlets.
Before importing the Corda Embedder library into a Java Servlet, first make sure that <product_root>\Server\dev_tools\embedder\java\CordaEmbedder.jar is in the Web Application's classpath. For more information, see Java Application Servers in the Corda 7 Install and Administration Guide.
To import the Corda Embedder library, include the following line at the beginning of the Servlet.
import com.corda.CordaEmbedder;
This instructs the Servlet to import the CordaEmbedder class.
To instantiate a Corda Embedder object, use code similar to the following:
CordaEmbedder myImage = new CordaEmbedder();
Corda Embedder needs to know two Corda Server™ addresses: the address that the Web client uses to access Corda Server (externalServerAddress), and the address that Corda Embedder uses to access Corda Server (internalCommPortAddress). For more information about determining these addresses, see Identifying Server Addresses.
For example, if Corda Embedder communicates with the Corda Server at 10.0.1.1:2002, and Web clients request images from http://<server_address>:2001, include the following in the code that generates the Corda image:
myImage.externalServerAddress = "http://<server_address>:2001"
myImage.internalCommPortAddress = "10.0.1.1:2002"
After specifying server address information, give Corda Embedder some information about the Corda image you want to embed.
There are numerous Corda Embedder commands to specify image information, which are consistent in all Web application environments. Thus, it is unnecessary to discuss each of these commands individually at this time.
The table below provides links to more information about Corda image features and components:
|
Topic |
Where To Go |
|
Annotations |
Data Annotations in the Corda Builder™ User Guide |
|
Content Delivery / Presentation Control |
|
|
Data |
|
|
Drilldown |
|
|
Graph or Map Customization |
|
|
Image Size & Format |
When you are ready to write the embedding HTML to a Web page, output the getEmbeddingHTML method to the Web page. For example, if you have instantiated a PrintWriter object named pw, use code similar to the following to output the embedding HTML:
pw.println(myImage.getEmbeddingHTML());
Make sure that CordaEmbedder.jar is in the classpath when compiling the servlet. If you do not know how to compile a servlet, refer to the documentation for the Web application server.
The example below demonstrates embedding a Corda image in a Java Servlet.
Note: To produce an example map instead of a graph, replace examples/bar.itxml with examples/map/australia_combo.itxml in the code designating the Image Template file.
Embedding a Corda Image in a Java Servlet
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.corda.CordaEmbedder;
public class example1 extends HttpServlet
{
public void init(ServletConfig config)
throws ServletException
{
super.init(config);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter pw = response.getWriter();
response.setContentType("text/html");
try
{
pw.println("<html>");
pw.println("<head>");
pw.println("<title>My First Embedded Image</title>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h1>Your image will appear below:</h1>");
// Begin Corda Embedder Code
CordaEmbedder myImage = new CordaEmbedder();
myImage.externalServerAddress = "http://<server_address>:2001";
myImage.internalCommPortAddress = "http://localhost:2002";
myImage.imageTemplate = "image_templates\examples\bar.itxml";
myImage.width = 600;
myImage.height = 400;
myImage.pcScript = "title.setText(Hello World)";
pw.println(myImage.getEmbeddingHTML());
// End Corda Embedder Code
pw.println("</body>");
pw.println("</html>");
}
catch(Exception exc)
{
}
}
}
Note: This code catches any exceptions that may be generated as you embed the Corda image. You do not have to do this. The Corda 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.