Source Code : Explicityly Set Context with JNDI

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Explicityly Set Context with JNDI


File: HelloServiceBean.java


import javax.ejb.Stateless;

@Stateless
public class HelloServiceBean implements HelloServiceLocal, HelloServiceRemote {
    public String sayHello(String name) {
        return "Hello1, "  + name;
    }
}

    


File: HelloServiceLocal.java



import javax.ejb.Local;

@Local
public interface HelloServiceLocal {
    public String sayHello(String name);
}



File: HelloServiceRemote.java




import javax.ejb.Remote;

@Remote
public interface HelloServiceRemote{
    public String sayHello(String name);
}



File: jndi.properties

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=localhost:1099


File: Main.java

import java.util.*;

import javax.naming.*;


public class Main{

   public static void main(String[] a) throws Exception{
    /* get a initial context. By default the settings in the file
     * jndi.properties are used.
     * You can explicitly set up properties instead of using the file.
     * 
    
    Properties properties = new Properties();
    properties.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
    properties.put("java.naming.factory.url.pkgs","=org.jboss.naming:org.jnp.interfaces");
    properties.put("java.naming.provider.url","localhost:1099");
     */   
        String name = "java2s";
        HelloServiceRemote service = null;
        

        //Context compEnv = (Context) new InitialContext().lookup("java:comp/env");
        
        //service = (HelloService)new InitialContext().lookup("java:comp/env/ejb/HelloService");
        service = (HelloServiceRemote)new InitialContext().lookup("HelloServiceBean/remote");
        System.out.println(service.sayHello(name));
 
   }

}




           
       

Thank with us