Adding Source Code to the Servlet

Prerequisites

You have created the servlet QuickReservationServlet.

Procedure

       1.      Open the Java editor for the new created servlet QuickReservationServlet.

       2.      Add the following source code after the doPost() method:

 

public void doWork(

   HttpServletRequest request,

   HttpServletResponse response)

   throws ServletException {

   QuickOrderProcessorLocal order = initializeController();

   handleRequest(request, response, order);

   viewAllBookings(request, order);

   HttpSession session = request.getSession(true);

   RequestDispatcher dispatcher =

   request.getRequestDispatcher("/view");

   try {

   dispatcher.forward(request, response);

   } catch (IOException e) {

   e.printStackTrace();

   throw new ServletException(e.getMessage());

   }

}

 

private QuickOrderProcessorLocal initializeController()

   throws ServletException {

   try {

       Context ctx = new InitialContext();

       QuickOrderProcessorLocalHome orderHome =

          (QuickOrderProcessorLocalHome) ctx.lookup(

              "java:comp/env/ejb/QuickOrderProcessorBean");

       return orderHome.create();

   } catch (CreateException e) {

       e.printStackTrace();

       throw new ServletException(e.toString());

   } catch (NamingException e) {

       e.printStackTrace();

       throw new ServletException(e.toString());

   }

}

 

public void handleRequest(

   HttpServletRequest request,

   HttpServletResponse response,

   QuickOrderProcessorLocal order)

   throws ServletException {

   HttpSession session = request.getSession(true);

   session.setAttribute(Constants.CLIENT_MESSAGE,null);

   String action = request.getParameter("appAction");

   if (action != null && action.length() > 0) {

       switch (Integer.parseInt(action)) {

          case Constants.ACTION_SAVE :

              {

                 saveAction(request, order);

              };

              break;

          case Constants.ACTION_CANCEL :

              {

                 cancelAction(request, order);

              }

              break;

       }

   }

      

}

 

private void viewAllBookings(

   HttpServletRequest request,

   QuickOrderProcessorLocal order) {

   HttpSession session = request.getSession(true);

   QuickBookingModel[] bookings;

   try {

       bookings = order.viewActiveBookings();

       session.setAttribute(

       Constants.RESERVATIONS,

       formatBookings(bookings));

   } catch (QuickCarRentalException e) {

       session.setAttribute(Constants.CLIENT_MESSAGE,e.getMessage());

   }

  

   }

  

private void saveAction(

   HttpServletRequest request,

   QuickOrderProcessorLocal order) {

   HttpSession session = request.getSession(true);

   try {

   java.lang.String dateFrom = request.getParameter("pickupDate");

   java.lang.String dateTo = request.getParameter("dropoffDate");

   String vehicleTypeId = request.getParameter("vehicleTypeId");

  

       String pickupLocation = request.getParameter("pickupLocation");

       String dropoffLocation = request.getParameter("dropoffLocation");

       order.saveBooking(vehicleTypeId,dateFrom,

          dateTo,

          pickupLocation,

          dropoffLocation);

   } catch (QuickCarRentalException e) {

       session.setAttribute(Constants.CLIENT_MESSAGE,e.getMessage());

   }

}

 

private void cancelAction(

   HttpServletRequest request,

   QuickOrderProcessorLocal order) {

   HttpSession session = request.getSession(true);

   String[] selectedBookings = request.getParameterValues("check");

   for (int i = 0; i < selectedBookings.length; i++) {

       try {

          order.cancelBooking((String) selectedBookings[i]);

       } catch (QuickCarRentalException e) {

         

          session.setAttribute(

       Constants.CLIENT_MESSAGE,e.getMessage());

       }

   }

}

private ArrayList formatBookings(QuickBookingModel[] bookings){

   ArrayList bookings_list = new ArrayList();

   DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);

   for (int i=0;i<bookings.length;i++){

       String[] booking = new String[7];

       booking[0] = bookings[i].getBookingId();

       booking[1] = bookings[i].getVehicleType();

       booking[2] = bookings[i].getPickupLocation();

       booking[3] = bookings[i].getDropoffLocation();

       booking[4] = bookings[i].getDateFrom();

       booking[5] = bookings[i].getDateTo();

       booking[6] = bookings[i].getPrice();

       bookings_list.add(booking);

   }

  

   return bookings_list;

}

       3.      Add the statement calling doWork() to the two servlet methods doGet() and doPost(). To do so, replace the comment line //TODO : Implement  in the two methods with:

doWork(request, response);

       4.      If necessary, correct the formatting of these lines by choosing Source ® Format from the context menu.

       5.      To add the import statements, position the cursor anywhere in the Java Editor and choose Source ® Organize Imports from the context menu.

       6.      Choose the context javax.naming.Context and if necessary com.sap.engine.examples.util.Constants,and confirm by choosing OK.

The appropriate import statements are added to the source code.

       7.      Save the contents of the editor by choosing the appropriate icon from the toolbar.

Result

The Developer Studio refreshes the servlet source code. Your Tasks view should no longer show any other errors.

Next step:

Creating a Web Archive