KickJava   Java API By Example, From Geeks To Geeks.

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


1 package edu.rice.rubis.beans.servlets;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.jms.MapMessage JavaDoc;
6 import javax.jms.Session JavaDoc;
7 import javax.jms.TextMessage JavaDoc;
8 import javax.jms.Topic JavaDoc;
9 import javax.jms.TopicConnection JavaDoc;
10 import javax.jms.TopicConnectionFactory JavaDoc;
11 import javax.jms.TopicRequestor JavaDoc;
12 import javax.jms.TopicSession JavaDoc;
13 import javax.naming.Context JavaDoc;
14 import javax.naming.InitialContext JavaDoc;
15 import javax.servlet.ServletException JavaDoc;
16 import javax.servlet.http.HttpServlet JavaDoc;
17 import javax.servlet.http.HttpServletRequest JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19
20 /** This servlet register a new user in the database and display
21  * the result of the transaction.
22  * It must be called this way :
23  * <pre>
24  * http://..../RegisterUser?firstname=aa&lastname=bb&nickname=cc&email=dd&password=ee&region=ff
25  * where: aa is the user first name
26  * bb is the user last name
27  * cc is the user nick name (login name)
28  * dd is the email address of the user
29  * ee is the user password
30  * ff is the identifier of the region where the user lives
31  * </pre>
32  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
33  * @version 1.0
34  */

35
36 public class RegisterUser extends HttpServlet JavaDoc
37 {
38
39   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
40   {
41     sp.printHTMLheader("RUBiS ERROR: Register user");
42     sp.printHTML("<h2>Your registration has not been processed due to the following error :</h2><br>");
43     sp.printHTML(errorMsg);
44     sp.printHTMLfooter();
45   }
46
47   /**
48    * Describe <code>doGet</code> method here.
49    *
50    * @param request a <code>HttpServletRequest</code> value
51    * @param response a <code>HttpServletResponse</code> value
52    * @exception IOException if an error occurs
53    * @exception ServletException if an error occurs
54    */

55   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
56   {
57     ServletPrinter sp = null;
58     String JavaDoc firstname=null, lastname=null, nickname=null, email=null, password=null;
59     int regionId = 0;
60     int userId;
61     String JavaDoc creationDate, regionName;
62
63     sp = new ServletPrinter(response, "RegisterUser");
64       
65     Context JavaDoc initialContext = null;
66     try
67     {
68       initialContext = new InitialContext JavaDoc();
69     }
70     catch (Exception JavaDoc e)
71     {
72       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
73       return ;
74     }
75
76     String JavaDoc value = request.getParameter("firstname");
77     if ((value == null) || (value.equals("")))
78     {
79       printError("You must provide a first name!<br>", sp);
80       return ;
81     }
82     else
83       firstname = value;
84
85     value = request.getParameter("lastname");
86     if ((value == null) || (value.equals("")))
87     {
88       printError("You must provide a last name!<br>", sp);
89       return ;
90     }
91     else
92       lastname = value;
93
94     value = request.getParameter("nickname");
95     if ((value == null) || (value.equals("")))
96     {
97       printError("You must provide a nick name!<br>", sp);
98       return ;
99     }
100     else
101       nickname = value;
102
103     value = request.getParameter("email");
104     if ((value == null) || (value.equals("")))
105     {
106       printError("You must provide an email address!<br>", sp);
107       return ;
108     }
109     else
110       email = value;
111
112     value = request.getParameter("password");
113     if ((value == null) || (value.equals("")))
114     {
115       printError("You must provide a password!<br>", sp);
116       return ;
117     }
118     else
119       password = value;
120
121
122     value = request.getParameter("region");
123     if ((value == null) || (value.equals("")))
124     {
125       printError("You must provide a valid region!<br>", sp);
126       return ;
127     }
128     else
129       regionName = value;
130
131     // Try to create a new user
132
TopicConnectionFactory JavaDoc topicFactory = null;
133     TopicConnection JavaDoc connection = null;
134     TopicSession JavaDoc session = null;
135     Topic JavaDoc topic = null;
136     String JavaDoc html;
137     try
138     {
139       // lookup the connection factory
140
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
141       // create a connection to the JMS provider
142
connection = topicFactory.createTopicConnection();
143       // lookup the destination
144
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicRegisterUser");
145       // create a session
146
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
147
}
148     catch (Exception JavaDoc e)
149     {
150       sp.printHTML("Cannot connect to message bean MDB_RegisterUser : " +e+"<br>");
151       return ;
152     }
153     try
154     {
155       // create a requestor to receive the reply
156
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
157       // create a message
158
MapMessage JavaDoc message = session.createMapMessage();
159       // set parameters
160
message.setString("firstname", firstname);
161       message.setString("lastname", lastname);
162       message.setString("nickname", nickname);
163       message.setString("email", email);
164       message.setString("password", password);
165       message.setString("region", regionName);
166       message.setJMSCorrelationID("registerUser");
167       // send the message and receive the reply
168
connection.start(); // allows message to be delivered (default is connection stopped)
169
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
170       connection.stop();
171       // read the reply
172
html = reply.getText();
173       // close connection and session
174
requestor.close(); // also close the session
175
connection.close();
176     }
177     catch (Exception JavaDoc e)
178     {
179       sp.printHTML("User registration failed: " +e+"<br>");
180       return ;
181     }
182     sp.printHTMLheader("RUBiS: Welcome to "+nickname);
183
184     sp.printHTML("<h2>Your registration has been processed successfully</h2><br>\n");
185     sp.printHTML("<h3>Welcome "+nickname+"</h3>\n");
186     sp.printHTML("RUBiS has stored the following information about you:<br>\n");
187     sp.printHTML("First Name : "+firstname+"<br>\n");
188     sp.printHTML("Last Name : "+lastname+"<br>\n");
189     sp.printHTML("Nick Name : "+nickname+"<br>\n");
190     sp.printHTML("Email : "+email+"<br>\n");
191     sp.printHTML("Password : "+password+"<br>\n");
192     sp.printHTML("Region : "+regionName+"<br>\n");
193     sp.printHTML("<br>The following information has been automatically generated by RUBiS:<br>\n");
194     sp.printHTML(html);
195     sp.printHTMLfooter();
196
197   }
198     
199  
200   /**
201    * Call the <code>doGet</code> method here.
202    *
203    * @param request a <code>HttpServletRequest</code> value
204    * @param response a <code>HttpServletResponse</code> value
205    * @exception IOException if an error occurs
206    * @exception ServletException if an error occurs
207    */

208   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
209   {
210     doGet(request, response);
211   }
212 }
213
Popular Tags