AJAX Tricks and Prototype
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package edu.sc.cse; import java.io.*; import java.net.*; import javax.servlet.*; import javax.servlet.http.*; import org.jdom.Document; import org.jdom.Element; import org.jdom.output.XMLOutputter; /** * * @author jmvidal */ public class AjaxResponseServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // key is the parameter passed in from the javascript // variable named url (see index.html) String key = request.getParameter("key"); StringBuffer returnXML = null; if (key != null) { // extract the first character from key // as an int, then convert that int to a String int keyInt = key.charAt(0); // setup the response response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); //Two ways of generating XML //String output = createStringBufferXML(keyInt); // String output = createJdomXML(keyInt); String output = createDom4jXML(keyInt); // write out the xml string response.getWriter().write(output); } else { // If key comes back as a null, return a question mark. response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); response.getWriter().write("?"); } } public String createStringBufferXML(int keyInt) { StringBuffer returnXML = null; returnXML = new StringBuffer("\r\n<converted-values>"); returnXML.append("\r\n<decimal>" + Integer.toString(keyInt) + "</decimal>"); returnXML.append("\r\n<hexadecimal>0x" + Integer.toString(keyInt, 16) + "</hexadecimal>"); returnXML.append("\r\n<octal>0" + Integer.toString(keyInt, 8) + "</octal>"); returnXML.append("\r\n<hyper>&0x" + Integer.toString(keyInt, 16) + ";</hyper>"); returnXML.append("\r\n<binary>" + Integer.toString(keyInt, 2) + "B</binary>"); returnXML.append("\r\n</converted-values>"); return returnXML.toString(); } public String createJdomXML(int key) throws IOException { Document document = new Document(); // create root node Element root = new org.jdom.Element("converted-values"); document.setRootElement(root); // create your a node org.jdom.Element element = new org.jdom.Element("decimal"); // add content to the node element.addContent(Integer.toString(key)); // add your node to root root.addContent(element); element = new org.jdom.Element("hexadecimal"); element.addContent("0x" + Integer.toString(key, 16)); root.addContent(element); element = new org.jdom.Element("octal"); element.addContent("0" + Integer.toString(key, 8)); root.addContent(element); element = new org.jdom.Element("hyper"); element.addContent("&0x" + Integer.toString(key, 16)); root.addContent(element); element = new org.jdom.Element("binary"); element.addContent(Integer.toString(key, 2) + "B"); root.addContent(element); // output JDOM document as a String of bytes XMLOutputter outputter = new XMLOutputter(); return outputter.outputString(document); } public String createDom4jXML(int key) throws IOException { org.dom4j.Document document = org.dom4j.DocumentHelper.createDocument(); org.dom4j.Element root = document.addElement("converted-values"); org.dom4j.Element element = root.addElement("decimal").addText( Integer.toString(key)); element = root.addElement("hexadecimal").addText( "0x" + Integer.toString(key, 16)); element = root.addElement("octal").addText("0" + Integer.toString(key, 8)); element = root.addElement("hyper").addText( "&0x" + Integer.toString(key, 16)); element = root.addElement("binary").addText(Integer.toString(key, 2) + "B"); StringBuffer xmlDoc = null; StringWriter sw = new StringWriter(); org.dom4j.io.XMLWriter writer = new org.dom4j.io.XMLWriter(sw); writer.write(document); writer.close(); xmlDoc = sw.getBuffer(); return xmlDoc.toString(); } // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /** * Handles the HTTP <code>GET</code> method. * @param request servlet request * @param response servlet response */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Handles the HTTP <code>POST</code> method. * @param request servlet request * @param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } /** * Returns a short description of the servlet. */ public String getServletInfo() { return "Short description"; } // </editor-fold> }
4 of 16