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

📄 confirmationservlet.java

📁 java web services how to program
💻 JAVA
字号:
// Fig. 11.25: ConfirmationServlet.java// JAXMServlet that receives messages from BuyerProvider.package com.deitel.jws1.jaxm.bookbuyer.receiver;// Java core packagesimport java.sql.*;import java.io.*;import java.util.*;// Java extension packagesimport javax.xml.messaging.*;import javax.xml.soap.*;import javax.servlet.*;import javax.servlet.http.*;public class ConfirmationServlet extends JAXMServlet   implements OnewayListener {   private ProviderConnection buyerProvider;   private Connection connection; // connection to database   // 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 propertyURL = getClass().getResource(             "bookStock.properties" );         // load properties file         Properties databaseProperties = new Properties();         databaseProperties.load( new FileInputStream(             propertyURL.getPath() ) );         // load JDBC driver         Class.forName( databaseProperties.getProperty(             "jdbcDriver" ) );         // establish database connection         connection = DriverManager.getConnection(            databaseProperties.getProperty( "databaseURI" ) );      }       // handle exception in provider connection      catch ( JAXMException jaxmException ) {         throw new ServletException( jaxmException.getMessage() +            "\nUnable to connect to provider." );      }      // handle exception if database driver does not exist      catch ( ClassNotFoundException classNotFoundException ) {         throw new ServletException(             classNotFoundException.getMessage() +            "\nUnable to load database driver." );      }      // handle exception in making Connection      catch ( SQLException sqlException ) {         throw new ServletException( sqlException.getMessage() +            "\nUnable to make database connection." );      }      // handle exception in loading properties file      catch ( IOException ioException ) {         throw new ServletException( ioException.getMessage() +            "\nUnable to load bookStock.properties." );      }   } // end method init   // invoked upon receiving message   public void onMessage( SOAPMessage message )   {      // determine whether order was successful      try {         // obtain ISBN, Quantity and Price attachments         Iterator attachments = message.getAttachments();         AttachmentPart isbnAttachment =             ( AttachmentPart ) attachments.next();         AttachmentPart quantityAttachment =             ( AttachmentPart ) attachments.next();         AttachmentPart priceAttachment =             ( AttachmentPart ) attachments.next();         // obtain ISBN, Quantity and Price from attachments         String isbn = ( String ) isbnAttachment.getContent();         Integer quantity = new Integer(             ( String ) quantityAttachment.getContent() );         Double price = new Double (             ( String ) priceAttachment.getContent() );         // ensure book availability         if ( price.doubleValue() < 0 ) {            System.err.println( isbn + " is unavailable" );            return;         }         // SQL query to database         Statement statement = connection.createStatement(             ResultSet.TYPE_SCROLL_INSENSITIVE,            ResultSet.CONCUR_READ_ONLY );         // make query to determine number of available books         ResultSet resultSet = statement.executeQuery( "SELECT" +            " quantity FROM books WHERE isbn = " + isbn );         // update BookStock1 databases content         if ( resultSet != null ) {            resultSet.next();            int newQuantity = resultSet.getInt( "quantity" ) +                quantity.intValue();            // update quantity of books in database             statement.execute( "UPDATE books SET quantity = " +                newQuantity + " WHERE isbn = " + isbn );            // place order (ibsn, quantity and price) in database            statement.execute( "INSERT INTO orders " +               "( isbn, quantity, price ) VALUES ( '" + isbn +                "' , '" + quantity + "' , '" + price.toString() +                "' )" );         }         statement.close();      }      // handle exception in accessing database      catch ( SQLException sqlException ) {         sqlException.printStackTrace();      }      // handle exception in parsing SOAP message      catch ( JAXMException jaxmException ) {         jaxmException.printStackTrace();      }      // handle exception in obtaining message attachments      catch ( SOAPException soapException ) {         soapException.printStackTrace();      }   } // end method onMessage} // end class ConfirmationServlet

⌨️ 快捷键说明

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