FileDocCategorySizeDatePackage
AjaxResponseServlet.javaAPI DocExample2699Wed May 10 09:44:06 BST 2006com.oreilly.ajax.servlet

AjaxResponseServlet

public class AjaxResponseServlet extends HttpServlet

Fields Summary
private static final long
serialVersionUID
Constructors Summary
Methods Summary
public java.lang.StringcreateJSONwithJSONsimple(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 java.lang.StringcreateStringBufferJSON(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();
  
public voiddoGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse res)


       
        
  
    // key is the parameter passed in from the javascript
    // variable named url (see index.html)
    String key = req.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
      res.setContentType("text/xml");
      res.setHeader("Cache-Control", "no-cache");
      // write out the xml string
      String outString = createJSONwithJSONsimple(keyInt);
      res.getWriter().write(outString);
    }
    else
    {
      // If key comes back as a null, return a question mark.
      res.setContentType("text/xml");
      res.setHeader("Cache-Control", "no-cache");
      res.getWriter().write("?");
    }