Home > Articles > Programming > Java

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Servlet

The job of the servlet is the most complex piece of code in this example. In the servlet I retrieve all the customers from the database and put them into an xml document. The xml document is modeled after Apple’s plist dtd, which enables me to load the xml document directly into standard Objective-C objects without any additional custom code on the client side.

package com.zarrastudios.example.servlet;

import com.zarrastudios.example.util.CustomerUtil;
import com.zarrastudios.example.entity.CustomerLocal;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;
import java.util.Iterator;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class CustomerListServlet extends HttpServlet {

  private static Log log = LogFactory.getLog(CustomerListServlet.class);
  private static DateFormat df = new SimpleDateFormat("yyyy-MM-dd’T’HH:mm:ss");
  private DocumentBuilder builder;
  private Transformer transformer;

  public void init() {
    try {
      transformer = TransformerFactory.newInstance().newTransformer();
      builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    } catch (Exception e) {
      log.error("Exception thrown", e);
    }
  }

  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    try {
      Document doc = builder.newDocument();

      Element plist = doc.createElement("plist");
      plist.setAttribute("version", "1.0");
      doc.appendChild(plist);

      Element arrayOfCustomers = doc.createElement("array");
      plist.appendChild(arrayOfCustomers);

      Iterator customers = CustomerUtil.getLocalHome().findAll().iterator();
      while (customers.hasNext()) {
        CustomerLocal local = (CustomerLocal) customers.next();
        Element customerDict = doc.createElement("dict");
        arrayOfCustomers.appendChild(customerDict);

        Element nameKey = doc.createElement("key");
        Element valueKey = doc.createElement("string");
        nameKey.appendChild(doc.createTextNode("name"));
        valueKey.appendChild(doc.createTextNode(local.getName()));
        customerDict.appendChild(nameKey);
        customerDict.appendChild(valueKey);

        nameKey = doc.createElement("key");
        valueKey = doc.createElement("string");
        nameKey.appendChild(doc.createTextNode("guid"));
        valueKey.appendChild(doc.createTextNode(local.getGuid()));
        customerDict.appendChild(nameKey);
        customerDict.appendChild(valueKey);

        nameKey = doc.createElement("key");
        valueKey = doc.createElement("date");
        nameKey.appendChild(doc.createTextNode("createDate"));
        valueKey.appendChild(doc.createTextNode(df.format(local.getCreateDate())));
        customerDict.appendChild(nameKey);
        customerDict.appendChild(valueKey);
      }
      resp.setContentType("Text/XML");
      transformer.transform(new DOMSource(doc), new StreamResult(resp.getWriter()));
    } catch (Exception e) {
      log.error("Error retrieving customer list", e);
      resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
    }
  }

}

I used the standard Java XML packages in this code snippet, but any other library can be used just as easily because the plist dtd is very straightforward.

  • + Share This
  • 🔖 Save To Your Account