Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JNDI Confusion  RSS feed

 
Mike London
Ranch Hand
Posts: 1338
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been doing Java for a while now, but looking at JNDI leaves me stumped.

Is JNDI still used a lot, say, with Tomcat?

I realize JNDI is probably a preferred way to do things, but I'm often left with the question: "But is the complexity (of this particular Java technology)... worth it?"

There don't seem to be any easy JNDI tutorials and the only book that has "tutorial" in the title is 6 years old.

I'm therefore left feeling JNDI really isn't that important...

What do do?

Look forward to any and all replies.

Mike
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

JNDI is still widely used even if there may be no up-to-date books available. I think it's mostly used for J2EE APIs and components (like Tomcat).

Basically JNDI is very helpful in that it provides an unified interface for many underlying protocols and services. There are JNDI provider implementations for a lot of typical lookup services ranging from LDAP to DNS.

Perhaps you should have a look at the online pages at Sun to see for what it's useful. Here's the link to the FAQ and this article explains some common JNDI providers.

Marco
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is JNDI still used a lot, say, with Tomcat?


Certainly, JNDI is one of the most widely used API from Java EE and J2EE.
It is found in the code of every Java-based application server and is also used heavily in distributed enterprise-level applications.

The JNDI API is very stable and very little has changed over the years because of this. There are many sections. If you are building application servers, you will need the SPI (service provider interfaces.) If you are writing distributed applications using messaging or Session EJBs, for example, you will need the client interfaces.

What do do?


It seems like you have not had the need to learn the API. This mostly reflects the type and size of the applications you have worked on up to this point. If you wrote this post, obviously you are interested. Take time, more time, to learn what it is used for and how it is used.

The JNDI Tutorial by Sun Microsystems, Inc.
 
Mike London
Ranch Hand
Posts: 1338
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, lets see if I have this straight.

What I've done so far is below.

I'm assuming now that if I create a Servlet or JSP and instantiate the class I created with the JNDI code as described below, things should work, right?

Is this correct?

Please comment!!! (Thanks!)

Mike

-------------------------------------

0. MySQL database exists, "user" table exists.

1. Created a Java Web Project

2. Added JDBC driver to classpath

3. Created class with code like this:





4. Created Context.xml in web application's WEB-INF folder with this:



5. Added following to web application's WEB.XML:

 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike, your second post seems like it would be better for the Apache Tomcat forum. Your first post was about the JNDI API in general.

I'm assuming now that if I create a Servlet or JSP and instantiate the class I created with the JNDI code as described below, things should work, right?


JNDI code is not used for instantiating servlet or JSP objects.

As an aside, web applications that use a relational database are typically better managed with an application server such as JBoss or Websphere, rather that a simple web server. The admin console for Apache Tomcat is not really optimised for managing it's JNDI namespace or system resources.

And this makse sense, because there should be a business/domain layer of objects that have no depedencies upon the web server. Check out the Business Delegate and Data Access Object design patterns for more info.
[ November 17, 2008: Message edited by: James Clark ]
 
Mike London
Ranch Hand
Posts: 1338
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,

Thanks for your reply.

I don't believe my post would be better in the Tomcat forum for the following reasons:

0. My main question remains how to leverage JNDI. I just picked a database example since that's a common usage. I'm assuming JMS would follow a similar method (also an implied part of my posting).

1. I got the code from a JNDI "How To". Yes, this is Tomcat, but the actual focus is connecting via JNDI.

http://tomcat.apache.org/tomcat-5.5-doc/jndi-resources-howto.html

2. As I understand it, most JNDI for databases works with app servers. Whether I use Tomcat or JBOSS, aren't the underlying concepts pretty much the same?

3. The design patters you referenced I'm sure would be useful, but I think you're missing the point that I'm trying to just get the "hello world" JNDI example working.

There don't seem to be any real tutorials (complete examples) that I can find and all the books seem to be ancient.

Hence my posting.

Thanks.

Mike
 
Mike London
Ranch Hand
Posts: 1338
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,

I should have also mentioned in my last posting that I wasn't going to use JNDI to instantiate ... anything.

Rather, (pardon the apparent confusion that posting), I was going to use a Servlet or JSP to instantiate the class whose code I posted to see if I can get back a list of users.

Of course, I was also really asking if what I had in the various files was (looked) correct.

Still waiting to hear that too (the main point of that posting).

Thanks.

Mike
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I think it would be better to learn JNDI with the API rather than the XML-based approach in Tomcat's implementation. The former will allow to you understand JNDI in other contexts, code/software wise.

The XML data that you put in these XML-based configurtation files is fed into the server's objects and used to create the application's JNDI objects. By only learning XML data without understanding the API itself, you may not truly appreciate the need for it.
 
Mike London
Ranch Hand
Posts: 1338
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James,

Creating a Servlet and adding the two lines below to the doGet() method, correctly gets the user listings using JNDI.

// now display the user listing using JNDI
TestJNDI userListing = new TestJNDI();
userListing.listUsers();


BTW, I'm familiar, and have used, the DAO pattern, but, again, my purpose here was to get a basic "Hello World" JNDI connection working.

I'm getting ready to use JMS in a project, but wanted to make sure I understood JNDI a bit more first with a simple JDBC connection.

It's a shame few, if any, complete examples exist for this "essential technology".

Thanks again

Mike
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike,

JNDI API is a service similar to the JDBC API. As in JDBC, where you have JDBC drivers which provide the service, there are JNDI service providers too. Most of the times, application servers act as JNDI service provides (since they have it inbuilt).

Context initCtx = new InitialContext();


This is the most commonly used and recommend approach to create a JNDI context. If you look at the InitialContext javadoc, it also has an alternate constructor which accepts a Properties object. If you are using the default constructor, a jndi.properties file will be searched in your classpath. This file will contain information specific to the JNDI provider. This piece of information is neccessary to instantiate the context object correctly. This is similar to the JDBC service where you do a Class.forName(....) to load the JDBC driver.

The jndi.properties contains the following information (an example from JBoss):



As you can see, this information helps locate the type and the location of the JNDI provider. So your single statement where you create the InitialContext is going to parse all this information and connect to the appropriate JNDI server.

And later on when you use the lookup() API of the Context interface, the objects are looked up on the corresponding JNDI server.
 
Mike London
Ranch Hand
Posts: 1338
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran,

It doesn't seem, from your reply, that you read the whole thread, but I am creating a JNDI connection as you suggested for JDBC.

For testing the JNDI logic, however, in a "Hello World" type app, I created a servlet that instantiated the class that would create the JNDI connection.

I think you were responding to my Servlet code that simply instantiates the JNDI class and not the entire thread that describes the JNDI logic I'm using.

------

The actual (now working) JNDI code portion is (in the referenced TestJNDI class):



Mike
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike London:
Jaikiran,

It doesn't seem, from your reply, that you read the whole thread, but I am creating a JNDI connection as you suggested for JDBC.



I did read the entire thread before posting, but to be honest did not pay a lot of attention

And reading this thread again, i am now confused about what the actual question is
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!