Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this javax.naming.NamingException related to error generated by Tomcat 7.0?

 
Mark Curlette
Ranch Hand
Posts: 46
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mark Curlette
Ranch Hand
Posts: 46
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using resource-ref? That's commonly used to reference factory objects.
 
Mark Curlette
Ranch Hand
Posts: 46
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 17989
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Boy Thomas
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting the same error.
context.xml
--------------------
<Environment name="ets/pollInterval" value="4000000"
type="java.lang.String" override="false"/>
<Resource name="timer/TestTimer" auth="Container"
type="commonj.timers.TimerManager"/>

web.xml
--------------------
<resource-ref>
<res-ref-name>timer/TestTimer</res-ref-name>
<res-type>commonj.timers.TimerManager</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>
<env-entry>
<description>ETS Polling Interval, in milliseconds.</description>
<env-entry-name>ets/pollInterval</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>600000</env-entry-value>
</env-entry>

java class
--------------------
InitialContext ic = new InitialContext();
TimerManager tm = (TimerManager)ic.lookup
("java:comp/env/timer/TestTimer");
// Execute timer every 10 seconds starting immediately
tm.schedule (new MyTimerListener(), 0, 10*1000);
out.println("<h4>Timer scheduled!</h4>");

Any help is greatly appreciated.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic