KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
21  * This servlets displays general information about the user loged in
22  * and about his current bids or items to sell.
23  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
24  * @version 1.0
25  */

26 public class AboutMe extends HttpServlet JavaDoc
27 {
28
29   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
30   {
31     sp.printHTMLheader("RUBiS ERROR: About me");
32     sp.printHTML("<h3>Your request has not been processed due to the following error :</h3><br>");
33     sp.printHTML(errorMsg);
34     sp.printHTMLfooter();
35   }
36
37
38   /**
39    * Call <code>doPost</code> method.
40    *
41    * @param request a <code>HttpServletRequest</code> value
42    * @param response a <code>HttpServletResponse</code> value
43    * @exception IOException if an error occurs
44    * @exception ServletException if an error occurs
45    */

46   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
47   {
48     doPost(request, response);
49   }
50
51
52   /**
53    * Check username and password and build the web page that display the information about
54    * the loged in user.
55    *
56    * @param request a <code>HttpServletRequest</code> value
57    * @param response a <code>HttpServletResponse</code> value
58    * @exception IOException if an error occurs
59    * @exception ServletException if an error occurs
60    */

61   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
62   {
63     ServletPrinter sp = null;
64     Context JavaDoc initialContext = null;
65     String JavaDoc password=null, username=null;
66     Integer JavaDoc userId=null;
67     
68     sp = new ServletPrinter(response, "About me");
69
70     username = request.getParameter("nickname");
71     password = request.getParameter("password");
72     // Authenticate the user
73
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
74     {
75       try
76       {
77         initialContext = new InitialContext JavaDoc();
78       }
79       catch (Exception JavaDoc e)
80       {
81         printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
82         return ;
83       }
84      }
85     else
86     {
87       printError("You must provide valid username and password.", sp);
88       return ;
89     }
90     sp.printHTMLheader("RUBiS: About Me");
91
92     TopicConnectionFactory JavaDoc topicFactory = null;
93     TopicConnection JavaDoc connection = null;
94     TopicSession JavaDoc session = null;
95     Topic JavaDoc topic = null;
96     String JavaDoc html;
97     try
98     {
99       // lookup the connection factory
100
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
101       // create a connection to the JMS provider
102
connection = topicFactory.createTopicConnection();
103       // lookup the destination
104
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicAboutMe");
105       // create a session
106
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
107
}
108     catch (Exception JavaDoc e)
109     {
110       sp.printHTML("Cannot connect to message bean MDB_AboutMe : " +e+"<br>");
111       return ;
112     }
113     try
114     {
115       // create a requestor to receive the reply
116
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
117       // create a message
118
MapMessage JavaDoc message = session.createMapMessage();
119       // set parameters
120
message.setString("username", username);
121       message.setString("password", password);
122       message.setJMSCorrelationID("aboutMe");
123       // send the message and receive the reply
124
connection.start(); // allows message to be delivered (default is connection stopped)
125
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
126       connection.stop();
127       // read the reply
128
html = reply.getText();
129       // close connection and session
130
requestor.close(); // also close the session
131
connection.close();
132     }
133     catch (Exception JavaDoc e)
134     {
135       sp.printHTML("Cannot retrieve information about you: " +e+"<br>");
136       return ;
137     }
138     sp.printHTML(html);
139     sp.printHTMLfooter();
140   }
141
142 }
143
Popular Tags