GeeCON Prague 2014*
The moose likes Tomcat and the fly likes Problem accessing a file resource from a web app on Tomcat server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Products » Tomcat
Bookmark "Problem accessing a file resource from a web app on Tomcat server " Watch "Problem accessing a file resource from a web app on Tomcat server " New topic
Author

Problem accessing a file resource from a web app on Tomcat server

prabhodh mekala
Greenhorn

Joined: Jan 18, 2008
Posts: 4
Hi ,

I want to develop a web app using grails ,which should invoke/execute a VBScript file. I have placed VBScript file in WEB_INF/resources folder.

I am using Runtime.getRuntime().exec() to invoke the VBscript .

I am accessing the VBSCript either by using
def vbPath = getServletContext().getRealPath("WEB-INF/resources/" + "calling.vbs");
or
String path = new File(getServletContext().getRealPath("WEB-INF/resources/" + "calling.vbs")).getAbsolutePath()
The above path values are working fine on Jetty server. but the same is not working on Tomcat. Though the above path values are returring the corrct values on both cases.

For local(Jetty) deployment the path value returning is : C:\Grails\workspace\MyProject\web-app\WEB-INF\resources\calling.vbs
On Tomcat deployment tha path vaue returning is : C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\MyProject-0.1\WEB-INF\resources\calling.vbs

I am thinking that, we can not access a file from Servlet/Web .
Could you please some one advise me how to accesss a file from a webservice on tomcat. It may be a local resource or WEB-INF folder resource on deployment machine.

Thanks.

Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16103
    
  21

According to the J2EE standard, a deployed webapp is a WAR (or EAR) file, both of which are in JAR (ZIP) format.

The getRealPath() method is therefore treacherous, since there's no actual filesystem path to resources that are stored within the WAR, only a path to the WAR itself.

While Tomcat often does "explode" WARs into actual directories and files, it's not a good idea to create webapps that depend on this. You are better off putting your script in an external directory and directing the webapp to that location. If you prefer, you can make the actual filesystem path of that script or its directory be a configuration option (in web.xml) or a JNDI object.

Customer surveys are for companies who didn't pay proper attention to begin with.
prabhodh mekala
Greenhorn

Joined: Jan 18, 2008
Posts: 4
Tim Holloway wrote:According to the J2EE standard, a deployed webapp is a WAR (or EAR) file, both of which are in JAR (ZIP) format.

The getRealPath() method is therefore treacherous, since there's no actual filesystem path to resources that are stored within the WAR, only a path to the WAR itself.

While Tomcat often does "explode" WARs into actual directories and files, it's not a good idea to create webapps that depend on this. You are better off putting your script in an external directory and directing the webapp to that location. If you prefer, you can make the actual filesystem path of that script or its directory be a configuration option (in web.xml) or a JNDI object.


Hi Tim,

I have placed the VBScript file on local directory , I have read it through String path = new File("C:\\TEST\\calling.vbs")).getAbsolutePath(); . On dev environment on Jetty server, the web app is working fine. But when I make a war file and deploy on Tomcat server, the web app never works.

Please could you provide some sample example on how to do it?

Thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16103
    
  21

While technically, there's nothing wrong with what you coded (although I prefer the "unix" filename syntax to avoid Backshlash Hell), it's kind of overkill. All you should need is:


Add suitable environmental settings, exception handlers, and stdio redirectors as needed.

Calling getAbsolutePath() on a File created from an absolute path just does a lot of work and then undoes it. getCanonicalPath() might be different, but that method doesn't get used as often.
prabhodh mekala
Greenhorn

Joined: Jan 18, 2008
Posts: 4
Hi ,

I have noticed a strange behaviour with Tomcat 7.0.23. First I have installed the Tomcat 7(Windows service installer), I have deployed my web application on the Tomcat server. My web application failed to access the local resource file. My application main functionality is : it executes a VBScript, the VBScript inturn calls another Execl file. The Excel file converts the given excel sheets into xml files.
so the flow is WebApp > Runtime.getexec(VBScript) > Excel file > Exel file to XML.

VBScript and Excel files are placed in war file. I am reading the path of these files using getServletContext.getRealPath("\").

But fortunately I have downloaded the Tomcat 7(zipped version). I have extracted the zip version and started the tomcat manually. Now I have deployed my web app on the zipped version of tomcat, my application works as expected. It was executing the VBSCript file, then Exel file then converted the given exel sheet XML.

So now my question is "is there any difference between tomcat.exe vs tomcat.zip versions". How can I make my application running on the Tomcat7(exe or Wiondows Service Installer). Because our deployment server is Tomcat(Windows Service Installer version).

Your help could be appreciated.

Thanks,
Regards,
Prabhodh.
 
GeeCON Prague 2014
 
subject: Problem accessing a file resource from a web app on Tomcat server