Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Access static file from apache to tomcat web applications

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


I have my static files loaded in c:/apache/htdocs of apache web server, I need to access that in my web application running in tomcat server.
For example

In c:/apache/htdocs, i have an images folder with static images which I am accessing in my web application

images
1.gif
2.jpg

In my web application i am accessing like
images/1.gif

But using the above I am unable to access the image from the Apache. Whether we need to integrate Apache & tomcat using
mod_jk, what are the other configuration we need to provide for this work?

In httpd.config I have added the below:-
<VirtualHost <IP address>:80>
DocumentRoot "c:/apache/htdocs"
/VirtualHost>

In tomcat web application I am accessing like:-
images/1.gif

When access the 1.gif in my webapplication that image is not displayed? Is there any other config I need to do in apache web server?

Thanks.
 
Saloon Keeper
Posts: 23255
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's no longer considered a good idea to keep your static content under Apache and your dynamic content under Tomcat for simple performance reasons. If you need multiple apps to share these resources, that's different, but not because of some reason that became obsolete over a decade ago.

Please note that webapps don't "access" things like images. When you display a webpage with images, css, javascript, and/or other independent content, that content is not part of the webpage in the sense that the image data is embedded in the webpage datastream. Instead the image contains a URL that the client will then open and obtain the image data from (in fact, in the days of slower modems, it was common for browsers to disable automatic fetching of images).

That URL can point anywhere in the world. Not only does it not have to be within the Tomcat webapp, it doesn't have to be on the same server, or literally even in the same country. Look at almost any commercial webpage and you'll see URL references to a whole host of third-party contributors.

So all you really need is an independent Apache source for the static content. You can do that by setting up a separate Apache VirtualHost for the images (say, http://static.myhost.com/images/pic1.gif) or you can just do an Apache mod_rewrite to rewrite the image URL requests as Apache-hosted URLs.
 
Rithanya Laxmi
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Could you please provide me the sample on setting up the virtual hosts to server static content from apache
In httpd.config, I have:-

NameVirtualHost <IP address>:80
<VirtualHost <IP address>:80>
DocumentRoot "c:/apache/htdocs\CSS"
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location>
Order allow,deny
Allow from all
</Location>
..

/VirtualHost>

In my web application running in tomcat:-

I am accessing the CSS as:-

<link href="CSS/style.css" rel="stylesheet" type="text/css" />

But I am unable do to so? Please clarify.
 
Tim Holloway
Saloon Keeper
Posts: 23255
158
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can't give step-by-step details - at least not without sending you a bill - but there are a couple of general things you should be aware of.

First, there are (at least) 2 different ways to connect Apache to Tomcat. The older one is to use mod_jk. The newer one is to use mod_proxy. In either event, you define the URL pattern you want to use for requests sent on to Tomcat as part of the proxy configuration. For example, using mod_proxy:


That makes any request sent to myvirtualhost/tomcat go to Tomcat. If you want to, you can use "/" as the URL root pattern, and EVERYTHING will go to Tomcat.

If you set up URL rewriting, you can rewrite your image and CSS requests to go to local directories instead of being routed to the ProxyPass URL.
 
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic