1 package edu.rice.rubis.beans.servlets; 2 3 import java.io.IOException ; 4 5 import javax.jms.MapMessage ; 6 import javax.jms.Session ; 7 import javax.jms.TextMessage ; 8 import javax.jms.Topic ; 9 import javax.jms.TopicConnection ; 10 import javax.jms.TopicConnectionFactory ; 11 import javax.jms.TopicRequestor ; 12 import javax.jms.TopicSession ; 13 import javax.naming.Context ; 14 import javax.naming.InitialContext ; 15 import javax.servlet.ServletException ; 16 import javax.servlet.http.HttpServlet ; 17 import javax.servlet.http.HttpServletRequest ; 18 import javax.servlet.http.HttpServletResponse ; 19 20 26 public class AboutMe extends HttpServlet 27 { 28 29 private void printError(String 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 46 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 47 { 48 doPost(request, response); 49 } 50 51 52 61 public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException , ServletException 62 { 63 ServletPrinter sp = null; 64 Context initialContext = null; 65 String password=null, username=null; 66 Integer userId=null; 67 68 sp = new ServletPrinter(response, "About me"); 69 70 username = request.getParameter("nickname"); 71 password = request.getParameter("password"); 72 if ((username != null && !username.equals("")) || (password != null && !password.equals(""))) 74 { 75 try 76 { 77 initialContext = new InitialContext (); 78 } 79 catch (Exception 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 topicFactory = null; 93 TopicConnection connection = null; 94 TopicSession session = null; 95 Topic topic = null; 96 String html; 97 try 98 { 99 topicFactory = (TopicConnectionFactory )initialContext.lookup(Config.TopicConnectionFactoryName); 101 connection = topicFactory.createTopicConnection(); 103 topic = (Topic ) initialContext.lookup(Config.PrefixTopicName+"topicAboutMe"); 105 session = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); } 108 catch (Exception e) 109 { 110 sp.printHTML("Cannot connect to message bean MDB_AboutMe : " +e+"<br>"); 111 return ; 112 } 113 try 114 { 115 TopicRequestor requestor = new TopicRequestor (session, topic); 117 MapMessage message = session.createMapMessage(); 119 message.setString("username", username); 121 message.setString("password", password); 122 message.setJMSCorrelationID("aboutMe"); 123 connection.start(); TextMessage reply = (TextMessage )requestor.request(message); 126 connection.stop(); 127 html = reply.getText(); 129 requestor.close(); connection.close(); 132 } 133 catch (Exception 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 |