KickJava   Java API By Example, From Geeks To Geeks.

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


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 servlets display the page allowing a user to put a bid
21  * on an item.
22  * It must be called this way :
23  * <pre>
24  * http://..../PutBid?itemId=xx&nickname=yy&password=zz
25  * where xx is the id of the item
26  * yy is the nick name of the user
27  * zz is the user password
28  * </pre>
29  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
30  * @version 1.0
31  */

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

54   public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
55   {
56     ServletPrinter sp = null;
57     String JavaDoc itemStr = request.getParameter("itemId");
58     String JavaDoc name = request.getParameter("nickname");
59     String JavaDoc pass = request.getParameter("password");
60     sp = new ServletPrinter(response, "PubBid");
61     
62     if ((itemStr == null) || (itemStr.equals("")) ||
63         (name == null) || (name.equals(""))||
64         (pass == null) || (pass.equals("")))
65     {
66       printError("Item id, name and password are required - Cannot process the request<br>", sp);
67       return ;
68     }
69     Integer JavaDoc itemId = new Integer JavaDoc(itemStr);
70
71     Context JavaDoc initialContext = null;
72     try
73     {
74       initialContext = new InitialContext JavaDoc();
75     }
76     catch (Exception JavaDoc e)
77     {
78       printError("Cannot get initial context for JNDI: " + e+"<br>", sp);
79       return ;
80     }
81
82     TopicConnectionFactory JavaDoc topicFactory = null;
83     TopicConnection JavaDoc connection = null;
84     TopicSession JavaDoc session = null;
85     Topic JavaDoc topic = null;
86     String JavaDoc html;
87     try
88     {
89       // lookup the connection factory
90
topicFactory = (TopicConnectionFactory JavaDoc)initialContext.lookup(Config.TopicConnectionFactoryName);
91       // create a connection to the JMS provider
92
connection = topicFactory.createTopicConnection();
93       // lookup the destination
94
topic = (Topic JavaDoc) initialContext.lookup(Config.PrefixTopicName+"topicPutBid");
95       // create a session
96
session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); // no transaction and auto ack
97
}
98     catch (Exception JavaDoc e)
99     {
100       sp.printHTML("Cannot connect to message bean MDB_PutBid : " +e+"<br>");
101       return ;
102     }
103     try
104     {
105       sp.printHTMLheader("RUBiS: Bidding service");
106       // create a requestor to receive the reply
107
TopicRequestor JavaDoc requestor = new TopicRequestor JavaDoc(session, topic);
108       // create a message
109
MapMessage JavaDoc message = session.createMapMessage();
110       // set parameters
111
message.setInt("itemId", itemId.intValue());
112       message.setString("username", name);
113       message.setString("password", pass);
114       message.setJMSCorrelationID("putBid");
115       // send the message and receive the reply
116
connection.start(); // allows message to be delivered (default is connection stopped)
117
TextMessage JavaDoc reply = (TextMessage JavaDoc)requestor.request(message);
118       connection.stop();
119       // read the reply
120
html = reply.getText();
121       // close connection and session
122
requestor.close(); // also close the session
123
connection.close();
124     }
125     catch (Exception JavaDoc e)
126     {
127       sp.printHTML("Bidding process failed: " +e+"<br>");
128       return ;
129     }
130     // Display the form for bidding
131
sp.printHTML(html);
132     sp.printHTMLfooter();
133   
134   }
135
136   /**
137    * Call the <code>doGet</code> method.
138    *
139    * @param request a <code>HttpServletRequest</code> value
140    * @param response a <code>HttpServletResponse</code> value
141    * @exception IOException if an error occurs
142    * @exception ServletException if an error occurs
143    */

144   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
145   {
146     doGet(request, response);
147   }
148 }
149
Popular Tags