⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bookbuyerservlet.java

📁 java web services how to program
💻 JAVA
字号:
// Fig. 11.19: BookBuyerServlet.java// BookBuyerServlet uses a message provider (BuyerProvider)// to send messages to SellerProvider.package com.deitel.jws1.jaxm.bookbuyer.sender;// Java core packagesimport java.io.*;import java.util.*;// Java extension packagesimport javax.servlet.http.*;import javax.servlet.*;import javax.xml.messaging.*;import javax.xml.soap.*;// ebXML packagesimport com.sun.xml.messaging.jaxm.ebxml.*;public class BookBuyerServlet extends HttpServlet {   private ProviderConnection buyerProvider;   private MessageFactory messageFactory;   // source and destination endpoints for messages   private String from, to;   private Properties endPointProperties;   // setup connection to message provider   public void init( ServletConfig servletConfig )       throws ServletException    {      super.init( servletConfig );      // establish connection to provider      try {         ProviderConnectionFactory providerFactory =             ProviderConnectionFactory.newInstance();         buyerProvider = providerFactory.createConnection();         // obtain URL of properties file         java.net.URL endPointURL = getClass().getResource(             "endpoint.properties" );         // load properties file         endPointProperties = new Properties();         endPointProperties.load( new FileInputStream(             endPointURL.getPath() ) );         // obtain source and destination endpoints         from = endPointProperties.getProperty( "from" );         to = endPointProperties.getProperty( "to" );         // obtain supported profiles for provider         ProviderMetaData metaData = buyerProvider.getMetaData();         String[] profiles = metaData.getSupportedProfiles();         // determine whether ebXML profile is supported         boolean isProfileSupported = false;         for ( int i = 0; i < profiles.length; i++ )            if ( profiles[ i ].equals( "ebxml" ) )               isProfileSupported = true;         // use ebXML profile, if supported         if ( isProfileSupported )            messageFactory =                buyerProvider.createMessageFactory( "ebxml" );         else            throw new ServletException( "Profile ebxml is " +               "not supported." );      }       // handle exception in connecting to provider      catch ( JAXMException jaxmException ) {         throw new ServletException( jaxmException.getMessage() +            "\nUnable to connect to message provider." );      }      // handle exception if unable to locate Endpoint.properties      catch ( FileNotFoundException fileNotFoundException ) {         throw new ServletException(             fileNotFoundException.getMessage() +            "\nUnable to locate endpoint.properties." );      }      // handle exception in loading properties file      catch ( IOException ioException ) {         throw new ServletException( ioException.getMessage() +            "\nUnable to load endpoint.properties." );      }   } // end method init   // invoked when client makes get request   public void doGet( HttpServletRequest request,       HttpServletResponse response ) throws ServletException   {      // create ebXML message for BuyerProvider      try {         // create ebXML message         EbXMLMessageImpl message =             ( EbXMLMessageImpl ) messageFactory.createMessage();         // set send and receive provider         message.setSender( new Party( from ) );         message.setReceiver( new Party( to ) );         // store ISBN in message attachment         AttachmentPart isbnAttachment =             message.createAttachmentPart();         isbnAttachment.setContent( request.getParameter(             "ISBN" ), "text/plain" );         // store quantity in message attachment         AttachmentPart quantityAttachment =             message.createAttachmentPart();         quantityAttachment.setContent( request.getParameter(             "Quantity" ), "text/plain" );         // add ISBN and Quantity attachments to message         message.addAttachmentPart( isbnAttachment );         message.addAttachmentPart( quantityAttachment );         // send message from BuyerProvider to Web service         buyerProvider.send( message );         // display HTML confirmation message to client         response.setContentType( "text/html" );         PrintWriter out = response.getWriter();         String viewOrderURL =             endPointProperties.getProperty( "viewOrderURL" );         out.println( "<html>" );         out.println( "<body>Order placed." );         out.println( "Visit <a href=" + viewOrderURL + ">" );         out.println( viewOrderURL + "</a>" );         out.println( "to view order status.</body>" );         out.println( "</html>" );      }      // handle exception in using message provider      catch ( JAXMException jaxmException ) {         throw new ServletException( jaxmException.getMessage() +            "\nError in using message provider." );      }      // handle exception in creating SOAP messages      catch ( SOAPException soapException ) {         throw new ServletException( soapException.getMessage() +            "\nUnable to create SOAP message." );      }      // handle exception if servlet not initialized      catch ( NullPointerException nullException ) {         throw new ServletException( nullException.getMessage() +            "\nServlet not initialized properly." );      }      // handle exception in writing HTML to client      catch ( IOException ioException ) {         throw new ServletException( ioException.getMessage() +            "\nServlet not initialized properly." );      }   } // end method doGet} // end class BookBuyerServlet

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -