📄 makeorderservlet.java
字号:
// MakeOrderServlet.java
// Retrieves user-input to invoke the Book Purchase Web service
// of the specified Book Store.
package jws1casestudy.clients.web;
// Java core packages
import java.io.*;
import java.util.*;
// Java extension packages
import javax.servlet.*;
import javax.servlet.http.*;
import javax.xml.rpc.*;
// Deitel packages
import jws1casestudy.clients.stubs.bookpurchase.*;
public class MakeOrderServlet extends HttpServlet {
private ServletContext servletContext;
// obtain servlet context
public void init( ServletConfig servletConfig )
throws ServletException
{
servletContext = servletConfig.getServletContext();
}
// invoke Web service Book Purchase with user input
protected void doPost( HttpServletRequest request,
HttpServletResponse response )
throws ServletException, IOException
{
// obtain session if exists
HttpSession session = request.getSession( false );
// if session does not exist, forward to start page
if ( session == null ) {
RequestDispatcher dispatcher =
servletContext.getRequestDispatcher(
"/FindBook.html" );
dispatcher.forward( null, null );
}
// obtain user input
String firstName = request.getParameter( "firstname" );
String lastName = request.getParameter( "lastname" );
String email = request.getParameter( "email" );
String streetAddress =
request.getParameter( "streetaddress" );
String city = request.getParameter( "city" );
String state = request.getParameter( "state" );
String zip = request.getParameter( "zip" );
String country = request.getParameter( "country" );
String creditCard = request.getParameter( "creditcard" );
// instantiate Customer instance
Customer customer = new Customer();
customer.setFirstName( firstName );
customer.setLastName( lastName );
customer.setEmailAddress( email );
customer.setStreetAddress( streetAddress);
customer.setCity( city );
customer.setState( state );
customer.setZipCode( zip );
customer.setCountry( country );
customer.setCreditCardNumber( creditCard );
// obtain price quote contents from session
String price =
( String )session.getAttribute( "price" );
String isbn =
( String )session.getAttribute( "isbn" );
String storeID =
( String )session.getAttribute( "storeID" );
String storeDescription =
( String )session.getAttribute( "storeDescription" );
// create PriceQuote from contents in session
PriceQuote priceQuote = new PriceQuote();
priceQuote.setIsbn( isbn );
priceQuote.setPrice( Double.parseDouble( price ) );
priceQuote.setStoreID( Integer.parseInt( storeID ) );
priceQuote.setStoreDescription( storeDescription );
// attempt to place order
try {
// create Web service stub factory
BookPurchaseService bookPurchaseService =
new BookPurchaseService_Impl();
// obtain reference to Web service stub
BookPurchase bookPurchase =
bookPurchaseService.getBookPurchase();
// place order
bookPurchase.orderBook( priceQuote, customer );
}
// handle Web service exceptions
catch ( Exception exception ) {
exception.printStackTrace();
}
// store customer information in session
session.setAttribute( "firstName", firstName );
session.setAttribute( "lastName", lastName );
session.setAttribute( "email", email );
session.setAttribute( "streetAddress", streetAddress );
session.setAttribute( "city", city );
session.setAttribute( "state", state );
session.setAttribute( "zip", zip );
session.setAttribute( "country", country );
session.setAttribute( "creditCard", creditCard );
// dispatch to order confirmation jsp
RequestDispatcher dispatcher =
servletContext.getRequestDispatcher(
"/OrderConfirmation.jsp" );
dispatcher.forward( request, response );
} // end method doPost
} // end class MakeOrderServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -