KickJava   Java API By Example, From Geeks To Geeks.

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


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 displays the full description of a given item
21  * and allows the user to bid on this item.
22  * It must be called this way :
23  * <pre>
24  * http://..../ViewItem?itemId=xx where xx is the id of the item
25  * </pre>
26  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
27  * @version 1.0
28  */

29
30
31 public class ViewItem extends HttpServlet JavaDoc
32 {
33
34   private void printError(String JavaDoc errorMsg, ServletPrinter sp)
35   {
36     sp.printHTMLheader("RUBiS ERROR: View item");
37     sp.printHTML("<h2>We cannot process your request due to the following error :</h2><br>");
38     sp.printHTML(errorMsg);
39     sp.printHTMLfooter();
40   }
41
42   /**
43    * Display all available information on an item.
44    *
45    * @param request a <code>HttpServletRequest</code> value
46    * @param response a <code>HttpServletResponse</code> value
47    * @exception IOException if an error occurs
48    * @exception ServletException if an error occurs
49    */

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

134   public void doPost(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws IOException JavaDoc, ServletException JavaDoc
135   {
136     doGet(request, response);
137   }
138 }
139
Popular Tags