KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > servlets > RegisterUser


1 package edu.rice.rubis.servlets;
2
3 import java.io.IOException JavaDoc;
4 import java.sql.Connection JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import javax.servlet.ServletException JavaDoc;
10 import javax.servlet.http.HttpServletRequest JavaDoc;
11 import javax.servlet.http.HttpServletResponse JavaDoc;
12 import javax.transaction.UserTransaction JavaDoc;
13
14 /**
15  * Add a new user in the database
16  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
17  * @version 1.0
18  */

19 public class RegisterUser extends RubisHttpServlet
20 {
21   private UserTransaction JavaDoc utx = null;
22   
23
24   public int getPoolSize()
25   {
26     return Config.RegisterUserPoolSize;
27   }
28
29 /**
30  * Close both statement and connection.
31  */

32   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
33   {
34     try
35     {
36       if (stmt != null)
37         stmt.close(); // close statement
38
if (conn != null)
39         releaseConnection(conn);
40     }
41     catch (Exception JavaDoc ignore)
42     {
43     }
44   }
45
46 /**
47  * Display an error message.
48  * @param errorMsg the error message value
49  */

50   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
51   {
52     sp.printHTMLheader("RUBiS ERROR: Register user");
53     sp.printHTML(
54       "<h2>Your registration has not been processed due to the following error :</h2><br>");
55     sp.printHTML(errorMsg);
56     sp.printHTMLfooter();
57
58
59   }
60
61   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
62     throws IOException JavaDoc, ServletException JavaDoc
63   {
64     PreparedStatement JavaDoc stmt = null;
65     Connection JavaDoc conn = null;
66     
67     String JavaDoc firstname = null,
68       lastname = null,
69       nickname = null,
70       email = null,
71       password = null;
72     int regionId;
73     int userId;
74     String JavaDoc creationDate, region;
75
76     ServletPrinter sp = null;
77     sp = new ServletPrinter(response, "RegisterUser");
78
79     String JavaDoc value = request.getParameter("firstname");
80     if ((value == null) || (value.equals("")))
81     {
82       printError("You must provide a first name!<br>", sp);
83       return;
84     }
85     else
86       firstname = value;
87
88     value = request.getParameter("lastname");
89     if ((value == null) || (value.equals("")))
90     {
91       printError("You must provide a last name!<br>", sp);
92       return;
93     }
94     else
95       lastname = value;
96
97     value = request.getParameter("nickname");
98     if ((value == null) || (value.equals("")))
99     {
100       printError("You must provide a nick name!<br>", sp);
101       return;
102     }
103     else
104       nickname = value;
105
106     value = request.getParameter("email");
107     if ((value == null) || (value.equals("")))
108     {
109       printError("You must provide an email address!<br>", sp);
110       return;
111     }
112     else
113       email = value;
114
115     value = request.getParameter("password");
116     if ((value == null) || (value.equals("")))
117     {
118       printError("You must provide a password!<br>", sp);
119       return;
120     }
121     else
122       password = value;
123
124     value = request.getParameter("region");
125     if ((value == null) || (value.equals("")))
126     {
127       printError("You must provide a valid region!<br>", sp);
128       return;
129     }
130     else
131     {
132       region = value;
133       
134       try
135       {
136         conn = getConnection();
137         stmt = conn.prepareStatement("SELECT id FROM regions WHERE name=?");
138         stmt.setString(1, region);
139         ResultSet JavaDoc rs = stmt.executeQuery();
140         if (!rs.first())
141         {
142           printError(
143             " Region " + value + " does not exist in the database!<br>", sp);
144             closeConnection(stmt, conn);
145           return;
146         }
147         regionId = rs.getInt("id");
148         stmt.close();
149       }
150       catch (SQLException JavaDoc e)
151       {
152         printError("Failed to execute Query for region: " + e, sp);
153         closeConnection(stmt, conn);
154         return;
155       }
156     }
157     // Try to create a new user
158
try
159     {
160       stmt =
161         conn.prepareStatement("SELECT nickname FROM users WHERE nickname=?");
162       stmt.setString(1, nickname);
163       ResultSet JavaDoc rs = stmt.executeQuery();
164       if (rs.first())
165       {
166         printError("The nickname you have choosen is already taken by someone else. Please choose a new nickname.<br>", sp);
167         closeConnection(stmt, conn);
168         return;
169       }
170       stmt.close();
171     }
172     catch (SQLException JavaDoc e)
173     {
174       printError("Failed to execute Query to check the nickname: " + e, sp);
175       closeConnection(stmt, conn);
176       return;
177     }
178     try
179     {
180       String JavaDoc now = TimeManagement.currentDateToString();
181       stmt =
182         conn.prepareStatement(
183           "INSERT INTO users VALUES (NULL, \""
184             + firstname
185             + "\", \""
186             + lastname
187             + "\", \""
188             + nickname
189             + "\", \""
190             + password
191             + "\", \""
192             + email
193             + "\", 0, 0,\""
194             + now
195             + "\", "
196             + regionId
197             + ")");
198       stmt.executeUpdate();
199       stmt.close();
200     }
201     catch (SQLException JavaDoc e)
202     {
203       printError(
204         "RUBiS internal error: User registration failed (got exception: "
205           + e
206           + ")<br>", sp);
207       closeConnection(stmt, conn);
208       return;
209     }
210     try
211     {
212       stmt =
213         conn.prepareStatement(
214           "SELECT id, creation_date FROM users WHERE nickname=?");
215       stmt.setString(1, nickname);
216       ResultSet JavaDoc urs = stmt.executeQuery();
217       if (!urs.first())
218       {
219         printError("This user does not exist in the database.", sp);
220         closeConnection(stmt, conn);
221         return;
222       }
223       userId = urs.getInt("id");
224       creationDate = urs.getString("creation_date");
225     }
226     catch (SQLException JavaDoc e)
227     {
228       printError("Failed to execute Query for user: " + e, sp);
229       closeConnection(stmt, conn);
230       return;
231     }
232
233
234     sp.printHTMLheader("RUBiS: Welcome to " + nickname);
235     sp.printHTML(
236       "<h2>Your registration has been processed successfully</h2><br>");
237     sp.printHTML("<h3>Welcome " + nickname + "</h3>");
238     sp.printHTML("RUBiS has stored the following information about you:<br>");
239     sp.printHTML("First Name : " + firstname + "<br>");
240     sp.printHTML("Last Name : " + lastname + "<br>");
241     sp.printHTML("Nick Name : " + nickname + "<br>");
242     sp.printHTML("Email : " + email + "<br>");
243     sp.printHTML("Password : " + password + "<br>");
244     sp.printHTML("Region : " + region + "<br>");
245     sp.printHTML(
246       "<br>The following information has been automatically generated by RUBiS:<br>");
247     sp.printHTML("User id :" + userId + "<br>");
248     sp.printHTML("Creation date :" + creationDate + "<br>");
249
250     sp.printHTMLfooter();
251     closeConnection(stmt, conn);
252   }
253
254   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
255     throws IOException JavaDoc, ServletException JavaDoc
256   {
257     doGet(request, response);
258   }
259
260   /**
261    * Clean up the connection pool.
262    */

263   public void destroy()
264   {
265     super.destroy();
266   }
267 }
268
Popular Tags