Java naming and directory interface(JNDI)


It is a mechanism to search 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 application use an interface between each other called as 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 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 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 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.

// client of EJB(session bean)
//standalone client
import javax.naming.*;
import java.util.*;
import account.*;
public class accClient
{
public static void main(String arg[])throws Exception
{
Properties p=new Properties();
p.put(Context.PROVIDER_URL,"t3://localhost:7001");
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
InitialContext ic=new InitialContext(p);
accHome ah=(accHome)ic.lookup("accBean");
Scanner sc=new Scanner(System.in);
System.out.print("Provide account no:");
int x=sc.nextInt();
System.out.print("Provide account Pin no:");
int y=sc.nextInt();
accRemote ar=ah.create();
System.out.println("Created...");
String str=ar.validate(x,y);
System.out.println("Ur information is "+str);
if(str.equals("Valid"))
System.out.println("Balance :"+ar.getBalance(x));
}
}


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

//Bean Class
package account;
import javax.ejb.*;
import java.sql.*;
public class accBean implements SessionBean
{
SessionContext sc;
int bal;
public void ejbCreate(){}
public void ejbRemove(){}
public void ejbActivate(){}
public void ejbPassivate(){}
public void setSessionContext(SessionContext s)
{
sc=s;
}
public String validate(int ano,int pno)
{
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection con=DriverManager.getConnection("jdbc:odbc:dsn1","system","pintu");
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery("select * from account where accno="+ano+" and pno="+pno);
if(rs.next())
{
bal=rs.getInt("balance");
return "Valid";
}
}
catch(Exception e){System.out.println(e);
}
return "Invalid";
}
public int getBalance(int ano)
{
return bal;
}

 
ejb-test.jsp
 
<!-- Web client of EJB -->
<%@ page import="javax.naming.*,java.util.*,account.*" %>
<html>
<body>
<form>
|<h1>Account No<input type="text" name="t1"></h1>
<h1>Pin No<input type="text" name="t2"></h1>
<input type="submit" value="submit">
 </form>
<%
 String st1=request.getParameter("t1");
String st2=request.getParameter("t2");
if(st1 !=null)
       {
Properties p=new Properties();
  p.put(Context.PROVIDER_URL,"t3://localhost:7001");
p.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
InitialContext ic=new InitialContext(p);
accHome ah=(accHome)ic.lookup("accBean");
  int x=Integer.parseInt(st1);
  int y=Integer.parseInt(st2);
accRemote ar=ah.create();
String str=ar.validate(x,y);
  if(str.equals("Valid"))
   {
  %>
<h1>Balance :<%= ar.getBalance(x) %></h1>
<%
  }
  else
   {
 %>
  <h1>Invalid Information </h1>
   <% } }%>
   </body>
</html>

Up Next
    Ebook Download
    View all
    Learn
    View all