AjaxResponseServletpublic class AjaxResponseServlet extends HttpServlet
Fields Summary |
---|
private static final long | serialVersionUID |
Methods Summary |
---|
public java.lang.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 java.lang.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();
| public void | doGet(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("?");
}
|
|