.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> General >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Java naming and directory interface

Posted By:satyapriyanayak       Posted Date: November 01, 2014    Points: 200    Category: General    URL: http://www.dotnetspark.com  

In this article we will know about JNDI (Java naming and directory interface).
 

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 these applications can be available in one machine or different machine in the network. Both these application use an interface between each other called as SPI (service provider interface). SPI is a part of application server. All EJBs must register a unique name called as JNDI name during their deployment. The client application search the machine of application server by using IP address, port no and the protocol of 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 reference of home interface. The client can use the reference of home interface to call create () method and to get the reference of remote interface. The reference of remote interface can be used to call business method.


Creation process of EJB client

1.Create an object of java.util.properties by using its default constructor.

2.Use 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>


 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend