1 24 25 package edu.rice.rubbos.servlets; 26 27 import java.io.IOException ; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.ResultSet ; 31 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 36 public class StoriesOfTheDay extends RubbosHttpServlet 37 { 38 39 public int getPoolSize() 40 { 41 return Config.BrowseCategoriesPoolSize; 42 } 43 44 private void closeConnection(PreparedStatement stmt, Connection conn) 45 { 46 try 47 { 48 if (stmt != null) 49 stmt.close(); } 51 catch (Exception ignore) 52 { 53 } 54 55 try 56 { 57 if (conn != null) 58 releaseConnection(conn); 59 } 60 catch (Exception ignore) 61 { 62 } 63 64 } 65 66 67 public void doGet(HttpServletRequest request, HttpServletResponse response) 68 throws IOException , ServletException 69 { 70 71 ServletPrinter sp = null; 72 PreparedStatement stmt = null; 73 Connection conn = null; 74 75 int bodySizeLimit = 512; 76 ResultSet rs = null; 77 78 sp = new ServletPrinter(response, "StoriesOfTheDay"); 79 sp.printHTMLheader("RUBBoS stories of the day"); 80 81 conn = getConnection(); 82 83 try 84 { 85 stmt = conn 86 .prepareStatement("SELECT * FROM stories ORDER BY date DESC LIMIT 10"); 87 rs = stmt.executeQuery(); 88 } 89 catch (Exception e) 90 { 91 sp.printHTML("Failed to execute Query for stories of the day: " + e); 92 closeConnection(stmt, conn); 93 return; 94 } 95 try 96 { 97 if (!rs.first()) 98 { 99 sp 100 .printHTML("<h2>Sorry, but there is no story available at this time.</h2><br>\n"); 101 closeConnection(stmt, conn); 102 return; 103 } 104 105 int storyId; 106 String storyTitle; 107 int writerId; 108 String userName; 109 String date; 110 String body; 111 do 112 { 113 sp.printHTML("<br><hr>\n"); 114 storyId = rs.getInt("id"); 115 storyTitle = rs.getString("title"); 116 sp 117 .printHTMLHighlighted("<a HREF=\"/rubbos/servlet/edu.rice.rubbos.servlets.ViewStory?storyId=" 118 + storyId + "\">" + storyTitle + "</a>"); 119 120 writerId = rs.getInt("writer"); 121 userName = sp.getUserName(writerId, conn); 122 date = rs.getString("date"); 123 sp.printHTML("<B>Posted by " + userName + " on " + date + "</B><br>\n"); 124 body = rs.getString("body"); 125 if (body.length() > bodySizeLimit) 126 { 127 sp.printHTML(body.substring(0, bodySizeLimit)); 128 sp.printHTML("<br><B>...</B>"); 129 } 130 else 131 sp.printHTML(body); 132 sp.printHTML("<br>\n"); 133 } 134 while (rs.next()); 135 } 136 catch (Exception e) 137 { 138 sp.printHTML("Exception getting stories of the day: " + e + "<br>"); 139 } 140 141 closeConnection(stmt, conn); 142 143 144 sp.printHTMLfooter(); 145 146 } 147 148 public void doPost(HttpServletRequest request, HttpServletResponse response) 149 throws IOException , ServletException 150 { 151 doGet(request, response); 152 } 153 154 } 155 | Popular Tags |