Search code examples
javajndi

What is JNDI and do I need it for website building


I am learning Java and Spring for websites. A few times I see the word JNDI. What is it and do I need to learn it or do I need it in the process of building a web application?


Solution

  • JNDI is a convenient way for an application to say "I need resource X" and for container (e.g. Tomcat) to say "resource X is available, look here or there where X is actually configured".

    You could use above mechanism to avoid, for example, hardcoding database properties in your code. You could achieve the same by having a configuration file, but some contexts (e.g.: .war files) configuration files are not persisted between redeployments so they are not an option.

    Good examples on what is being bound to JNDI would be database data sources, references to SMTP email servers, security realms and so on.

    Think of JNDI as a HashMap where key is a String and value is either a concrete object or another HashMap of the same structure. So locating a value like jndi://a/b would resolve to going to the top-level map, finding key a and expecting it's value to be another map. Then key b would be looked up in that sub-map and relevant value returned to the user.