OneSpan Sign How To: Creating a Callback Event Notification Listener in Java

Haris Haidary, October 26, 2016

In my previous blog, I showed you how to create a simple callback event notification listener in C# and listen for the "PACKAGE_COMPLETE" event in order to download signed documents. In this blog, I will show you how to do the same in Java. Let's get straight into it.


Register for Event Notifications

Before the OneSpan Sign system can notify you of an event, you must first register to be notified in case of an event. You can follow this feature guide on how to register for event notifications.

Eclipse IDE for Java EE Developers

For this blog, you will need Eclipse's IDE for Java EE Developers. If you do not already have it, you can download it for free, here.

OneSpan Sign's Java SDK

You can download the Java SDK from the following link. It also provides a link to some examples of using the Java SDK on GitHub. Unzip the SDK to the location of your choice.


In order to receive callback event notifications, you will need a publicly accessible URL to hit and work. Your localhost server on its own will not work. Ngrok is a very simple and easy to use tool that creates a secure tunnel on your local machine along with a public URL you can use for browsing your local site. This saves you the trouble of deploying your web application.

The Code

You can download the example code from the Developer Community Code Share. Go ahead and create a new Dynamic Web Project. If this is your first time working with the Java framework, you can follow this previous blog on how you can get started. Make sure to generate a web.xml deployment descriptor when creating your project.

Once your project has been created, in your Project Explorer, open your "Java Resources" folder. Right-click on "src" and create a new package named "listener". Then, add a new class named". Below is the code of the action controller to process the event notification.

public class ListenerController extends HttpServlet {
	 private String pkgId = new String();
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    StringBuilder builder = new StringBuilder();
	    String aux = "";

	    while ((aux = req.getReader().readLine()) != null) {

	    String text = builder.toString();
	    try {
			JSONObject json = new JSONObject(text);
			pkgId = json.get("packageId").toString();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
	    new Thread(new Runnable() {
	        public void run() {
	        	try {;
				} catch (IOException e) {
					// TODO Auto-generated catch block


In the code above, we use the doPost() method to intercept HTTP POST requests. Once all documents in a package has been signed and the package completed, OneSpan Sign will make a POST request to my registered URL with the following example JSON payload:


  "@class": "com.silanis.esl.packages.event.ESLProcessEvent",
  "sessionUser": "0787be84-f095-44c7-ba00-787093df86fc",
  "packageId": "KHiRfOXgKK0gpVWwwpFOSNy6o34=",
  "message": null,
  "documentId": null


Then, we will use the package id in order to download the signed documents. In your "src" folder, create a new class named "" with the code below.


public class DownloadDocuments {

	public static void download(String packageId) throws IOException{
		EslClient eslClient = new EslClient("api_key", "");
		byte[] documents = eslClient.downloadZippedDocuments(new PackageId(packageId));
		FileOutputStream stream = new FileOutputStream("C:/Users/hhaidary/Desktop/documents_" + packageId + ".zip");
		try {
		} finally {



The code above utilizes the eslClient to download the signed documents. Make sure to reference the OneSpan Sign Java SDK in this class. Then, create two JSP file named "index.jsp" and "listenerpage.jsp". These two JSP files can be anything. For the purposes of this example, I will keep them as static pages.


<!-- index.jsp -->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>eSignLive Listener</title>
	<div>eSignLive Notifications</div>
<!-- listenerpage.jsp -->
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
	<div>Testing some esignlive listener...</div>


Finally, you will need to edit your web.xml file in order to map the servlet controller to the "listenerpage.jsp" page. Here's how your web.xml file should look like:


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">




Running Your Code

In your Project Explorer, right-click on your project and select Run As > Run on Server. Hit Finish. Then, open a command prompt and change the directory to the location where you saved your ngrok executable. Enter the following command:

ngrok http [port] -host-header="localhost:[port]"

Below is a screenshot of what you can expect after running the command:




Then, login to your OneSpan Sign account and browse to the Admin page. Enter in the "Callback URL" field the URL to your callback listener (i.e. and register for the "transaction complete" event. Finally, create, send, and complete a test package. You should be able to view the signed documents as a zipped file in the location where you chose to save it in the "" class.

If you have questions regarding this blog or anything else concerning integrating OneSpan Sign into your application, visit the developer community forums: That's it from me. Thank you for reading! If you found this post helpful, please share it on Facebook, Twitter, or LinkedIn. You can find all of my previous blogs by going to my author page.

Haris Haidary
Junior Technical Evangelist
LinkedIn | Twitter


OneSpan Sign Developer Community

Join the OneSpan Sign Developer Community! Forums, blogs, documentation, SDK downloads, and more.

Join Today