Make JNDI variable for context available for Tomcat in Eclipse

I am using Tomcat 8.5.6 inside Eclipse 4.6.1. I have a foo web application project / context that has a JAX-RS endpoint (using RESTEasy 3.1.0.CR3) bar , so I can run Tomcat inside Eclipse and access:

 http://localhost:8080/foo/bar 

I have a variable called foobar that I want to get in my JAX-RS implementation using JNDI:

 final String foobar = (String) new InitialContext().lookup("java:comp/env/foobar"); 

I plan to deploy the WAR produced in production using Tomcat autodeploy. I want to configure foobar variable for Tomcat externally in WAR. How can I do this to test it in Eclipse?

After a lot of reading, I found what I considered $CATALINA_HOME Eclipse: …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\ . So I created a context file for foo in …\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf\Catalina\localhost\foo.xml to fit my project / context and put the following inside it:

 <?xml version="1.0" encoding="UTF-8"?> <Context> <Environment name="foobar" type="java.lang.String" value="123"/> </Context> 

Yes, I know that Eclipse erases this directory whenever I rebuild. But after construction, I saved the file, at least I want to see if it works. This is not true. I get an error message:

 javax.naming.NameNotFoundException: Name [foobar] is not bound in this Context. Unable to find [foobar]. 

I want to at least get it working so that I can know how to do this during production and worry a bit about the problem of deleting the context file in Eclipse. So what have I done wrong? Why can't Tomcat in Eclipse find this JNDI variable?

Note. I do not use the web.xml and do not want to do this; in addition, this variable must be defined outside the WAR in the production deployment.

Update. The good news is that (on Windows 10 Professional Anniversary Edition 64-bit), using the same Tomcat, but offline, I put the same foobar.xml file in the offline Tomcat conf\Catalina\localhost\foo.xml , and my JAX- RS application is great. So, how can I define a JNDI variable in Tomcat inside Eclipse for testing?

0
source share
1 answer

In order for Eclipse + Tomcat to recognize context files for each module, you must enter the server configuration (double-click on the server) and enable the Publish module contexts to separate XML files . This way, Tomcat will use the specific context XML file that you created. Otherwise, it apparently puts them in conf/server.xml and ignores the context-sensitive file you created.

There is still a problem that Eclipse will regenerate this file every time you recreate, destroying all the JNDI variables that you placed there. I am trying to find a workaround fooobar.com/questions/965662 / ... to work, but have not succeeded yet. Anyone have any better ideas?

At least I can reproduce the production environment now, albeit temporarily, until the next restructuring.

0
source

All Articles