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 an 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 Anoto's penDirector from the download area. By using the Requestsender, it is possible to post a PGC file to an URL and display some feedback. Anoto's 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 front-page 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):

<span class="wysiwyg-font-size-small"><%@page contentType="text/html"%>  <%@page pageEncoding="UTF-8"%></span>

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 then 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):

<span class="wysiwyg-font-size-small">import*;  import*;  import java.util.*;    import javax.servlet.*;  import javax.servlet.http.*;    import com.anoto.api.*;  import org.apache.commons.fileupload.*;  </span>

We let our ASH class extend the existing HttpServlet class and load some basic files and directories:

<span class="wysiwyg-font-size-small"> 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";  	</span>

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

<span class="wysiwyg-font-size-small">    /**       * 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...           }       }</span>

To handle incoming HTTP requests, we override 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”:

<span class="wysiwyg-font-size-small">    /*       * 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...           }       }  }</span>

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 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 the source code from here.

Still need help? Contact Us Contact Us