📄 makereservationservlet.java
字号:
/*
* Copyright 2005-2007 Kevin A. Lee
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sourceforge.hoteldj.web;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sourceforge.hoteldj.ejb.CustomerLocal;
import net.sourceforge.hoteldj.ejb.HotelManagerFacadeLocal;
import net.sourceforge.hoteldj.ejb.HotelManagerFacadeLocalHome;
import net.sourceforge.hoteldj.ejb.ReservationLocal;
/**
* Servlet implementation class for Servlet: MakeReservationServlet
*
* @author Kevin A. Lee
* @email kevin.lee@buildmeister.com
*
*/
public class MakeReservationServlet extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
private static final long serialVersionUID = -8878864874912407172L;
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public MakeReservationServlet() {
super();
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
makeReservation(request, response);
}
/*
* (non-Java-doc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
* HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
/**
* Make a new reservation
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private void makeReservation(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Context context = null;
String path = "/error.jsp";
try {
String firstName = request.getParameter("firstName");
String lastName = request.getParameter("lastName");
Date arrivalDate = new SimpleDateFormat("dd-MMM-yyyy").parse(request
.getParameter("arrivalDate"));
Integer numNights = new Integer(request.getParameter("numNights"));
Integer numGuests = new Integer(request.getParameter("numGuests"));
context = new InitialContext();
HotelManagerFacadeLocalHome hmHome = (HotelManagerFacadeLocalHome) context
.lookup(HotelManagerFacadeLocalHome.JNDI_NAME);
HotelManagerFacadeLocal hm = hmHome.create();
// check if existing customer
CustomerLocal customer = hm.getCustomerByName(firstName, lastName);
if (customer == null) {
// no, create customer
customer = hm.newCustomer(firstName, lastName);
}
// create a new reservation
ReservationLocal res = hm.newReservation(numGuests, arrivalDate,
numNights, customer.getId());
if (res != null) {
ReservationIDHelper ridHelper = new ReservationIDHelper();
String resID = ridHelper.idToFormatted(res.getId());
request.setAttribute("resID", resID);
request.setAttribute("resArrival", arrivalDate);
request.setAttribute("resNights", numNights);
request.setAttribute("resGuests", numGuests);
request.setAttribute("resCustFirst", firstName);
request.setAttribute("resCustLast", lastName);
path = "/made_reservation.jsp";
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
context.close();
} catch (NamingException e) {
e.printStackTrace();
}
}
getServletContext().getRequestDispatcher(path).forward(request,
response);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -