File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Getting Initial Context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Getting Initial Context" Watch "Getting Initial Context" New topic

Getting Initial Context

ravi inguva
Ranch Hand

Joined: Feb 07, 2002
Posts: 34
I have a doubt for getting the initial context is it always required to metion the properties specific to appserver as below
if (initContext == null) {
try {
Properties properties = new Properties();
// Get location of name service
bundle.getString(“providerUrl” );
// Get name of initial context factory
bundle.getString(“nameService” );
initContext = new InitialContext(properties);
} catch (Exception e) {
// Error getting the initial context…
to get the reference of an EJB
but i followed a process by just creating the initial context object as below in a JSP or servlet
InitailContext ctx = new InitailContext()
it worked for me without specifying the properties

can anybody say me the advantages and disadvantages of both the approaches and also which the best approach
Thanks in advance

Sun Certified Java2 Programmer
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
If you are using the JNDI tree in the same appserver as your application is running, then it's of course more simple to use just new InitialContext().
But if you want your application to be more flexible (i.e. not prevent your application to fetch the EJB from another appserver instance without code changes), you should use the properties-constructor and read the properties from somewhere dynamically/cached/etc.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Chris Mathews
Ranch Hand

Joined: Jul 18, 2001
Posts: 2712
The file is used to allevate this platform dependency. See this link:
Best thing about this is that it doesn't require additional programming to read from the properties file.
I agree. Here's the link:
subject: Getting Initial Context
It's not a secret anymore!