Java Servlets

To embed a Highwire™ link in a Java* Servlet, use the Java Corda® Embedder.

Embedding a Highwire link into a Java Servlet can be broken down into the following steps:

The example code at the end of this section shows how a Java Servlet can produce a Web page containing a link to a Highwire document.

Note: This documentation assumes that you have set up a Java-extensible Web application server and that you are familiar with Java Servlets

Importing the Library

Before importing the Corda Embedder library into a Java Servlet, you must first make sure that the CordaEmbedder.jar file, located in the dev_tools/embedder/java folder, is in the classpath for the Web application. If you do not know how to add a .jar file to the Web application's classpath, see Java Application Servers in the Corda 7 Install and Administration manual.

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.

Instantiating a Corda Embedder Object

To instantiate a Corda Embedder object, Use the following line of code (this assumes that you want to name the object myImage).

CordaEmbedder myImage = new CordaEmbedder();

Setting the Server Information

In order to use the Corda Embedder you must first tell it the location of Corda Server™ .

You need to set two values: 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 example, if the Corda Embedder communicates with Corda Server at 10.0.1.1:2002, and Web clients request Corda images from http://myserver.mycompany.com:2001, you need to include the following lines in the code that generates the Highwire link:

myImage.externalServerAddress = "http://myserver.mycompany.com:2001"

myImage.internalCommPortAddress = "10.0.1.1:2002"

Your Corda Server administrator should be able to tell you what values you need to use for these settings. If you are having trouble finding these values, see Identifying Server Addresses in the Corda 7 Developer Reference.

Specifying Document Information

After you have specified the server address information, give the Corda Embedder some information about the Corda image you want to create.

The most important piece of information you need to provide is the location of the Web document you want to convert. Do this using the Corda Embedder loadDoc method.

myImage.loadDoc("http://localhost/highwire");

This allows us to specify the URL directly, but it unfortunately decreases the portability of the Servlet—you'll have to change the code if you ever change the location of the Web application.

Alternatively, use the request object to dynamically compute the current URL of the Servlet. The code below shows one way of doing this.

String pagename = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI();

myImage.loadDoc(pagename);

For more information about the request object, see the JSP documentation.

There are a number of other Corda Embedder commands for specifying document information. Fortunately, these commands are consistent in all Web application environments. Thus, it is unnecessary to discuss each of these commands individually at this time. For more information, see Other Topics in this manual, and Corda Embedder API in the Corda 7 Developer Reference .

Writing the Link to a Web Page

When you are ready to actually write the embedding HTML to the Web page, 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(myImage.getEmbeddingHTML());

Compiling a Servlet

Make sure that CordaEmbedder.jar is in the classpath when you compile the servlet.

If you do not know how to compile a servlet, see the Web appliation server's documentation.

Complete Example Code

The following Java Servlet produces a Web page that contains a link to a Highwire document. When you click this link, Corda Server translates the Web page to PDF.

Embedding a Highwire Link 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 Highwire Document</title>");

pw.println("</head>");

pw.println("<body>");

pw.println("<h1>This is a very simple Web page</h1>");

pw.println("<hr /><p>Click below to convert this page to PDF using Corda Server's Highwire.</p>"

 

// Begin Corda Embedder Code

CordaEmbedder myImage = new CordaEmbedder();

myImage.externalServerAddress = "http://<server_address>:2001";

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

String pagename = "http://" + request.getServerName() + ":" + request.getServerPort() + request.getRequestURI();

myImage.loadDoc(pagename);

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 Highwire link. 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.