3

My WAR is deploying fine in TomEE under the correct context root "/myApp" so that I can go to http://localhost:8080/myApp

I have some static files under: /myApp/WEB-INF/files

From looking at the exploded WAR the files are definitely there. My problem is when in code I have written servletContext.getResource("/WEB-INF/files/MyFile.json")I am getting an error that the file is not found because it appears the actual path in the logs is: /localhost/myApp/WEB-INF/files/MyFile.json

This code was actually working when I had the app in a regular tomcat server, in Google App Engine and even in WebSphere 7. In TomEE I am NOT deploying the app under webapps but under wtpwebapps. I've done searches for context configuration but most of those deal with making the app have a context root of "" (making it the root app).

I can write some code to remove the "/localhost" part of the URL but WHY is it being placed in there?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Justin
  • 859
  • 4
  • 15
  • 30

2 Answers2

2

That looks like one of Tomcat's internal jndi URLs which takes the form:

jndi:/hostname/contextPath/pathInWebApp

It looks like you have found a TomEE bug.

Mark Thomas
  • 16,339
  • 1
  • 39
  • 60
  • Yeah I noticed the 'jndi;' part added to the URL as well but figured it was normal. Would you mind elaborating why that is a bug just so I can understand what's happening? – Justin Nov 27 '13 at 15:31
  • When you call ServletContext.getResource() Tomcat returns a URL. Tomcat uses a custom jndi scheme because the resources implementation was written to the JNDI API so there was a standard API folks could use to create their own implementations. Tomcat ships with an implementation for WARs and directories. Custom implementations never really happened but the JNDI based approach remained until Tomcat 8 where it has been replaced. Anyway, Tomcat has a custom handler for JNDI URLs and it looks like TomEE isn't quite getting that right. I'm not exactly sure why, but that is where I'd start looking. – Mark Thomas Nov 28 '13 at 15:23
  • BTW I switched to JBoss EAP 6.1 (a.k.a JBoss 7.x) and doing the servletContext.getResource(..) doesn't work. Looks like Jboss is doing "/default-host/myApp/WEB-INF/files/MyFile.json" so the same thing as tomEE. Since I'm seeing this pattern I wonder if it's something else? The path /WEB-INF/files/MyFile.json is mine, the rest is added in. Since its a WAR I have tried putting it in classes/WEB-INF instead of just base /WEB-INF (via Maven build) with no luck. – Justin Dec 12 '13 at 14:48
1

I had the same problem, trying to access the log4j.properties file inside the WEB-INF folder. It worked in my local tomcat which is tomcat 8 but failed with the FileNotFoundException on the production server, which runs tomcat 7. So it's not TomEE. I ended up doing this to make it work:

String filePath = context.getRealPath("/") + "/WEB-INF/config/log4j.properties";

That gives me the full path. In my case it was

/home/tomcat/tomcat7/webapps/MY-APPLICATION
Mikayil Abdullayev
  • 12,117
  • 26
  • 122
  • 206