Java naming and directory interface(JNDI)

Introduction 

 
It is a mechanism to search for a remote application from the local application. The remote application can be an EJB and the local application can be the client of EJB. Both of these applications can be available in one machine or different machine in the network. Both of these applications use an interface between each other called SPI (service provider interface). SPI is a part of the application server. All EJBs must register a unique name called JNDI name during their deployment. The client application can search the machine of the application server by using its IP address, port number and the protocol of the distributed application. The client initializes the SPI by using the class name present in WebLogic. The client searches the EJB by using the JNDI name of EJB. The SPI returns a reference of the home interface. The client can use the reference of the home interface to call the create() method and to get the reference of the remote interface. The reference of the remote interface can be used to call business method.
 

Architecture of JNDI

 
architecture of JNDI 
 
Creation process of EJB client
  1. Create an object of java.util.properties by using its default constructor.
  2. Use the put () method of properties to store the constants of javax.naming.context with their respective value. 
  3. Create an object of javax.name.InitialContext by using the object or properties into its constructor. 
  4. Use lookup () method of InitialContext by providing JNDI name of EJB as its arguments. This method returns the reference of the home interface. 
  5. Call create () method of home interface to get the reference of remote interface. 
  6. Call the business methods by using reference of remote interface.
  1. // client of EJB(session bean)    
  2. //standalone client    
  3. import javax.naming.*;    
  4. import java.util.*;    
  5. import account.*;    
  6. public class accClient {    
  7.     public static void main(String arg[]) throws Exception {    
  8.         Properties p = new Properties();    
  9.         p.put(Context.PROVIDER_URL, "t3://localhost:7001");    
  10.         p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");    
  11.         InitialContext ic = new InitialContext(p);    
  12.         accHome ah = (accHome) ic.lookup("accBean");    
  13.         Scanner sc = new Scanner(System.in);    
  14.         System.out.print("Provide account no:");    
  15.         int x = sc.nextInt();    
  16.         System.out.print("Provide account Pin no:");    
  17.         int y = sc.nextInt();    
  18.         accRemote ar = ah.create();    
  19.         System.out.println("Created...");    
  20.         String str = ar.validate(x, y);    
  21.         System.out.println("Ur information is " + str);    
  22.         if (str.equals("Valid"))    
  23.             System.out.println("Balance :" + ar.getBalance(x));    
  24.     }    
  25. }   
Creating webbased client of EJB
  1. Create the JSP by using JNDI code to connect Ejb and to call method of EJB.
  2. Store the JSP in context folder of web logic. 
  3. Get the copy of Ejb's .jar files (e.jar) and make it available inside lib folder of the context. 
  4. Start the server in the domain and access the jsp from browser.
Example
accBean.java 
  1. //Bean Class  
  2. package account;  
  3. import javax.ejb.*;  
  4. import java.sql.*;  
  5. public class accBean implements SessionBean {  
  6.     SessionContext sc;  
  7.     int bal;  
  8.     public void ejbCreate() {}  
  9.     public void ejbRemove() {}  
  10.     public void ejbActivate() {}  
  11.     public void ejbPassivate() {}  
  12.     public void setSessionContext(SessionContext s) {  
  13.         sc = s;  
  14.     }  
  15.     public String validate(int ano, int pno) {  
  16.         try {  
  17.             Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
  18.             Connection con = DriverManager.getConnection("jdbc:odbc:dsn1""system""pintu");  
  19.             Statement stmt = con.createStatement();  
  20.             ResultSet rs = stmt.executeQuery("select * from account where accno=" + ano + " and pno=" + pno);  
  21.             if (rs.next()) {  
  22.                 bal = rs.getInt("balance");  
  23.                 return "Valid";  
  24.             }  
  25.         } catch (Exception e) {  
  26.             System.out.println(e);  
  27.         }  
  28.         return "Invalid";  
  29.     }  
  30.     public int getBalance(int ano) {  
  31.         return bal;  
  32.     }  
  33. }  
ejb-test.jsp
  1. <!-- Web client of EJB -->  
  2. <%@ page import="javax.naming.*,java.util.*,account.*" %>  
  3. <html>  
  4. <body>  
  5. <form>  
  6. |<h1>Account No<input type="text" name="t1"></h1>  
  7. <h1>Pin No<input type="text" name="t2"></h1>  
  8. <input type="submit" value="submit">  
  9.  </form>  
  10. <%  
  11.  String st1=request.getParameter("t1");  
  12. String st2=request.getParameter("t2");  
  13. if(st1 !=null)  
  14.        {  
  15. Properties p=new Properties();  
  16.   p.put(Context.PROVIDER_URL,"t3://localhost:7001");  
  17. p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");  
  18. InitialContext ic=new InitialContext(p);  
  19. accHome ah=(accHome)ic.lookup("accBean");  
  20.   int x=Integer.parseInt(st1);  
  21.   int y=Integer.parseInt(st2);  
  22. accRemote ar=ah.create();  
  23. String str=ar.validate(x,y);  
  24.   if(str.equals("Valid"))  
  25.    {  
  26.   %>  
  27. <h1>Balance :<%= ar.getBalance(x) %></h1>  
  28. <%  
  29.   }  
  30.   else  
  31.    {  
  32.  %>  
  33.   <h1>Invalid Information </h1>  
  34.    <% } }%>  
  35.    </body>  
  36. </html> 


Similar Articles