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 StoreComment 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 String nickname, password, storyId, parent, userIdstring, subject, body; 76 String comment_table; 77 int page = 0, nbOfStories = 0, userId; 78 int updateResult; 79 80 sp = new ServletPrinter(response, "StoreComment"); 81 82 nickname = request.getParameter("nickname"); 83 password = request.getParameter("password"); 84 storyId = request.getParameter("storyId"); 85 parent = request.getParameter("parent"); 86 subject = request.getParameter("subject"); 87 body = request.getParameter("body"); 88 comment_table = request.getParameter("comment_table"); 89 90 if (nickname == null) 91 { 92 nickname = request.getParameter("nickname"); 93 } 94 95 if (password == null) 96 { 97 password = request.getParameter("password"); 98 } 99 100 if (storyId == null) 101 { 102 sp.printHTML("StoreComment, You must provide a story identifier!<br>"); 103 return; 104 } 105 106 if (parent == null) 107 { 108 sp 109 .printHTML("StoreComment, You must provide a follow up identifier!<br>"); 110 return; 111 } 112 113 if (subject == null) 114 { 115 sp.printHTML("StoreComment, You must provide a comment subject!<br>"); 116 return; 117 } 118 119 if (body == null) 120 { 121 sp 122 .printHTML("StoreComment, <h3>You must provide a comment body!<br></h3>"); 123 return; 124 } 125 126 if (comment_table == null) 127 { 128 sp.printHTML("Viewing comment, You must provide a comment table!<br>"); 129 return; 130 } 131 132 sp.printHTMLheader("RUBBoS: Comment submission result"); 133 134 sp.printHTML("<center><h2>Comment submission result:</h2></center><p>\n"); 135 136 conn = getConnection(); 137 138 userIdstring = sp.authenticate(nickname, password, conn); 140 userId = (Integer.valueOf(userIdstring)).intValue(); 141 142 if (userId == 0) 143 sp.printHTML("Comment posted by the 'Anonymous Coward'<br>\n"); 144 else 145 sp.printHTML("Comment posted by user #" + userId + "<br>\n"); 146 147 149 try 150 { 151 stmt = conn.prepareStatement("INSERT INTO " + comment_table 152 + " VALUES (NULL, " + userId + ", " + storyId + ", " + parent 153 + ", 0, 0, NOW(), \"" + subject + "\", \"" + body + "\")"); 154 updateResult = stmt.executeUpdate(); 155 156 stmt.close(); 157 158 stmt = conn.prepareStatement("UPDATE " + comment_table 159 + " SET childs=childs+1 WHERE id=" + parent); 160 updateResult = stmt.executeUpdate(); 161 } 162 catch (Exception e) 163 { 164 closeConnection(stmt, conn); 165 sp.printHTML("Exception getting categories: " + e + "<br>"); 166 return; 167 } 168 169 closeConnection(stmt, conn); 170 171 sp.printHTML("Your comment has been successfully stored in the " 172 + comment_table + " database table<br>\n"); 173 sp.printHTMLfooter(); 174 175 } 176 177 public void doPost(HttpServletRequest request, HttpServletResponse response) 178 throws IOException , ServletException 179 { 180 doGet(request, response); 181 } 182 183 } 184 | Popular Tags |