- EJBs can be used to create enterprise applications like banking systems. Clients will interact with such systems for operations like: withdraw cash, transfer cash, pay bills
- When clients directly access EJBs it poses concerns such as: security risks, firewall blocking, EJB architecture becomes transparent to the clients
- Servlets can act as the middleman between the EJBs and clients.
- Change to EJB becomes easier – hidden from the user
- Data in EJB better protected
- Central control of EJBs
- When servlets work as the proxy, smaller devices become capable to access EJBs through http protocol (light weight protocol – good for smaller devices )
- Servlets can work as a front controller
- Supports different types of clients
- Better separation of responsibilities that is desirable – web: flow control logic, EJBs: business logic, EIS: Enterprise Information Systems
- Remember: servlets can be bottlenecks here, better servlet design strategy is required – synchronization may help
- For small applications – may be overkill
How to use EJBs from servlets
- Think about a banking system implemented in EJBs.
- Entity beans = Customers, Accounts
- Session beans = operations like withdraw cash
- In this scenario, servlets can call the session beans and return the results to the clients
- A sample servlet to access the EJBs: Here remote ejbs are assumed : EJB2 is assumed : just example is shown – the coding style may not be great … just an example
-
// for remote references to objects such as EJBsimport javax.rmi.PortableRemoteObject;//banking system ejb packageimport bankingSystemEjbClasses.*;import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import javax.naming.*;public class ServletEjb extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ float credit = 10; float debit = 20; String custName = ""; float initBalance = 0, endBalance = 0; try{ //locate and instantiate the account manager bean Context context = new InitialContext(); Object ref = context.lookup("AccountManager"); AccountManagerHome accountManagerHome = (AccountManagerHome) PortableRemoteObject.narrow (ref, AccountManagerHome.class); AccountManager accountManager = accountManagerHome.create(); //create a savings account and manipulate itaccountManager.createSavingsAccount(1,"John Smith") initBalance = accountManager.getInitialBalance("1");//create customeraccountManager.createCustomer(1,"John Smith") custName = accountManager.getCustomerName();//generate outputresponse.setContentType("text/html");PrintWriter out = response.getWriter();out.println("");out.println("");out.println("Customer Name:" + custName);out.println("Initial Balance:" + initBalance);out.println("");out.println(""); }catch(Exception ex){}}}
From: http://sitestree.com/?p=4965
Categories:Java Short Notes
Tags:
Post Data:2010-04-10 03:31:53
Shop Online: https://www.ShopForSoul.com/
(Big Data, Cloud, Security, Machine Learning): Courses: http://Training.SitesTree.com
In Bengali: http://Bangla.SaLearningSchool.com
http://SitesTree.com
8112223 Canada Inc./JustEtc: http://JustEtc.net (Software/Web/Mobile/Big-Data/Machine Learning)
Shop Online: https://www.ShopForSoul.com/
Medium: https://medium.com/@SayedAhmedCanada