aspose file tools*
The moose likes Tomcat and the fly likes re: Is this  javax.naming.NamingException related to  error generated by Tomcat 7.0 ? and why ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Bookmark "re: Is this  javax.naming.NamingException related to  error generated by Tomcat 7.0 ? and why ?" Watch "re: Is this  javax.naming.NamingException related to  error generated by Tomcat 7.0 ? and why ?" New topic
Author

re: Is this javax.naming.NamingException related to error generated by Tomcat 7.0 ? and why ?

Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

Hello,

I am new to Tomcat , and I tried to create a school project using eclipse and Tomcat 7.0.

I received a javax.naming.NamingException: Cannot create resource instance with root cause which I think it may relate to the Tomcat server. I tried to solve it but with my limited understanding of the Tomcat server, i was not able to find a good explanation for the problem.

So i hope if you could guide me to why did i have this kind of error and how to resolve it.

My simple jsp is calling a resource reference which i declared in the web.xml file




My web.xml file which contains the above resource reference as following





I don't know why it gave the error since the jsp is calling a resource reference, and the resource reference is defined in the web.xml. The error is as followed:




I read some related article and it suggested that I need to declare the resource ref in context.xml. I don't why since I already declared it in web.xml. Is is not enough for Tomcat 7 to recognize?

If i declare it again in context.xml, then should it be exacly the same as I declared it in web.xml ?? such as the following :




Thanks so much in advance for your consideration.

Tom
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

I'm going to let Bear growl at you for using scriptlets in the first place. Here's some info on JNDI and Tomcat.

You define a JNDI resource in web.xml, that is true. Here's one of mine:



You can optionally set an override in the Tomcat Context definition, but the syntax is different:



Note that the "override" attribute in Context indicates when web.xml overrides the Context, so it's the reverse of what you might think.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

Hello Tim,

Thanks for the pointer, I add the resource name reference to my context.xml but somehow Tomcat still gave me the error message saying that " javax.naming.NamingException: Cannot create resource instance with root cause".

I did not know why it did not work as it should. Please take a look to see if you could find anything unusual.

Thanks so much in advance for all of your help!!

Tom

my web.xml again is as following :




My context.xml is as the following:




and finally the following run time exception error saying it can not create the resource reference

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Why are you using resource-ref? That's commonly used to reference factory objects.
Mark Curlette
Ranch Hand

Joined: May 26, 2011
Posts: 46

Thank you very much Tim. You are absolutely right. I got rid of resource reference and replace it with your env-entry and it worked well.
I was able to Lookup: 4000000 from both of my jsp and eclipse console.

I would like to ask you an additional question if you don't mind relating to a class project which simply print out a stock quote and email in every ten minutes. It requires using the servlet context listener.

I would like to ask you please check my logic and please correct me if you spot any flaw in my logic.

Thanks so much for all of your helps.

I plan to do the following :

1. Create MyServletContextListener



My question is that can i replace the resource ref in the above code with the env-entry ? Please correct me if i am wrong because I think i need the resource refereence to get to the timer instance of the QuoteTimerListener in the code below.

2. Create a stock QuoteTimerListener which is called by the above MyServletContextListener




3. I have the web.xml defined as :



4. and my content.xml is defined as




Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16145
    
  21

Actually, we'd prefer that when you switch topics that you start a whole new thread. It makes it easier for people to follow.

As far as I can tell, you are attempting to use the ServletContextListener to invokes functions in a servlet. Only web requests ought to do that. What I'd recommend is that the ServletContextListener construct a separate business object to manage the timer-controlled process. And make it an independent thread. The ServletContextListener (unlike servlets) is allowed to spawn threads.
 
Don't get me started about those stupid light bulbs.
 
subject: re: Is this javax.naming.NamingException related to error generated by Tomcat 7.0 ? and why ?