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.json.simple.JSONObject; /** * * @author jmvidal */ public class AjaxResponseServlet extends HttpServlet { /** * 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"); 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"); // write out the xml string String outString = createJSONwithJSONsimple(keyInt); response.getWriter().write(outString); } 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 createJSONwithJSONsimple(int keyInt) { JSONObject obj = new JSONObject(); JSONObject obj2 = new JSONObject(); obj2.put("decimal", Integer.toString(keyInt)); obj2.put("hexadecimal", Integer.toString(keyInt, 16)); obj2.put("octal", Integer.toString(keyInt, 8)); obj2.put("hyper", "&0x" + Integer.toString(keyInt, 16)); obj2.put("binary", Integer.toString(keyInt, 2) + "B"); obj.put("conversion", obj2); return (obj.toString()); } public String createStringBufferJSON(int keyInt) { StringBuffer returnJSON = new StringBuffer("\r\n{\"conversion\":{"); returnJSON.append("\r\n\"decimal\": \"" + Integer.toString(keyInt) + "\","); returnJSON.append("\r\n\"hexadecimal\": \"" + Integer.toString(keyInt, 16) + "\","); returnJSON.append("\r\n\"octal\": \"" + Integer.toString(keyInt, 8) + "\","); returnJSON.append("\r\n\"hyper\": \"&0x" + Integer.toString(keyInt, 16) + "\","); returnJSON.append("\r\n\"binary\": \"" + Integer.toString(keyInt, 2) + "B\""); returnJSON.append("\r\n}}"); return returnJSON.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> }
5 of 16