Network SDK for Java - Developing a simple ASH Servlet

This example gives you a simple guide on how to develop a simple ASH servlet. By using Servlets it’s possible to send in PGC files to a web application as http request and process the data in the ash servlet.
This small example is written as a web application in Netbeans and the web pages and servlet is packaged into a web application archive (war) file which is later deployed on a web server capable of running java code (e.g. Apache Tomcat which is included in the Netbeans installation). The servlet requires both the Anoto network SDK for Java and the Apache Commons fileupload library to work. Apache Commons fileupload can be found at the Apache website.
To test your Ash Servlet you can download a Requestsender or the Anoto PenDirector from the download area. By using the Requestsender it is possible to post a pgc file to an URL and display some feedback. The Anoto PenDirector can be used to extract strokes from the pen and post them directly to a URL.


First we need to develop simple Java Server Page (JSP) that will act as a frontpage and pass on incoming http requests to your servlet. The JSP will also require some meta data which is placed in the folders WEB-INF and META-INF. If you are using a tool like Netbeans this information will be automatically generated when you create a web application.


First we specify the content type (text and html) and encoding (in this case western).

<%@page contentType="text/html"%>  <%@page pageEncoding="UTF-8"%>

Then we write some simple HTML code and create a simple website.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Demo ASH</title>

        <h1>Write on!</h1>
        Waiting for doPost request containing a PGC file.

We add a tag and specify that all incoming data should be handled by the doPost method in the class Ash.

Now we can continue and write the actual servlet. We simply name the servlet (remember the tag in index.jsp).

import*;  import*;  import java.util.*;    import javax.servlet.*;  import javax.servlet.http.*;    import com.anoto.api.*;  import org.apache.commons.fileupload.*;  

We let our Ash class exstend the existing HttpServlet class and load some basic files and directories.

 public class Ash extends HttpServlet {         private final String BASE_PATH = "C:\\development\\";       private final String PAD_FILE = BASE_PATH + "";       private final String BG_IMAGE = BASE_PATH + "BGImage.png";       private final String APPL_NAME = "webAsh";  	

Next we overide the init method of the httpServlet class and add some new code for loading a pad file. We initialize the servlet through call to the super construktor in HttpServlet.

    /**       * Init the Ash Servlet and load a pad file       */       public void init(ServletConfig config) throws ServletException       {           //Init servlet through call to super class           super.init(config);           //Open and load pad file           File padFile = new File(PAD_FILE);           try           {               PenHome.loadPad(APPL_NAME, padFile);           } catch (Exception e){               //Something went wrong...           }       }

To handle incomming http reqests we overide the doPost method of HttpServlet and add some Anoto Specific code for creating a pen object, extracting pages and rendering images. For more details on how to process strokes from a pgc file please have a look at the first example “developing a simple Application Service Handeler”.

    /*       * doPost will handle all files posted to the Ash.       */       protected void doPost(HttpServletRequest request, HttpServletResponse response)           throws ServletException, IOException       {             //Create a writer for sending response to sender           PrintWriter out = response.getWriter();           out.println("Recieved PGC file, processing content");             //Start processing recieved file           try           {               //Create a new Pen Object from recieved PGC file               Pen myPen = PenHome.create(request, response, APPL_NAME);               //Create a renderer               Renderer strokeRenderer;               //Create an Iterator containing all pages in PGC file.               Iterator pageIter = myPen.getPages();                 while (pageIter != null && pageIter.hasNext())               {                   //For each page in the iterator we create a new page                   Page newPage = (Page);                   //Add a new page to the renderer                   strokeRenderer = RendererFactory.create(newPage);                   //Add a background image to the page in the renderer                   strokeRenderer.setBackground(BG_IMAGE);                   //Render page to a png file                   String fileName = BASE_PATH + "output_" +                       newPage.getPageAddress() + ".png";                   strokeRenderer.renderToFile(fileName);                     //Send some feedback about rendered form and close writer                   out.println("rendered file: " + fileName);                   out.flush();                   out.close();               }           }           catch (Exception e)           {               //Something went wrong...           }       }  }

Now we should have a working ash servlet that can receive incoming request and process pgc files. To test the ash you will have to create a new web application in your development environment and add the files and index.jsp. You can also deploy a prepared webAsh.war file (included in the zip below) on your web server (must be able to run Java). To get a working solution you also need to add the Anoto Java API Jar file in the folder \Apache Software Foundation\Tomcat 5.5\webapps\webAsh\WEB-INF\lib\ after deployment and then restart your web server. Now you can try posting a pgc file to the application running on your web server. The URL should be http://localhost:port/webAsh/Ash.

Download source code

Still need help? Contact Us Contact Us