SAX Servlet Example

Aim
Create a simple java SAX (Simple API for XML) servlet demonstrating all the basic fundamentals. This example servlet will accept some parameters and output some XML. This Can be used with the AJAX tutorial Ajax Example Using XMLHttpRequest and Callback. SAX maps element to objects well. If your data is structured in a way that makes it easy to create this mapping you should use SAX. If however, your data is much better represented as a tree then you should use DOM.

Assumptions
You have an application/web server and a compatible JDK installed.

Versions used in this example
Sofware/ComponentImage
Windows XP SP2N/A
tomcat 6N/A
JDK 1.5.0N/A
Links to these files can be found here.

This tutorial will follow the directory structure below. This is pretty much the standard webapps structure. The SAXHelloWorldServlet directory will be referred to as the project directory. This servlet accepts a parameter called 'name' and echoes it back with a greeting.

SAXHelloWorldServlet
_|_WEB-INF
_|__|__classes
_|__|__web.xml
_|_SAXServletDemo.java


Write and Compile Servlet
  1. Write the servlet,
     1. import java.io.*;
     2. 
     3. import javax.servlet.*;
     4. import javax.servlet.http.*;
     5. 
     6. import org.xml.sax.*;
     7. import org.xml.sax.helpers.*;
     8. import javax.xml.parsers.*;
     9. import javax.xml.transform.*;
    10. import javax.xml.transform.stream.*;
    11. import javax.xml.transform.sax.*;
    12. 
    13. public class SAXServletDemo extends HttpServlet {
    14.     
    15.     public void doGet(HttpServletRequest request, HttpServletResponse response){
    16.     try{
    17.         String name=request.getParameter("name");
    18.         String message="Hello there from the SAX servlet";
    19.         
    20.         response.setContentType("text/xml");
    21.         
    22.         SAXTransformerFactory tfactory= (SAXTransformerFactory)SAXTransformerFactory.newInstance();
    23.         TransformerHandler thandle   = tfactory.newTransformerHandler();
    24.         Transformer tformer          = thandle.getTransformer();
    25.         
    26.         thandle.setResult(new StreamResult(response.getWriter()));
    27.         tformer.setOutputProperty(OutputKeys.ENCODING,"ISO-8859-1");
    28.                 
    29.         thandle.startDocument();
    30.         AttributesImpl attribs = new AttributesImpl();
    31.         thandle.startElement("","","user-data",attribs);
    32.         
    33.         if(name!=null){
    34.             attribs.clear();
    35.             attribs.addAttribute("","","type","CDATA","Alias");
    36.             thandle.startElement("","","name",attribs);
    37.             thandle.characters(name.toCharArray(),0,name.length());
    38.             thandle.endElement("","","name");
    39.         }
    40.         
    41.            attribs.clear();
    42.         attribs.addAttribute("","","type","CDATA","greeting");
    43.         thandle.startElement("","","message",attribs);
    44.         thandle.characters(message.toCharArray(),0,message.length());
    45.         thandle.endElement("","","message");
    46.         
    47.         thandle.endElement("","","user-data");
    48.         thandle.endDocument();
    49.         
    50.     }catch(Exception e){
    51.         e.printStackTrace();
    52.     }
    53. 
    54.     }
    55. }
    Hide line numbers
  2. `
  3. Compile the code, setting your tomcat's lib dir as an ecternal directory to pickup the servlet api,

    ..workspace\SAXHelloWorldServlet>javac -d WEB-INF\classes -extdirs D:\apache-tomcat-6.0.18\lib *.java
  4. `
  5. Create the web.xml file as show below in the WEB-INF directory
     1. <?xml version="1.0" encoding="UTF-8"?>
     2. <web-app>
     3. 
     4.     <display-name>SAX XML Output</display-name>
     5. 
     6.     <servlet>
     7.         <servlet-name>SAX XML</servlet-name>
     8.         <servlet-class>SAXServletDemo</servlet-class>
     9.     </servlet>
    10. 
    11.     <servlet-mapping>
    12.         <servlet-name>SAX XML</servlet-name>
    13.         <url-pattern>/saxxmldemo.xml</url-pattern>
    14.     </servlet-mapping>
    15. 
    16.     <welcome-file-list>
    17.         <welcome-file>index.html</welcome-file>
    18.     </welcome-file-list>
    19. </web-app>
    Hide line numbers
  6. `
  7. Copy the directory to the webapps directory in tomcat or create a war file and deploy it in an application server such as glassfish.

Testing
  1. Open up a browser ant type in http://127.0.0.1:8080/SAXJHelloWorldServlet/saxxmldemo.xml?name=spiderman
    You should see the XML below,
     1. <?xml version="1.0" encoding="UTF-8"?>
     2. <user-data> 
     3.    <name type="Alias">spiderman</name>
     4.    <message type="greeting">Hello there from the SAX servlet</message>
     5. </user-data> 
    Hide line numbers

Back to the tutorial trail | Home