I am uploading images to jboss server by getting the absolute path using the following code
getServletContext().getRealPath("");
The uploaded image is moved to the absolute path and I can access the image using http://test.com:8080/image.jpg
My problem is the image is being uploaded to the tmp directory of jboss server, so i am losing the uploaded images in the next deployment.
I tried uploading the image to various paths to make it work
\jboss-5.0.1.GA\server\default\deploy
and here \jboss-5.0.1.GA\server\default\work\jboss.web\localhost as well
But fails, I cannot access the image using http://test.com:8080/image.jpg
Kindly help me out in this...
You can add a new context to specify a path to access an external folder.
Steps for Jboss 4 and older versions:
Open your file /YOURINSTANCE_JBOSS/deploy/jboss-web.deployer/server.xml.
Define a new Context in the tag <Host name=”localhost” ...>
Example:
<Host name=”localhost” ...>
<Context path=”/myfolder” docBase=”/home/username/my_images” reloadable=”true”></Context>
Where /myfolder will be the path that you are going to use to access your files, and /home/username/my_images the folder where you are going to upload your pictures.
Restart JBoss
Now you will be able to access your files with the next path:
http://yourserver:yourport/myfolder/filename
Steps for Jboss 5:
Create a new file named context.xml into your WEB-INF folder with the next content:
<?xml version="1.0" encoding="UTF-8"?>
<Context allowLinking="true" cookies="true" crossContext="true" override="true">
<Resources allowLinking="true" className="YOUR_PACKAGE.MyResources" homeDir="/home/username/my_images" />
</Context>
Where className is the java class that will access the resources and homeDir your external directory.
According to this link create a new class to access your resources defined in the file context.xml
Example:
public class MyResources extends FileDirContext {
}
Now you will be able to access your files with the next function:
request.getServletContext().getResourceAsStream(uri);
Steps for Jboss 5 and older versions:
Create a new file named context.xml into your WEB-INF folder with the next content:
<?xml version="1.0" encoding="UTF-8"?>
<Context allowLinking="true" cookies="true" crossContext="true" override="true">
<Resources allowLinking="true" homeDir="/home/username/my_images" />
</Context>
Where homeDir is your external directory.
Create a symbolic link: YourDeployedProject.war/myfolder linked to /home/username/my_images
Windows:
mklink /D C:\YOUR_JBOSS_SERVER\server\default\deploy\YourDeployedProject.war\myfolder C:\users\YOURUSER\my_images
Linux:
YourDeployedProject.war# ln -s /home/username/my_images myfolder
Now you will be able to access your files with the next path:
http://localhost:8080/DeployedProject/myfolder/filename
Steps for Jboss 7:
JBoss 7 doesn't allow any of the methods for the previous JBoss versions, so the easiest solution is to implement a Servlet to access your files like in the next link.
Related
I am new to Tomcat deployment.
I found in Tomcat's document and it says this about <Context> tag's docBase attribute:
The value of this field must not be set unless the Context element is defined in server.xml or the docBase is not located under the Host's appBase.
Here is my problem:
I create a <Host> tag inside server.xml and set appBase="C:/xxx/"
create a xml file named ROOT.xml inside C:/xxx/ and add <Context docBase="../yyy" />
put index.html inside C:/yyy/
Why is it doesn't work when I try to browser localhost:8080/ ?
Doesn't it supposed to show the index.html inside C:/yyy ?
Summary
When you add appBase="C:/xxx" to server.xml, that means all your web appplications will be found in that directory. At that point you cannot then have a specific web app which defines its own redirection to another directory (a custom docBase) from within its own web application location. At that point, its docBase location has already been set to be the appBase directory.
A Different Approach
The ROOT.xml file that you created can be used to point to a location which is different from the appBase - but you cannot place that file inside the appBase directory (as noted above).
Instead you have to place it in $CATALINA_BASE/conf/[enginename]/[hostname]/ as described in this documentation page.
So, for me, that would be:
C:\tomcat_9_test\conf\Catalina\localhost\ROOT.xml
And its contents would simply be:
<?xml version="1.0" encoding="UTF-8"?>
<Context docBase="C:/yyy" />
Because this is outside of the docBase, this specific application can have a different location from all the other web applications in docBase.
Now, your C:\yyyy\index.html file will be served at http://localhost:8080/, as expected.
A Note on context.xml
What if you do not use this ROOT.xml file at all, but instead you create a ROOT folder in C:\xxx and then create a META-INF folder there, containing a context.xml file?
You could define <Context docBase="../yyy" /> in there. But it would be ignored (again, because the docBase is already set).
I have implemented an image uploading functionality. The problem is that I am saving these files outside an application (so that they won't be deleted in case of redeploy), and I don't really know how to refer them from a jsp file then, as I only know how to refer to the webapp (or resources) directory.
Have you tried adding to server.xml under $CATALINA_HOME/config/server.xml
<Context docBase="/usr/local/tomcat/folder/with/images" path="/uploads/img" />
For example:If you have foo.jpg inside the
/usr/local/tomcat/folder/with/images directory
then you can access the foo.jpg file via
localhost:8081/uploads/img/foo.jpg
How can I deploy mywebapp-1.0.0.war to $TOMCAT_HOME/webapps directory with context path /mywebapp using context.xml inside the war file on Tomcat 8?
I'm getting back to work with Tomcat after long time since version 5. I'm used to create META-INF/context.xml inside my war file:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mywebapp">
...
</Context>
Maven creates a war file with this name: mywebapp-1.0.0.war
But when I deploy the war file to $TOMCAT_HOME/webapps directory the context path will be http://localhost:8080/mywebapp-1.0.0 instead of http://localhost:8080/mywebapp/.
Also I see that $TOMCAT_HOME/conf/Catalina/localhost is empty, instead of having the xml file copied from the war file deployed.
I also added to $TOMCAT_HOME/conf/server.xml the deployXML="true"
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true"
deployXML="true">
It is not possible to put a war inside the webapp directory and set the path attribute in META-INF/context.xml file at the same time. Tomcat 8 documentation, clearly says about this attribute:
This attribute must only be used when statically defining a Context in server.xml. In all other circumstances, the path will be inferred from the filenames used for either the .xml context file or the docBase.
Can't you just rename your war file to mywebapp (via Maven or else) so that Tomcat will deploy it under /mywebapp ?
Thanks for your research j2gl!
I've found out that good way how to achieve both .war file with full name with version and deployed short path is to use Tomcat manager API. For example through Tomcat7-maven-plugin.
I have application which is having lots of PDF files , when I try to build a war file of my application due to pdfs its size converts to Gbz , so basically I want to remove the pdf file folder from the application ,put it some location and then build a war file and deploy it to tomcat.so ,is there any way I can read folder from my application which is outside the war file?
You can use the File class to work with files and directories on the local file system.
For example if if your pdf files are in a folder called pdf you can create a File object like so
File pdfsFolder = new File("\pdfs");
You need to have a separe folder on your server to store the pdf. You pass this folder location to your application (for example using globaresouces in server.xml, so your production path and local path are coming from the server rather than hardcoded in your app.). You access the given folder normally from java (new File(dir), Paths.get...). If there is no SecurityManager you can access any part of the filesystem from within your webapp.
For example your aplication META-INF/context.xml
<Context>
<ResourceLink global="PDFPath" name="PDFPath" type="java.lang.String"/>
</Context>
Your tomcat, inside server.xml
<GlobalNamingResources>
<Environment name="PDFPath" value="D:\\Data\\PDFs" type="java.lang.String" override="false"/>
</GlobalNamingResources>
In your code:
Context initCtx = new InitialContext();
Context envCtx = (Context)initCtx.lookup("java:comp/env");
String storagePath = (String) getContext().lookup("PDFPath");
File pdfDir = new File(storagePath);
ya its correct way but give the Tomcat user permission to access the Pdf file , which is use to avoid the file listing when the time of directly url access Ex (www.yourSite.com/pdf/ex.pdf)
Is there a way to set a relative path to the docBase attribute in the context.xml of a web application, so it is outside of the appBase directory of the tomcat server instance?
I would like to be able to share the context configuration between computers and have the app living in a directory, not a war file. That way i can compile the classes directly into that directory (in my project development directory) and have tomcat use these classes without any copying/packaging needed.
I am using the tomcat 8.0.0-RC5.
My directory Layout is:
/home/david/projects/frontend/web-content <-- the static html files
/home/david/projects/frontend/web-content/WEB-INF <-- the WEB-INF with the web.xml
/home/david/projects/tomcat <-- tomcat base directory
/home/david/projects/tomcat/Catalina/localhost <-- holds the frontend.xml context configuration
I have tried
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/frontend" docBase="../../frontend/web-content">
</Context>
but that did not work. The whole path before /web-content seems to be ignored. The log says:
The main resource set specified [/home/david/projects/tomcat/webapps/web-content] is not valid
The Tomcat 8 documentation for the context container says:
You may specify an absolute pathname for this directory or WAR file, or a pathname that is relative to the appBase directory of the owning Host.
Does relative here mean a strict subdirectory of appBase (no .. allowed)?
Setting an absolute path works without problems. The configuration
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/frontend" docBase="/home/david/projects/frontend/web-content">
</Context>
works, but it is specific to my computer. So I cannot share the context configuration without modification anymore.
I could create a symbolic link inside the appBase directory of the tomcat server and let it point to the web-content folder of my application. This would work, but I would have different configurations (symbolic links) on linux and windows machines.
Just taking only the last name of relative path names and ignoring the first parts
is most likly a bug in tomcat. Relative path names should work or must throw errors.
But ignoring parts is bad.
A workaround could be using an absolute path name like this:
<Context docBase="${catalina.home}/../www.war"
I just reading the changelog of Tomcat 8.0.15.
The bug should be fixed(!):
"Correctly handle relative values for the docBase attribute of a Context."