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 40 public class RegisterUser extends RubbosHttpServlet 41 { 42 43 public int getPoolSize() 44 { 45 return Config.BrowseCategoriesPoolSize; 46 } 47 48 private void closeConnection(PreparedStatement stmt, Connection conn) 49 { 50 try 51 { 52 if (stmt != null) 53 stmt.close(); } 55 catch (Exception ignore) 56 { 57 } 58 59 try 60 { 61 if (conn != null) 62 releaseConnection(conn); 63 } 64 catch (Exception ignore) 65 { 66 } 67 } 68 69 70 public void doGet(HttpServletRequest request, HttpServletResponse response) 71 throws IOException , ServletException 72 { 73 74 ServletPrinter sp = null; 75 PreparedStatement stmt = null; 76 Connection conn = null; 77 78 sp = new ServletPrinter(response, "RegisterUser"); 79 80 String categoryName, nickname, firstname, lastname, email; 81 String password = null, creation_date = null; 82 int userId; 83 int access = 0, id = 0, rating = 0; 84 ResultSet rs = null; 85 int updateResult; 86 87 firstname = request.getParameter("firstname"); 88 lastname = request.getParameter("lastname"); 89 nickname = request.getParameter("nickname"); 90 email = request.getParameter("email"); 91 password = request.getParameter("password"); 92 93 if (firstname == null) 94 { 95 sp.printHTML("You must provide a story title!<br>"); 96 return; 97 } 98 99 if (lastname == null) 100 { 101 sp.printHTML("<h3>You must provide a story body!<br></h3>"); 102 return; 103 } 104 105 if (nickname == null) 106 { 107 sp.printHTML("<h3>You must provide a category!<br></h3>"); 108 return; 109 } 110 111 if (email == null) 112 { 113 sp.printHTML("<h3>You must provide an email address!<br></h3>"); 114 return; 115 } 116 117 if (password == null) 118 { 119 sp.printHTML("<h3>You must provide a password!<br></h3>"); 120 return; 121 } 122 123 conn = getConnection(); 124 125 127 try 128 { 129 stmt = conn.prepareStatement("SELECT * FROM users WHERE nickname=\"" 130 + nickname + "\""); 131 rs = stmt.executeQuery(); 132 } 133 catch (Exception e) 134 { 135 sp.printHTML("ERROR: Nickname query failed" + e); 136 closeConnection(stmt, conn); 137 return; 138 } 139 try 140 { 141 if (rs.first()) 142 { 143 sp 144 .printHTML("The nickname you have chosen is already taken by someone else. Please choose a new nickname.<br>\n"); 145 closeConnection(stmt, conn); 146 return; 147 } 148 stmt = conn.prepareStatement("INSERT INTO users VALUES (NULL, \"" 149 + firstname + "\", \"" + lastname + "\", \"" + nickname + "\", \"" 150 + password + "\", \"" + email + "\", 0, 0, NOW())"); 151 updateResult = stmt.executeUpdate(); 152 stmt.close(); 153 154 stmt = conn.prepareStatement("SELECT * FROM users WHERE nickname=\"" 155 + nickname + "\""); 156 rs = stmt.executeQuery(); 157 158 rs.first(); 159 id = rs.getInt("id"); 160 creation_date = rs.getString("creation_date"); 161 rating = rs.getInt("rating"); 162 access = rs.getInt("access"); 163 164 } 165 catch (Exception e) 166 { 167 sp.printHTML("Exception registering user " + e + "<br>"); 168 closeConnection(stmt, conn); 169 return; 170 } 171 172 closeConnection(stmt, conn); 173 174 sp 175 .printHTML("<h2>Your registration has been processed successfully</h2><br>\n"); 176 sp.printHTML("<h3>Welcome " + nickname + "</h3>\n"); 177 sp 178 .printHTML("RUBBoS has stored the following information about you:<br>\n"); 179 sp.printHTML("First Name : " + firstname + "<br>\n"); 180 sp.printHTML("Last Name : " + lastname + "<br>\n"); 181 sp.printHTML("Nick Name : " + nickname + "<br>\n"); 182 sp.printHTML("Email : " + email + "<br>\n"); 183 sp.printHTML("Password : " + password + "<br>\n"); 184 sp 185 .printHTML("<br>The following information has been automatically generated by RUBBoS:<br>\n"); 186 sp.printHTML("User id :" + id + "<br>\n"); 187 sp.printHTML("Creation date :" + creation_date + "<br>\n"); 188 sp.printHTML("Rating :" + rating + "<br>\n"); 189 sp.printHTML("Access :" + access + "<br>\n"); 190 191 sp.printHTMLfooter(); 192 193 } 194 195 public void doPost(HttpServletRequest request, HttpServletResponse response) 196 throws IOException , ServletException 197 { 198 doGet(request, response); 199 } 200 201 } 202 | Popular Tags |