java ejb tomcat_Java Web 应用开发: J2EE 和 Tomcat 蔡 剑, Ph.D.. 本讲内容 EJB 层技术 (II) Transaction and Security ...

(35) 2024-04-12 11:01:01

Java WebJ2EETomcat , Ph.D.

EJB (II)Transaction and SecurityResource ConnectionDeploymentWeb Services

Review: J2EE Framework

Web Application Architecture: MVC Model

Transaction Conceptbegin transaction debit checking account credit savings account update history log commit transaction

CMT Transaction Managementpublic 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 : JDBCpublic 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 : JTApublic 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());}

Transaction Allowance

Bean Type Container-Managed Bean-Managed JTA JDBC Entity Y N N Session Y Y Y Message-driven Y Y Y

Updating Multiple Databases

Resource Connections

Resource Manager Type Connection Factory Type JNDI Subcontext JDBC javax.sql.DataSource java:comp/env/jdbc JMS javax.jms.TopicConnectionFactory javax.jms.QueueConnectionFactory java:comp/env/jms JavaMail javax.mail.Session java:comp/env/mail URL java.net.URL java:comp/env/url Connector javax.resource.cci.ConnectionFactory java:comp/env/eis JAXR Resource Adapter javax.xml.registry.ConnectionFactory java:comp/env/eis/JAXR

Database Connectionprivate String dbName = "java:comp/env/jdbc/SavingsAccountDB";

InitialContext ic = new InitialContext(); DataSource ds = (DataSource) ic.lookup(dbName);

Connection con = ds.getConnection();

EJB Deployment: Security Declare

Programmatic Security in the EJB Tier public String getUser() { return context.getCallerPrincipal().getName(); }

boolean result = context.isCallerInRole("Customer");

Propagating Security Identity

Web Deployment: Security Declare

Packaging ModulesEJBejbWebjava(connector),

Deployment Configuration FilesEJB web.xml

EJB Deployment: Packaging

EJB and Web ServicesJ2EE1.4 Web Services.Net Web ServicesPerl Web Services

EJB2.1 ContainerStatelessSession BeanSOAPSOAPSOAP

Web Services

Web Services SupportWeb 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.

Web Services Concept

Web Services

Web Services Standards

Three Players of WS

SOAP

UDDI

WSDL

J2EE Web Services

THE END

发表回复