1 package edu.rice.rubis.servlets; 2 3 import java.io.IOException ; 4 import java.sql.Connection ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 8 import javax.servlet.ServletException ; 9 import javax.servlet.http.HttpServletRequest ; 10 import javax.servlet.http.HttpServletResponse ; 11 12 13 public class BrowseRegions extends RubisHttpServlet 14 { 15 16 17 18 public int getPoolSize() 19 { 20 return Config.BrowseRegionsPoolSize; 21 } 22 23 26 private void closeConnection(PreparedStatement stmt, Connection conn) 27 { 28 try 29 { 30 if (stmt != null) 31 stmt.close(); if (conn != null) 33 releaseConnection(conn); 34 } 35 catch (Exception ignore) 36 { 37 } 38 } 39 40 43 private void regionList(ServletPrinter sp) 44 { 45 PreparedStatement stmt = null; 46 Connection conn = null; 47 String regionName; 48 ResultSet rs = null; 49 50 try 52 { 53 conn = getConnection(); 54 55 stmt = conn.prepareStatement("SELECT name, id FROM regions"); 56 rs = stmt.executeQuery(); 57 } 58 catch (Exception e) 59 { 60 sp.printHTML("Failed to executeQuery for the list of regions" + e); 61 closeConnection(stmt, conn); 62 return; 63 } 64 try 65 { 66 if (!rs.first()) 67 { 68 sp.printHTML( 69 "<h2>Sorry, but there is no region available at this time. Database table is empty</h2><br>"); 70 closeConnection(stmt, conn); 71 return; 72 } 73 else 74 sp.printHTML("<h2>Currently available regions</h2><br>"); 75 76 do 77 { 78 regionName = rs.getString("name"); 79 sp.printRegion(regionName); 80 } 81 while (rs.next()); 82 closeConnection(stmt, conn); 83 84 } 85 catch (Exception e) 86 { 87 sp.printHTML("Exception getting region list: " + e + "<br>"); 88 closeConnection(stmt, conn); 89 } 90 } 91 92 public void doGet(HttpServletRequest request, HttpServletResponse response) 93 throws IOException , ServletException 94 { 95 ServletPrinter sp = null; 96 sp = new ServletPrinter(response, "BrowseRegions"); 97 sp.printHTMLheader("RUBiS: Available regions"); 98 99 regionList(sp); 100 sp.printHTMLfooter(); 101 } 102 103 106 public void destroy() 107 { 108 super.destroy(); 109 } 110 111 } 112 | Popular Tags |