Ejb is denoted as Enterprise Java Bean Component, also called server-side software components; it will mainly be used as the applications’ business logic. The web containers used for runtime environments include the software components, computer security, servlet lifecycle management, transaction processing, and web services.EJb is the architecture style written in the Java programming languages running on the server-side of the computer network, so it has followed the client-server model in the applications. And also, ejb is the java bean technology to distribute the web components on the client-side it has the feature called reusable components in multiple web applications.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax:
The basic syntax of the EJB model in java programming as follows.
import javax.ejb.*; import java packages; class classname { Main method() { -----Some programming logics---- } }
The above codes are basic steps for developing the application using ejb packages.
Below are the examples mentioned:
Interface:
package JPAEJB; import java.util.List; import javax.ejb.Remote; @Remote public interface CustomerInterface { void addBooks(String name); List getBooks(); }
Implementation:
import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import JPAEJB.CustomerInterface; @Stateless public class CustomerClass implements CustomerInterface { List<String>books; public CustomerClass() { books = new ArrayList<String>(); } public void addBooks(String name) { books.add(name); } public List<String> getBooks() { return books; } }
CustomerInterface:
import JPAEJB.CustomerInterface; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.List; import java.util.Properties; import javax.naming.InitialContext; import javax.naming.NamingException; public class CustomerInterfaceImplem { BufferedReader brConsoleReader = null; Properties p; InitialContext c; { p = new Properties(); try { p.load(new FileInputStream("jndi.properties")); } catch (IOException ex) { ex.printStackTrace(); } try { c = new InitialContext(p); } catch (NamingException ex) { ex.printStackTrace(); } brConsoleReader = new BufferedReader(new InputStreamReader(System.in)); } public static void main(String[] args) { CustomerInterfaceImplem cust = new CustomerInterfaceImplem(); cust.testStatelessEjb(); } private void show() { System.out.println("Welcome to my domain"); System.out.print("Options \n1. Add the Books\n2. Exit \nEnter your Choice: "); } private void StatelessEjb() { try { intc = 1; CustomerInterface cust = (CustomerInterface)ctx.lookup("CustomerInterface/remote"); while (c != 2) { String books; show(); String str = brConsoleReader.readLine(); c = Integer.parseInt(str); if (c == 1) { books = brConsoleReader.readLine(); cust.addBooks(books); }elseif (c == 2) { break; } } List<String>books1 = cust.getBooks(); System.out.println(books1.size()); for (inti = 0; i<books1.size(); ++i) { System.out.println((i+1)+". " + books1.get(i)); } CustomerInterface cust1 = (CustomerInterface)ctx.lookup("cust/remote"); List<String>books2 = cust.getBooks(); System.out.println(books2.size()); for (inti = 0; i<books2.size(); ++i) { System.out.println((i+1)+". " + books2.get(i)); } } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } finally { try { if(brConsoleReader !=null) { brConsoleReader.close(); } } catch (IOException ex) { System.out.println(ex.getMessage()); } } } }
Sample Output:
EJB in Web Services:
package JPAEJB; import java.util.List; import Customers.CustomerInterfaceImplem; public class Ejbclients{ public static void main(String[] args) { for(CustomerInterface cust:getBooks()) { System.out.println(cust.getBooks()); } } private static List <CustomerInterface> getBooks() { CustomerInterfaceImplem s = new CustomerInterfaceImplem(); CustomerInterface c = s.getCustomerInterfacePort(); return c.getBooks(); } }
EJB Security:
import javax.ejb.* @Stateless @DeclareRoles({"customer""books"}) public class CustSecurity implements CustomerInterface { @RolesAllowed({"books"}) public void delete(CustomerInterface cust) { System.out.println("customer delete the books"); } @PermitAll public void showBooks(CustomerInterface cust) { System.out.println("customer viewed the books"); } @DenyAll public void deleteAll() { System.out.println("customer delete all the books in libraries"); } }
Security XML:
<?xml version="1.0"encoding="UTF-8"?> <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 9.0 EJB 3.0//EN""http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_0-0.dtd"> <ejb-jar> <security-role-mapping> <role-name>customer</role-name> <group-name>customer-groups</group-name> </security-role-mapping> <security-role-mapping> <role-name>Books</role-name> <group-name>Books-group</group-name> </security-role-mapping> <enterprise-beans/> </ejb-jar>
Explanation: The above three examples are the same output we used different features of the ejb first example we use the customer will add and delete the books using jndi(java naming directory interface) second example we use the web service for customer done the operations through web final example we used the additional security.
Generally, EJB act as an interface outside of business logic applications; it has the browser compatibility feature security-wise more when compared to other business logic frameworks. It also maintains the system-level transactions.
The above is the detailed content of EJB in Java. For more information, please follow other related articles on the PHP Chinese website!