开发者

tomcat 5.5 setting up context path with virtual hosts (non webapps directory)

I have been a web developer for a couple of years (some ASP, mostly PHP) and have recently taken on Java/JSP in the last two years.

The one thing I have tried repeatedly but continue to fail to wrap my head around is the way to setup a working JSP application in another location that is NOT the webapps folder. Rocking my application out of webapps does not seem to be a problem for me.

Could anyone help break this down, or point me to post somewhere which outlines it?

I have Googled repeatedly and read and re-read the documentation on the apache.org website, but I still cant seem to get it :(

I am trying to setup an "exploded" site (folders and files separated - not part of a WAR).

I know that I CAN edit the server.xml file, but am not supposed to (not that it matters... even when I attempt to edit that I fail).

I know I am missing something with the way the paths work. I THOUGHT I understand but I dont thinks so.

Any help is mucho appreciated!!

Love this site.

-- I have decided to re-phrase my question with more detail --

Let's say I have a directory structure as follows (**NOTE: webproject is NOT in the tomcat/webapps directory)

/webproject
/webproject/htdocs
/webproject/htdocs/images
/webproject/htdocs/images开发者_开发技巧/logo.gif
/webproject/java
/webproject/java/page1.jsp
/webproject/java/page2.jsp 

I would like to setup a virtual host so that I can view my project via a URL: http://webproject

I THINK all I need is two things (I am using apache):

  1. a 'VirtualHost' entry in my httpd.conf file.
  2. some type of 'context' file or fragment somewhere.

Am I right? If so, could someone help me define these two elements?

Also, if I want to use a domain-relative URL for referencing my logo.gif file from either of my jsp pages what would my path look like?

What if I want to use a domain-relative URL for referencing page2.jsp from page1.jsp?


HOWEVER, if there are paths within my application that reference resources (images, scripts, etc) using a path which starts at the root (ie. img src="/htdocs/images/foo.gif") it is not found.

Are you aware that those paths are supposed to be URL's relative to the current request URL (the one of the JSP page) and thus not local disk file system paths relative to their location at the local disk file system? As you're using /htdocs, it namely look like that you thought the latter.

Thus, if the JSP page is for example to be requested by http://example.com/context/page.jsp and the foo.gif is actually available by http://example.com/context/images/foo.gif, then you need to use either page-relative URL:

<img src="images/foo.gif">

or domain-relative URL:

<img src="/context/images/foo.gif">

or protocol-relative URL:

<img src="//example.com/context/images/foo.gif">

or absolute URL:

<img src="http://example.com/context/images/foo.gif">


OK, after struggling with this I finally got it to work. I aint saying this is the best (or even correct!) way of doing it, but this is what ultimately worked for me locally on my Windows machine (running WAMP) and on a remote server running Linux.

The paths listed are where they were for my environments. Obviously this could be different for your situation.

BEFORE-FIRST (Windows only)

With Windows, you need to setup an entry in your hosts file for the new "domain".

WIN - C:\Windows\System32\drivers\etc\hosts

127.0.0.1 webproject.local

FIRST

Need to edit httpd.conf and add a virtual host

WIN - C:\wamp\bin\apache\Apache2.2.11\conf\extra\httpd-vhosts.conf

LINUX - /etc/httpd/conf/httpd.conf

 <VirtualHost *:80>
     DocumentRoot "C:\wamp\www\webproject"
     ServerName webproject.local
 </VirtualHost>

SECOND

Setup a host and context in server.xml. From what I read online, editing server.xml is discouraged but this was the only way I could figure it out.

WIN - C:\Program Files (x86)\Apache Software Foundation\Tomcat 5.5\conf\server.xml

LINUX - etc/tomcat5/server.xml

<Host name="webproject" debug="0" appBase="webapps" unpackWARs="true">
   <Context path="" docBase="C:/wamp/www/webproject"/>
   <Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="webproject.local_error-log." suffix=".txt" timestamp="true"/>
</Host>

The thing I don't think is right, but worked for me was using "webapps" as the appBase even though my files are not in the "webapps" folder. Not sure why this is working... I fear that it is one of those situations where it works even though it shouldnt. I hope someone a lot smarter than me can offer some insight.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜