KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * RUBBoS: Rice University Bulletin Board System.
3  * Copyright (C) 2001-2004 Rice University and French National Institute For
4  * Research In Computer Science And Control (INRIA).
5  * Contact: jmob@objectweb.org
6  *
7  * This library is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as published by the
9  * Free Software Foundation; either version 2.1 of the License, or any later
10  * version.
11  *
12  * This library is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15  * for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this library; if not, write to the Free Software Foundation,
19  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20  *
21  * Initial developer(s): Emmanuel Cecchet.
22  * Contributor(s): Niraj Tolia.
23  */

24
25 package edu.rice.rubbos.servlets;
26
27 import java.io.IOException JavaDoc;
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31
32 import javax.servlet.ServletException JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpServletResponse JavaDoc;
35
36 /**
37  * Builds the html page with the list of all categories and provides links to
38  * browse all items in a category or items in a category for a given region
39  */

40 public class RegisterUser extends RubbosHttpServlet
41 {
42
43   public int getPoolSize()
44   {
45     return Config.BrowseCategoriesPoolSize;
46   }
47
48   private void closeConnection(PreparedStatement JavaDoc stmt, Connection JavaDoc conn)
49   {
50     try
51     {
52       if (stmt != null)
53         stmt.close(); // close statement
54
}
55     catch (Exception JavaDoc ignore)
56     {
57     }
58
59     try
60     {
61       if (conn != null)
62           releaseConnection(conn);
63     }
64     catch (Exception JavaDoc ignore)
65     {
66     }
67   }
68
69   /** Build the html page for the response */
70   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
71       throws IOException JavaDoc, ServletException JavaDoc
72   {
73
74     ServletPrinter sp = null;
75     PreparedStatement JavaDoc stmt = null;
76     Connection JavaDoc conn = null;
77
78     sp = new ServletPrinter(response, "RegisterUser");
79
80     String JavaDoc categoryName, nickname, firstname, lastname, email;
81     String JavaDoc password = null, creation_date = null;
82     int userId;
83     int access = 0, id = 0, rating = 0;
84     ResultSet JavaDoc 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     //Authenticate the user
126

127     try
128     {
129       stmt = conn.prepareStatement("SELECT * FROM users WHERE nickname=\""
130           + nickname + "\"");
131       rs = stmt.executeQuery();
132     }
133     catch (Exception JavaDoc 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 JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response)
196       throws IOException JavaDoc, ServletException JavaDoc
197   {
198     doGet(request, response);
199   }
200
201 }
202
Popular Tags