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 SubmitStory 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 ResultSet rs = null; 75 76 sp = new ServletPrinter(response, "Submit Story"); 77 sp.printHTMLheader("RUBBoS: Story submission"); 78 sp.printHTML("<center><h2>Submit your incredible story !</h2><br>\n"); 79 sp 80 .printHTML("<form action=\"/rubbos/servlet/edu.rice.rubbos.servlets.StoreStory\" method=POST>\n" 81 + "<center><table>\n" 82 + "<tr><td><b>Nickname</b><td><input type=text size=20 name=nickname>\n" 83 + "<tr><td><b>Password</b><td><input type=text size=20 name=password>\n" 84 + "<tr><td><b>Story title</b><td><input type=text size=100 name=title>\n" 85 + "<tr><td><b>Category</b><td><SELECT name=category>\n"); 86 87 conn = getConnection(); 88 89 92 96 97 try 98 { 99 stmt = conn.prepareStatement("SELECT * FROM categories"); 100 rs = stmt.executeQuery(); 101 } 102 catch (Exception e) 103 { 104 sp.printHTML(" Failed to execute Query for SubmitStory: " + e); 105 closeConnection(stmt, conn); 106 return; 107 } 108 109 try 110 { 111 if (!rs.first()) 112 { 113 sp 114 .printHTML("<h3>ERROR: Sorry, but this story does not exist.</h3><br>"); 115 closeConnection(stmt, conn); 116 return; 117 } 118 119 121 String Name; 122 int Id; 123 do 124 { 125 Name = rs.getString("name"); 126 Id = rs.getInt("id"); 127 sp.printHTML("<OPTION value=\"" + Id + "\">" + Name + "</OPTION>\n"); 128 } 129 while (rs.next()); 130 } 131 catch (Exception e) 132 { 133 sp.printHTML("Exception accepting stories: " + e + "<br>"); 134 } 135 136 closeConnection(stmt, conn); 137 138 139 sp 140 .printHTML("</SELECT></table><p><br>\n" 141 + "<TEXTAREA rows=\"20\" cols=\"80\" name=\"body\">Write your story here</TEXTAREA><br><p>\n" 142 + "<input type=submit value=\"Submit this story now!\"></center><p>\n"); 143 sp.printHTMLfooter(); 144 145 } 146 147 public void doPost(HttpServletRequest request, HttpServletResponse response) 148 throws IOException , ServletException 149 { 150 doGet(request, response); 151 } 152 153 } 154 | Popular Tags |