1 51 52 package org.objectweb.jass.examples.travelagency.servlets; 53 54 import org.objectweb.jass.examples.travelagency.ejbs.AirlineHome; 55 import org.objectweb.jass.examples.travelagency.ejbs.Airline; 56 import org.objectweb.jass.examples.travelagency.ejbs.HotelHome; 57 import org.objectweb.jass.examples.travelagency.ejbs.Hotel; 58 import org.objectweb.jass.examples.travelagency.ejbs.TravelAgency; 59 import org.objectweb.jass.examples.travelagency.ejbs.TravelAgencyHome; 60 import org.objectweb.jass.examples.travelagency.exceptions.DBException; 61 import org 62 .objectweb 63 .jass 64 .examples 65 .travelagency 66 .exceptions 67 .NotEnoughSeatsException; 68 import org 69 .objectweb 70 .jass 71 .examples 72 .travelagency 73 .exceptions 74 .NotEnoughRoomsException; 75 76 import javax.servlet.http.HttpServlet ; 77 import javax.servlet.http.HttpServletRequest ; 78 import javax.servlet.http.HttpServletResponse ; 79 import javax.naming.Context ; 80 import javax.naming.NamingException ; 81 import javax.naming.InitialContext ; 82 import javax.ejb.CreateException ; 83 import javax.activity.opennested.UserOpenNested; 84 import java.io.IOException ; 85 import java.io.PrintWriter ; 86 import java.rmi.RemoteException ; 87 88 95 public class ReservationServlet extends HttpServlet { 96 private Airline airline; 97 private Hotel hotel; 98 private TravelAgency travelAgency; 99 private UserOpenNested uon; 100 101 105 public void init() { 106 airline = getAdaptAirlineEJB(); 107 hotel = getAdaptHotelEJB(); 108 travelAgency = getAdaptTravelAgencyEJB(); 109 System.out.println("Reservation Servlet. Init called!!!"); 110 } 111 112 115 public void destroy() { 116 System.out.println("Reservation Servlet. Destroy called!!!"); 117 } 118 119 122 public void service(HttpServletRequest req, HttpServletResponse res) 123 throws IOException { 124 res.setContentType("text/html"); 125 PrintWriter out = res.getWriter(); 126 127 out.println( 128 "<HTML><HEAD></HEAD><BODY>" 129 + "<H1><U>ADAPT RESERVATION SERVICE</U></H1>"); 130 131 String action = req.getParameter("action"); 132 if (action == null) { 133 try { 134 showFlightInfo(out); 135 showHotelInfo(out); 136 showRequest(out); 137 } catch (DBException e) { 138 out.println( 139 "<H2><FONT COLOR=\"red\">Database ERROR!!!" + "</FONT></H2>"); 140 } 141 } else { 142 if (action.equals("reserve")) { 143 String name = req.getParameter("name"); 144 String nSeats = req.getParameter("number_of_seats"); 145 String nRooms = req.getParameter("number_of_rooms"); 146 makeReservation( 147 out, 148 name, 149 Integer.parseInt(nSeats), 150 Integer.parseInt(nRooms)); 151 } 152 } 153 insertReturn(out); 154 out.println("</BODY></HTML>"); 155 } 156 157 161 private void showFlightInfo(PrintWriter out) throws DBException { 162 int freeSeats = 0; 163 164 try { 165 freeSeats = airline.getFreeSeats(); 166 } catch (RemoteException e) { 167 e.printStackTrace(); 168 } 169 170 out.println( 171 "<CENTER><H3>Flight Info (ADPT001):</H3>" 172 + "<FONT COLOR=\"red\"><U>Free</U> seats in flight: </FONT>" 173 + freeSeats 174 + "</CENTER><HR SIZE=3>"); 175 } 176 177 181 private void showHotelInfo(PrintWriter out) throws DBException { 182 int freeRooms = 0; 183 184 try { 185 freeRooms = hotel.getFreeRooms(); 186 } catch (RemoteException e) { 187 e.printStackTrace(); 188 } 189 190 out.println( 191 "<CENTER><H3>Hotel Info (ADAPT PALACE *****):</H3>" 192 + "<FONT COLOR=\"red\"><U>Free</U> rooms in hotel: </FONT>" 193 + freeRooms 194 + "</CENTER><HR SIZE=3>"); 195 } 196 197 201 private void showRequest(PrintWriter out) { 202 out.println( 203 "<BR><FONT COLOR=\"red\"><U>Instructions:</U></FONT> Please, " 204 + "insert below your name and the number of seats " 205 + "and rooms that you want to reserve. If it is possible, the system " 206 + "will made the corresponding reserves for you -attending to the " 207 + "values shown above- and it will report to you the results or any " 208 + "possible failure.<BR><BR>"); 209 out.println( 210 "<FORM METHOD=\"POST\" ACTION=\"reservation\"> " 211 + "<H3>Reservation Info:</H3>" 212 + "Reservation Name:<INPUT TYPE=\"TEXT\" NAME=\"name\"><BR><BR>" 213 + "Flight info (Number of seats): <INPUT TYPE=\"TEXT\" NAME=\"number_of_seats\"><BR>" 214 + "Hotel info (Number of rooms): <INPUT TYPE=\"TEXT\" NAME=\"number_of_rooms\"><BR>" 215 + "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"reserve\">" 216 + "<BR><INPUT TYPE=\"RESET\" VALUE=\"Clear\">" 217 + "<INPUT TYPE=\"SUBMIT\" VALUE=\"Send\">" 218 + "</FORM>"); 219 out.println("</CENTER><HR SIZE=3>"); 220 } 221 222 226 private void insertReturn(PrintWriter out) { 227 out.println( 228 "<FORM METHOD=\"POST\" ACTION=\"reservation\"> " 229 + "<INPUT TYPE=\"SUBMIT\" VALUE=\"Go Reservation Page\"> " 230 + "</FORM>"); 231 } 232 233 236 private Airline getAdaptAirlineEJB() { 237 Object obj = null; 238 Context ctx = null; 239 Airline airline = null; 240 try { 241 ctx = new InitialContext (); 242 obj = ctx.lookup("AirlineEJB"); 243 AirlineHome airlineHome = 244 (AirlineHome) javax.rmi.PortableRemoteObject.narrow( 245 obj, 246 AirlineHome.class); 247 airline = airlineHome.create(); 248 } catch (NamingException e) { 249 e.printStackTrace(); 250 } catch (CreateException e) { 251 e.printStackTrace(); 252 } catch (RemoteException e) { 253 e.printStackTrace(); 254 } 255 return airline; 256 } 257 258 261 private Hotel getAdaptHotelEJB() { 262 Object obj = null; 263 Context ctx = null; 264 Hotel hotel = null; 265 try { 266 ctx = new InitialContext (); 267 obj = ctx.lookup("HotelEJB"); 268 HotelHome hotelHome = 269 (HotelHome) javax.rmi.PortableRemoteObject.narrow( 270 obj, 271 HotelHome.class); 272 hotel = hotelHome.create(); 273 } catch (NamingException e) { 274 e.printStackTrace(); 275 } catch (CreateException e) { 276 e.printStackTrace(); 277 } catch (RemoteException e) { 278 e.printStackTrace(); 279 } 280 return hotel; 281 } 282 283 286 private TravelAgency getAdaptTravelAgencyEJB() { 287 Object obj = null; 288 Context ctx = null; 289 TravelAgency travelAgency = null; 290 try { 291 ctx = new InitialContext (); 292 obj = ctx.lookup("TravelAgencyEJB"); 293 TravelAgencyHome travelAgencyHome = 294 (TravelAgencyHome) javax.rmi.PortableRemoteObject.narrow( 295 obj, 296 TravelAgencyHome.class); 297 travelAgency = travelAgencyHome.create(); 298 } catch (NamingException e) { 299 e.printStackTrace(); 300 } catch (CreateException e) { 301 e.printStackTrace(); 302 } catch (RemoteException e) { 303 e.printStackTrace(); 304 } 305 return travelAgency; 306 } 307 308 316 private void makeReservation( 317 PrintWriter out, 318 String name, 319 int nSeats, 320 int nRooms) { 321 try { 322 travelAgency.makeReservation(name, nSeats, nRooms); 323 out.println( 324 "<CENTER><H2>Congratulations!!!</H2>" 325 + "<H3>Customer <FONT COLOR=\"red\">" 326 + name 327 + "</FONT> has reserved <FONT COLOR=\"red\">" 328 + nSeats 329 + "</FONT> seats and <FONT COLOR=\"red\">" 330 + nRooms 331 + "</FONT> rooms</H3></CENTER>"); 332 } catch (RemoteException e) { 333 e.printStackTrace(); 334 } catch (NotEnoughSeatsException e) { 335 out.println( 336 "<CENTER><H2>An ERROR has occurred!!!</H2><BR>" 337 + "<H3><FONT COLOR=\"red\">Not enough seats in flight!!!</FONT></H3></CENTER>"); 338 } catch (NotEnoughRoomsException e) { 339 out.println( 340 "<CENTER><H2>An ERROR has occurred!!!</H2><BR>" 341 + "<H3><FONT COLOR=\"red\">Not enough rooms in hotel!!!</FONT></H3></CENTER>"); 342 } catch (DBException e) { 343 out.println( 344 "<H2><FONT COLOR=\"red\">Database ERROR!!!" + "</FONT></H2>"); 345 } catch (Exception e) { 346 out.println( 347 "<H2><FONT COLOR=\"red\">Unespected ERROR!!!" + "</FONT></H2>"); 348 } 349 } 350 } 351 | Popular Tags |