📄 bookpriceservlet.java
字号:
// Fig. 15.9: BookPriceServlet.java.
// Class BookPriceServlet exposes the Book Price Web Service for
// Bookstore 1.
package jws1casestudy.bookstore1;
// Java core packages
import java.sql.*;
import java.util.*;
// Java extension packages
import javax.servlet.*;
import javax.xml.messaging.*;
import javax.xml.soap.*;
// Deitel packages
import jws1casestudy.pricefinder.common.*;
public class BookPriceServlet extends JAXMServlet
implements ReqRespListener {
// factory used to create SOAPMessage objects
private MessageFactory messageFactory;
// reference to object that provides Web-Service
private BookPrice service;
// initialize BookPriceServlet
public void init( ServletConfig config )
throws ServletException
{
super.init( config );
// instantiate Web-service object and obtain MessageFactory
try {
service = new BookPriceImpl();
messageFactory = MessageFactory.newInstance();
}
// handle exception in creating MessageFactory
catch ( Exception exception ) {
exception.printStackTrace();
}
} // end method init
// container invokes this method upon receiving SOAP message
public SOAPMessage onMessage( SOAPMessage message )
{
// invoke Web service and return results as SOAP message
try {
// extract ISBN from incoming SOAPMessage
String isbn = getISBN( message );
// invoke Book Price Web service
if ( service != null && isbn != null ) {
PriceQuote priceQuote = service.getPrice( isbn );
// store result as SOAP message and return to client
if ( priceQuote != null )
return createResponse( priceQuote );
}
}
// handle exception if unable to create SOAP message
catch ( Exception exception ) {
exception.printStackTrace();
}
return null;
} // end method onMessage
// extract ISBN from SOAP message request
private String getISBN( SOAPMessage message )
throws SOAPException
{
// obtain references to envelope and body of message
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
// get child elements of SOAPBody
Iterator childElements = body.getChildElements();
childElements.next(); // skip first element
// cast to SOAPBodyElement
SOAPBodyElement getPriceElement =
( SOAPBodyElement ) childElements.next();
// get child elements of SOAPBodyElement
Iterator subElements = getPriceElement.getChildElements();
subElements.next(); // skip first element
// obtain SOAPElement that contains isbn
SOAPElement isbnElement =
( SOAPElement ) subElements.next();
return isbnElement.getValue();
} // end method getISBN
// store PriceQuote as SOAP message
private SOAPMessage createResponse( PriceQuote priceQuote )
throws SOAPException
{
// create empty SOAP message
SOAPMessage message = messageFactory.createMessage();
// obtain references to response message elements
SOAPPart part = message.getSOAPPart();
SOAPEnvelope envelope = part.getEnvelope();
SOAPBody body = envelope.getBody();
// add namespace declarations
envelope.addAttribute( envelope.createName( "xmlns:xsd" ),
"http://www.w3.org/2001/XMLSchema" );
envelope.addAttribute( envelope.createName( "xmlns:ns1" ),
"http://www.deitel.com/BookPrice/type" );
envelope.addAttribute( envelope.createName( "xmlns:xsi" ),
"http://www.w3.org/2001/XMLSchema-instance" );
envelope.addAttribute(
envelope.createName( "xmlns:SOAP-ENC" ),
"http://schemas.xmlsoap.org/soap/encoding/" );
// set encoding style for SOAP body
body.setEncodingStyle(
"http://schemas.xmlsoap.org/soap/encoding/" );
// create root element
SOAPBodyElement returnMessageElement = body.addBodyElement(
envelope.createName( "getPriceResponse",
"ns2", "http://www.deitel.com/BookPrice.wsdl" ) );
// create root element
SOAPElement priceQuoteElement =
returnMessageElement.addChildElement(
envelope.createName( "return" ) );
priceQuoteElement.addAttribute(
envelope.createName( "xsi:type" ),
"ns1:PriceQuote" );
// store price element as priceQuoteElement child
SOAPElement priceElement =
priceQuoteElement.addChildElement(
envelope.createName( "price" ) );
priceElement.addTextNode(
Double.toString( priceQuote.getPrice() ) );
priceElement.addAttribute(
envelope.createName( "xsi:type" ), "xsd:double" );
// store ISBN element as priceQuoteElement child
SOAPElement isbnElement =
priceQuoteElement.addChildElement(
envelope.createName( "isbn" ) );
isbnElement.addTextNode( priceQuote.getIsbn() );
isbnElement.addAttribute(
envelope.createName( "xsi:type" ), "xsd:string" );
// store storeID element as priceQuoteElement child
SOAPElement storeIdElement =
priceQuoteElement.addChildElement(
envelope.createName( "storeID" ) );
storeIdElement.addTextNode(
Integer.toString( priceQuote.getStoreID() ) );
storeIdElement.addAttribute(
envelope.createName( "xsi:type" ), "xsd:int" );
// store storeDescription element as priceQuoteElement child
SOAPElement storeDescriptionElement =
priceQuoteElement.addChildElement(
envelope.createName( "storeDescription" ) );
storeDescriptionElement.addTextNode(
priceQuote.getStoreDescription() );
storeDescriptionElement.addAttribute(
envelope.createName( "xsi:type" ), "xsd:string" );
return message;
} // end method createResponse
} // end class BookPriceServlet
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -