In this video, we will finish JSP Form which plays Message Producer role of JMS Queue. Here, we create JMS Connection and get JMSSession. Then we form TextMessage and send it to the JMS Queue.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!-- Sample 9.06: All required Packages --> <%@ page import='javax.jms.Queue' %> <%@ page import='javax.jms.ConnectionFactory' %> <%@ page import='javax.jms.Connection' %> <%@ page import='javax.jms.Session' %> <%@ page import='javax.jms.MessageProducer' %> <%@ page import='javax.jms.TextMessage' %> <%@ page import='javax.naming.InitialContext' %> <%@ page import='javax.naming.Context' %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Insert title here</title> </head> <body> <!-- Sample 9.07: Create the HTML Form --> <form action="BookFreeShipment.jsp"> Booking Person Name: <Input name="BookerName" type="text" size="25" value="Your Name Here" ><br> Weight of Shipment: <Input name="ShipmentWt" type="text" size="5" value="0" > in Kg <br> <INPUT type="submit" name="todo" value="Book Shipment"> <p>Once you get your Shipment Number, call our agent and give the Number for Free Shipping.</p> <p>Note: Getting Shipment Number may take a while!</p> <br> </form> <% //Sample 9.08: Get Request Form Field Data String BookingPerson = request.getParameter("BookerName"); String CarryWt = request.getParameter("ShipmentWt"); //Sample 9.09 Check we have Valid Request if (BookingPerson != null && BookingPerson.trim().length() > 0 && CarryWt != null && CarryWt.trim().length() > 0) { //Sample 9.10: Get JMS Queue Context ctx = new InitialContext(System.getProperties()); Queue ShipmentQueue = (javax.jms.Queue) ctx.lookup("java:/jms/queue/FreeShipmentReqQ"); //Sample 9.11: Get the connection ConnectionFactory factory = (ConnectionFactory)ctx.lookup("java:/ConnectionFactory"); Connection JmsCon = factory.createConnection(); Session JmsSession = JmsCon.createSession(false, Session.AUTO_ACKNOWLEDGE); //Sample 9.12: Format the Message String String data = BookingPerson + "^" + CarryWt ; //Sample 9.13: Post Message Data to the Queue MessageProducer sender = JmsSession.createProducer(ShipmentQueue); TextMessage TextMsg = JmsSession.createTextMessage(); TextMsg.setText(data); //Sample 9.14: Now Post the message to Shipment Queue on the Server sender.send(TextMsg); out.println("<h1>Shipment Request Posted.</h1>"); out.println( "<h3><em>Login Tomorrow to get your free Shipment Id</em></h1>"); } %> </body> </html> |
Categories: JavaEE-EJB-Tube