I am trying to use relative paths in a Dynamic Web Project in Eclipse. I am NOT using them in the Servlet, but in another class that is called by the Servlet. The file I am trying to access to is a property file located in
MyProject/WebContent/WEB-INF/propertyFile.properties
I have tried almost every relative path...
WebContent/WEB-INF/propertyFile.properties
/WebContent/WEB-INF/propertyFile.properties
./WebContent/WEB-INF/propertyFile.properties
MyProject/WebContent/WEB-INF/propertyFile.properties
/MyProject/WebContent/WEB-INF/propertyFile.properties
WEB-INF/propertyFile.properties
/WEB-INF/propertyFile.properties
./WEB-INF/propertyFile.properties
...and so on...what can I do?
Thanks in advance!
You nee to understand that file IO read files, from the filesystem, and relative to the directory from which the JVM (i.e. your web container) is started.
Remember that, once your app is deployed to production, there won't be any MyProject or WebContent folder. That's what exists on your development machine. The only thing that will exist in production is the war file deployed in the application server.
What you actually want is to load a resource, located in the deployed web application (i.e. which is inside your war file).
To do that, you must use ServletContext.getResourceAsStream():
InputStream in = servletContext.getResourceAsStream("/WEB-INF/propertyFile.properties");
Related
I am trying to create a file using below code in a Servlet:
File outfile= new File(servletContext.getRealPath("/Output/output.xml"));
OutputStream os = new FileOutputStream(outfile);
from a J2EE Application (CallMain) and the file gets created in deployed temp path
.metadata.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\CallMain\Output
Actually I want the file to be created in current project folder: WebContent.
Actually when you ran the servlet within eclipse using the embedded tomcat server
getServletContext().getRealPath("WEB-INF")
Gives you this
D:\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\DynaServletProject\WEB-INF
But when you ran the servlet from standalone Apache Tomcat:
getServletContext().getRealPath("WEB-INF")
This gives you
D:\apache-tomcat-7.0.33\webapps\DynaServletProject\WEB-INF
While in eclipse you can give the absolute path i dont think this is the best approach though
By default, your application stages and runs from a directory buried in the workspace's .metadata directory. That is the "real path" at runtime. Check your Server's configuration for options regarding deploying the app directory from the workspace folders.
I am able to access a resource(say test.html) only when it is directly under WebContent in eclipse.If i place it in WEB-INF or WEB-INF/jsp the IDE shows error 404-requested resource is not available.
http://localhost:8080/MyApp/test.html (This works when test.html is under WebContent and not any of it's subdirectory)
When it is in WebContent/WEB-INF
Right click-run takes to the below URL which shows 404-resource not available
http://localhost:8080/MyApp/WEB-INF/test.html
How do i access it by placing "test.html" in WEB-INF or WEB-INF/jsp?
All configurations are default with no restrictions specified in web.xml and using Tomcat 7 as the web server
To prevent access to specific files. You cannot access /WEB-INF/ directory from outside the server itself. If you want to create subdirectory create them under
WebApp/
-index.jsp
-YourSubdirectory/
--test.jsp
test.jsp can be accessed via localhost:8080/MyApp/YourSubdirectory/test.jsp
Files at WEB-INF directory cannot be accessed directly from browser. This is the basic restriction.
However content of this directory could be accessed at application level. Web container provides such opportunity.
For example, if you using SpringMVC, you can map some URL to the file at WIB-INF directory and it will work correctly.
I am having big issues deploying my web app to Tomcat 5.5
My server side code must access files found under a directory parallel to my WEB-INF folder.
When on developement mode, a simple relative path (mydirectory/myfile) works prefectly.
When deployed, the sayed path does not work anymore I dont know why.
Is there a way to make it work without using any absolute path?
Here is my War directory
War
WEB-INF
Mydirectory
Myfile
Mywebbapp.html
Obtain your root folder relative to contextRoot by calling javax.servlet.ServletContext.getRealPath("/"). Then proceed with this path...
Using relative path will work, but you need to be aware of what the containers considers to be the root directory and base your relative path from there. If you specify your directory structure better, I can help you with the path - Basically try using the .. operator to move from the container root to the needed directory
I wanted to know one thing. When deploying a GWT application on
the server, lets say an application in which there are client files
Greeting.java(entrypoint), service.java,serviceAsync.java and server
file serviceImpl.java(), only the client files will converted to
javascript. After compilation, I take the war file and place it on the
server, but where to place the server file that is serviceImpl.java???
The war file will also contain a /WEB-INF folder with the web.xml your libs and the other java classes needed for the server side.
So you only need to copy your war file in the proper webapps folder from your server.
I am building a web service and am packaging it into a war file for deployment. Right now all of my config files (.properties and .xml) are being packaged into my .war file. This isn't going to work as some of these files will need to be modified for each individual installation. I know that some servlet containers will leave the .war files intact which would mean the config files would never be easily modified. My question is this: what is the best practice for deploying a .war file with these external config files? I'm thinking that the config files will need to be shipped separate from the .war file and placed into a directory that is in the classpath. Is there a default directory setup like this in Tomcat that these files can just be dropped into and my web service will be able to find without much trouble?
Maybe I shouldn't be using a war file for this setup? Maybe I should just be providing a zip file (with the same contents as the war file) and the deployment will simply be to extract the zip into the webapps directory?
I do not know any default directory in Tomcat to store configuration, my
attempts to solve the same issue have been :
1 - Move configuration to the DB and provide scripts or webpages to modify values.
2 - Have a script to deploy the war. The script would merge configuration from a user directory into web.xml or other deployed config files.
3 - Have webapps look first in a user directory for configuration and
if not found then look for configuration files deployed by the war.
Least favorite is 3 - it require all webapps to check two places for configuration and
you end up with two different xml files on the server with different values and it is not always clear which one is used.
Next favorite is 2 - the webapps can be written without knowledge of multiple config files, but you run into issue when someone does a deploy from Tomcat manager instead of using your script.
Favorite is 1. This just works in most cases. Problem is when you don't have a DB or
want to configure how you connect to the DB.
If having the file visible from all webapps is not an issue, you could put it $CATALINA_HOME/lib.
One solution is to modify property file after deployment of war file is to use ServletContext.getRealpath() method to get the real path means path of file in the server where it is deployed and then modify that file it will modify file in container only not the original file. So you need to backup it if it is important modification for you. So by this you do not need to redeploy war file as it is already modifying file from deployed container.
This solution can edit a file that is in webpages folder also from the java class.
If you want more description or how to do it then let me know i have did it.