package readingPhoneBook; //Copyright (c) 2009 Anatoly S. Krivitsky, PhD. //All rights reserved. //Conditional permission for free use of the code //As soon as above copyright notes are mentioned, //the author grants a permission to any person or organization //to use, distribute and publish this code for free //Questions and comments may be directed to the author at //akrivitsky@yahoo.com and akrivitsky@usa.com //Disclaimer of Liability //The user assumes all responsibility //and risk for the use of this code "as is". //There is no warranty of any kind associated with the code. //Under no circumstances, including negligence, shall the author be liable //for any DIRECT, INDIRECT, INCIDENTAL, SPECIAL or CONSEQUENTIAL DAMAGES, //or LOST PROFITS that result from the use or inability to use the code. //Nor shall the author be liable for any possible damages related to the code including, //but not limited to, reliance by any person on any information obtained with the code //File name is readingPhoneBook.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.sql.*; import java.util.LinkedHashMap; import java.util.Map; import javax.sql.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.json.simple.JSONValue; public class readingPhoneBook extends HttpServlet { DataSource poolExtJS; Connection connExtJS = null; private static Log logExtJS = LogFactory.getLog(readingPhoneBook.class); public void init() throws ServletException { Context envExtJS = null; try{ envExtJS = (Context) new InitialContext().lookup("java:comp/env"); poolExtJS = (DataSource) envExtJS.lookup("jdbc/mysql"); if (poolExtJS == null) { // Replace three statements below with your own handling of "unknown DataSource" situation here as needed System.out.println("in init() jdbc/mysql is is an unknown DataSource"); logExtJS.error("in init() jdbc/mysql is an unknown DataSource"); throw new ServletException("'jdbc/mysql' is an unknown DataSource"); } } catch (NamingException neExtJS) { throw new ServletException(neExtJS); } } public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { res.setContentType("text/html"); PrintWriter outExtJS = res.getWriter(); try{ connExtJS = poolExtJS.getConnection(); CallableStatement stExtJS = connExtJS.prepareCall("{call read_phonebook()}"); stExtJS.execute(); ResultSet rsExtJS = stExtJS.getResultSet(); int i = 0; outExtJS.print("["); while(rsExtJS.next()) { i++; if (i != 1) outExtJS.print(","); Map obj1=new LinkedHashMap(); obj1.put("id", (new Integer(rsExtJS.getInt(1))).toString()); obj1.put("FirstName", rsExtJS.getString(2)); obj1.put("LastName", rsExtJS.getString(3)); obj1.put("MiddleName", rsExtJS.getString(4)); obj1.put("Phone", rsExtJS.getString(5)); obj1.put("WhatKindOfPhone", rsExtJS.getString(6)); obj1.put("Country", rsExtJS.getString(7)); obj1.put("Description", rsExtJS.getString(8)); StringWriter out1 = new StringWriter(); JSONValue.writeJSONString(obj1, out1); String jsonTextForExtJS = out1.toString(); outExtJS.print(jsonTextForExtJS); } outExtJS.print("]"); outExtJS.flush(); } catch (Exception eJson) { // Replace two statements below with your own logic for exception handling here as needed System.out.println("exception eJson while preparing JSON = " + eJson.toString() + " eJson message = " + eJson.getMessage()); eJson.printStackTrace(System.out); } finally { try { if (connExtJS != null) connExtJS.close(); } catch (Exception ignored) { // put our own logic for exception handling here as needed } } } }