Mapping a directory outside the web-app to URL in TOMCAT - image

I need to map an directory containing images which resides outside tomcat webapps folder, so that application can serve those images.
I am making a J2EE Web application running under tomcat 6. User can upload/delete images in the application. Currenly I store these images to a directory under application's WebContent folder, but I want to take it outside the tomcat (e.g. C:/test/images).
I need to know how to I configure tomcat so that if I access URL http://.com/images/abc.jpg , it serves the image from directory C:/test/images
Thanks,

Add a <Context> tag in server.xml, inside the <Host> tag:
<Context path="/images" docBase="C:/test/images/" />
Docs will be accessible at http://localhost:8080/images

in Tomcat8 you can also add PotsResources to you META-INF/context.xml as follow :
<Context>
<Resources allowLinking="false">
<PostResources readOnly="false"
className="org.apache.catalina.webresources.DirResourceSet"
base="path-to-your-local-folder"
webAppMount="/images"/>
</Resources>
...
</Context>

I had the same issue but found a solution.
If you are using Eclipse and a Tomcat plugin then please note that the Eclipse Tomcat plugin creates a separate CATALINA_BASE under the Eclipse workspace directory.
You can go to this location and you will find server.xml.
Use that server.xml and it will work.
My actual tomcat directory is:
C:\apache-tomcat-7.0.62x64\apache-tomcat-7.0.62\conf
and my Eclipse Tomcat server uses:
C:\workspace\JSF\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\conf
Use the workspace path and server.xml from this location.
Add this in server.xml inside the host tag:
< Context docBase="D:/personal" path="/images" />
and it will work if D:/personal has 1.png, and then the url http://localhost:8080/images/1.png will load the image.

Related

Tomcat context.xml from META-INF folder for context root configuration

I have an application which is being deployed in Tomcat 8. This application needs to be the context root.
To do this, I put a root.xml file in conf\Catalina\localhost folder and it worked.
Now, the problem is that when i debug through IntelliJ idea (Remote host) it is not being deployed as the root application.
Seems like the only way to fix this problem is to add context.xml file in META-INF folder and specify the configuration there.
Now, when I do this, context root configuration from context.xml file is not being picked up.
Any thoughts on this? Ultimately, I just need to debug the application through a remote server.

Can't deploy site on tomcat on mac

I cannot figure out how to deploy a website (not app) on Tomcat on my mac. I just want to have Tomcat serve the file index.html. I tried creating the file myapp.xml in library/Tomcat/conf/Catalina/localhost with the content:
<Content path="/myapp" docBase="/Users/jerryk/Development/myapp" />
But when I attempt to access the site with
"http://localhost:8080/myapp" or "localhost:8080/myapp/index.html"
I get a resource not found error.
http://localhost:8080 brings up the Tomcat control panel.
Any thoughts on what I am doing wrong? FWIW, this works fine on windows.
Thanks,
Jerry
I'm not sure what's going on with Windows, but theoretically, the docBase must point either to a .war file, or a directory that has the same layout as a war file.
So, you can't just have an index.html in that directory, you also need at least a WEB-INF directory with a minimal web.xml in it.
See, for example, http://oreilly.com/java/archive/tomcat.html

Same webapp on Tomcat with different log4j config for each

I've hunted and read, and I think there's no way...but just in case.
I need to deploy the same webapp to Tomcat multiple times, each with a different config that indicates which database to work with. I've figured out how to do that without modifying the contents of the war file.
In short, I copied WebApp.jar to WebAppDB1.jar, WebAppDB2.jar, and deploy these to the webapps folder, and use a custom context configuration for each in tomcat/conf/Catalina/localhost. So I'm good there.
But I would really like for each of these to log to a separate file. As it is, everything goes to catalina.out. It's a Spring app using log4j and slf4j. Every avenue I've explored gets me nowhere.
For now, I'm back to updating the actual war file, going into WEB-INF/classes and updating log4j.xml, but that makes it a manual process.
Any ideas?
You can put the log4j.xml files somewhere in the outside the webapp and use Tomcat's VirtualWebappLoader to load different log4.xml for each webapps.
http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation
It looks like this.
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB1.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB1" />
</Context>
${CATALINA_BASE}/conf/Catalina/localhost/WebAppDB2.xml:
<Context docBase="..." >
<Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/somewhere/WebAppDB2" />
</Context>
And then put log4j.xml files into /somewhere/WebAppDB1 and /somewhere/WebAppDB2.
Hope this helps.

image not loading in jsp deployed inside webapps ROOT folder

When i deploy the war file named 'myapp.war' inside the tomcat 6 webapps folder the images in jsp are loading properly whereas if i deploy it inside webapps/ROOT folder the images are not loading in jsp.
war file structure
-- myapp
- WEB-INF
-jsp
-images
In the jsp page the image inside the images folder are called like
<img src="images/img1.png"/>
You should use an absolute path for referencing images. If you include the contextPath in it, the link should work even if your application path changes. Try
<img src="${pageContext.request.contextPath}/images/mobile.png" height="34" width="38" alt="mobile" />
I have found the solution finally.
What happens is when the code is deployed inside ROOT or the war deployed inside webapp and made as ROOT application by changing the context in tomcat server.xml
in either way tomcat is considering like the complete war contents is directly inside the tomcat webapp folder .
I was already having a folder named images directly inside webapps (for some other purpose) because of this the images folder was overridden . when i removed/renamed that folder the expected images (the images called from jsp) started appearing.

Do i need a context.xml file to deploy spring webapp to tomcat

I am new to Tomcat, so I apologize if this is a dumb question. I have created a spring mvc webapp that currently runs locally using the maven-jetty-plugin.
I can successfully create the WAR file. I would like to deploy the WAR file into a tomcat6 instance. However, I am not sure if I need to create a context.xml file for tomcat? And if I do, where would I place the file in my spring webapp? My current directory structure looks like this:
src
|
|-main
| |-java
|-resource
| |-META-INF
|-webapp
| |-WEB-INF
|-web.xml
The context.xml file is used to configure application specific instructions for your container (tomcat). For instance, you can define JNDI resources, loggers, valves, etc. See Context Container for more details.
By default, Tomcat will auto-generate a default context.xml for your war if you do not specify one yourself and store it within its own configuration files in /conf/[enginename]/[hostname]/[context-root].xml
If you want to include it as part of your war, you can place it in /META-INF/context.xml within your war.
No, you don't. The context.xml file is optional, and by no means required by Spring.
context.xml is used to configure Tomcat itself. The defaults are sensible, there's no need to override them unless you have a good reason.

Resources