• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Getting Initial Context

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
properties.put(javax.naming.Context.PROVIDER_URL,
bundle.getString(“providerUrl” );
// Get name of initial context factory
properties.put(javax.naming.Context.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
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The jndi.properties file is used to allevate this platform dependency. See this link: http://java.sun.com/products/jndi/tutorial/beyond/env/source.html.
Best thing about this is that it doesn't require additional programming to read from the properties file.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic