300 likes | 456 Views
蔡 剑 , Ph.D. Java Web应用开发:J2EE和Tomcat. 本讲内容. EJB 层技术 ( II) Transaction and Security Resource Connection Deployment Web Services. RDMS. JDBC. Web Container. JSPs. ( X)HTML XML. Servlets. HTTP. JSTL. JavaMail. Mail Server. J2EE Application Server. JAX RPC. Mgmt. Java
E N D
蔡 剑, Ph.D. Java Web应用开发:J2EE和Tomcat
本讲内容 • EJB层技术 (II) • Transaction and Security • Resource Connection • Deployment • Web Services
RDMS JDBC Web Container JSPs (X)HTML XML Servlets HTTP JSTL JavaMail Mail Server J2EE Application Server JAX RPC Mgmt Java Application RMI JAXR JACC JNDI JDBC JMS JAF JTA Applet SAAJ JMX CORBA Server IIOP EJB Container Application Client Container Message Beans Session Beans Entity Beans Client Application Directory Service JNDI JAX RPC Mgmt JMS JAX RPC Mgmt Message Queue JAXR JACC JNDI JDBC JMS JAF JTA JAXR JMS SAAJ JMX SAAJ JMX Review: J2EE Framework
Model • Encapsulates application state • Responds to state queries • Exposes application functionality • Notifies views of changes State Change State Query Change Notice • View • Renders the models • Request updates from models • Sends user gestures to Controller • Allows controller to select View • Controller • Define application behavior • Maps user actions to model updates • Select view for response • One for each functionality View Selection User gestures Web Application Architecture: MVC Model
Transaction Concept begin transaction debit checking account credit savings account update history log commit transaction
CMT Transaction Management public void transferToSaving(double amount) throws InsufficientBalanceException { checkingBalance -= amount; savingBalance += amount; try { updateChecking(checkingBalance); if (checkingBalance < 0.00) { context.setRollbackOnly(); throw new InsufficientBalanceException(); } updateSaving(savingBalance); } catch (SQLException ex) { throw new EJBException ("Transaction failed due to SQLException: " + ex.getMessage()); } }
BMT Transaction Management : JDBC public void ship (String productId, String orderId, int quantity) { try { con.setAutoCommit(false); updateOrderItem(productId, orderId); updateInventory(productId, quantity); con.commit(); } catch (Exception ex) { try { con.rollback(); throw new EJBException("Transaction failed: " + ex.getMessage()); } catch (SQLException sqx) { throw new EJBException("Rollback failed: " + sqx.getMessage()); } } }
BMT Transaction Management : JTA public void withdrawCash(double amount) { UserTransaction ut = context.getUserTransaction(); try { ut.begin(); updateChecking(amount); machineBalance -= amount; insertMachine(machineBalance); ut.commit(); } catch (Exception ex) { try { ut.rollback(); } catch (SystemException syex) { throw new EJBException ("Rollback failed: " + syex.getMessage()); } throw new EJBException ("Transaction failed: " + ex.getMessage()); }
Database Connection private String dbName = "java:comp/env/jdbc/SavingsAccountDB"; InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName); Connection con = ds.getConnection();
Programmatic Security in the EJB Tier public String getUser() { return context.getCallerPrincipal().getName(); } boolean result = context.isCallerInRole("Customer");
Packaging Modules • EJB模块,其包含ejb文件及相应类 • Web模块,其包含网络层的组件及资源 • 应用客户模块,其包含应用客户类 • 资源适配器模块,其包含java连接器(connector),资源适配器,和帮助库函数及相关资源。
Deployment Configuration Files • EJB 部署描述文件 • 网络层部署描述文件 web.xml • 应用客户部署描述文件 • 资源适配器部署描述文件
EJB and Web Services J2EE1.4 Web Services SOAP EJB2.1 Container Stateless Session Bean SOAP .Net Web Services SOAP Perl Web Services
Web Services Support • Web services are Web-based enterprise applications that use open, Extensible Markup Language (XML)-based standards and transport protocols to exchange data with calling clients. • The J2EE platform provides the XML APIs and tools you need to quickly design, develop, test, and deploy Web services and clients that fully interoperate with other Web services and clients running on Java-based or non-Java-based platforms. • It is easy to write Web services and clients with the J2EE XML APIs. No low-level programming is needed because the XML API implementations do the work of translating the application data to and from an XML-based data stream that is sent over the standardized XML-based transport protocols. • The translation of data to a standardized XML-based data stream is what makes Web services and clients written with the J2EE XML APIs fully interoperable.