aspose file tools*
The moose likes Tomcat and the fly likes Accessing a context.xml from a virtualhost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Tomcat
Reply locked New topic
Author

Accessing a context.xml from a virtualhost

Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
Hi everyone,

I'm using MyEclipse/Eclipse to build a JSF based application running on the Apache Tomcat 5.5 application server.

I have this sucessfully working as a project called "Contact", eclipse takes care of exploding the war file for me.

In the "META-INF" directory I have a "Context.xml" file which has the JNDI connection information.

Now if I access this file via http://localhost:8000/Contact/ it works perfectly (as expected).
The database queries occur etc. etc.

However, when this application goes into production, we don't want customers typing.

http://contact.domain.com/Contact/

We want them typing:
http://contact.domain.com/

Right? Well I can configure Apache 2.0 and Tomcat to do this by doing this in the server.xml of Tomcat:

<Host name="3.xx.xx.xxx" debug="1" appBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Contact" unpackWARs="true" autoDeploy="true">
<!-- <Alias>contact.domain.com</Alias>-->

<Context docBase="" path="" debug="1" reloadable="true"/>
</Host>

Now this sets up a host running off my local IP address (of course on the server the name will be a FQDN, a URL of the project).

And this is good and well, I can access the program via the IP address 3.xx.xx.xxx (where the x's are my static IP address).

However, if I try to do anything database related it throws a:
2006-08-03 15:25:03,801 [http-8000-Processor24] ERROR com.xxxx.backend.DatabaseUtils - Error trying to connect to database
javax.naming.NameNotFoundException: Name jdbc is not bound in this Context

The only reason why I can see this is not bound is because its not finding the context file correctly.

If I change the virtual host to have an appbase of one directory higher like this:

<Host name="3.xx.xx.xxx" debug="1" appBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps" unpackWARs="true" autoDeploy="true">
<!-- <Alias>contact.domain.com</Alias>-->

<Context docBase="" path="" debug="1" reloadable="true"/>
</Host>

And then access the program via http://3.xx.xx.xxx/contact this will also work fine (no errors).

However I cannot seem to get the context information right when I don't have the /Contact there.

Is there something obvious I'm missing? I've spent about 7hours on this problem with no solutions so far

Context.xml looks like this:

<?xml version='1.0' encoding='utf-8'?>
<Context path="/Contact" reloadable="true">

<Resource name="jdbc/CLV" auth="Container"
type="javax.sql.DataSource" username="xx" password="xx"
driverClassName="oracle.jdbc.OracleDriver" url="jdbcracle:thin:urlgoeshere" connectionProperties="oracle.net.encryption_types_client=(DES56C);oracle.net.encryption_client=(REQUIRED);oracle.net.crypto_checksum_types_client=(MD5);oracle.net.crypto_checksum_client=(REQUIRED)"
maxActive="8" maxIdle="4" maxWait="5000" />
</Context>

Where there is of course a real URL there.

And the lookup code is simply:
Context context = new InitialContext();
DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/CLV");

Any ideas here? Am I missing something obvious in my configuration file?

How do I get this context file to work correctly? I've tried renaming it to "Contact.xml" and putting it in the $CATALINA_HOME/conf/localhost but this only works for the method of accessing it with "/Contact" on the end.

I've tried adding the <resource-ref> etc. to web.xml, again this only works with the /Contact on the end. How do I get this context.xml to show up if there is no /Contact on the URL?

Thanks,
Gareth
[ August 03, 2006: Message edited by: Bear Bibeault ]
Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
Read a little more documentation:

"The context path of this web application, which is matched against the beginning of each request URI to select the appropriate web application for processing."

Well the problem is my request URI is not going to have the context information in it because we don't want the customer to always have to type the "/ContextName" on the end of it.

So maybe I need to learn how to tell the virtualhost that its always in that context...
Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
Had a look at more documentation and still can't figure out what to do or where to start

I'm not sure where to look anymore either, the tomcat documentation doesn't help, nearly every forum post tries to do it a different way (and I can't get any of them to work ).
[ August 03, 2006: Message edited by: Gareth Anderson ]
Jan Groth
Ranch Hand

Joined: Feb 03, 2004
Posts: 456
hi gareth,

sorry, can't help you with any details... but what i just learnt: whenever you manipulate tomcat configuration, delete the work directory afterwards. otherwise you'll end up with cached (and unchanged) configuration...

jan
Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
Sorry, where are you referring to the work directory? And does this apply to tomcat 5.5 (quite different to Tomcat 5.0)

The only conclusion the group came to so far was to do a forward . Other staff members pages on the same server don't have this problem, but they use hibernate which does not require a context.xml to be used in the meta-inf directory.

So I'm the only one trying to do it this way...
[ August 05, 2006: Message edited by: Gareth Anderson ]
Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
Looks like the only way to do this is to perform a re-direct on the server as the "Contact" in the URL is required for the context to work as expected.

If I was not using a context file then I could have done without the re-direct (or I could use hibernate etc.).
Gareth Anderson
Greenhorn

Joined: Feb 27, 2006
Posts: 13
Here's the reply I sent to my group about this, I was approaching the problem in the wrong way...

Thanks for those who replied with some advice or assistance about this, I found a way to resolve this and here is some information for anyone else who runs into the same issue.

I was thinking about the problem the wrong way, I was also not interpretting the Tomcat documentation correctly.

From http://tomcat.apache.org/tomcat-5.5-doc/config/context.html it says:
"The web application used to process each HTTP request is selected by Catalina based on matching the longest possible prefix of the Request URI against the context path of each defined Context. Once selected, that Context will select an appropriate servlet to process the incoming request, according to the servlet mappings defined in the web application deployment descriptor file (which MUST be located at /WEB-INF/web.xml within the web app's directory hierarchy).

You may define as many Context elements as you wish. Each such Context MUST have a unique context path, which is defined by the path attribute. In addition, you MUST define a Context with a context path equal to a zero-length string. This Context becomes the default web application for this virtual host, and is used to process all requests that do not match any other Context's context path."

Now you *must* have a default Context in a virtualhost if its going to work at all. But if you create a Context file in the META-INF directory as I was trying to do, then it is not going to relate to the default web application unless there is the application name in the URL (which is what I was trying to avoid).

In other words the way of specifying the virutal host was fine, for testing I created a virtualhost from my IP address of my computer, for example:

<Host name="3.210.23.253" appBase="C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Contact" unpackWARs="true" autoDeploy="true">
<Context docBase="" path="" debug="0" reloadable="true"/>
</Host>

The above is fine for creating a virtual host but the context will be lost if the webapplication exists under the directory "Contact".

So what I did is create a context file that is default to the host in "C:\Program Files\Apache Software Foundation\Tomcat 5.5\conf\Catalina\3.210.23.253", called "context.xml.default" (which is just a copy of the context file in META-INF/context.xml), however this time the context file is for any application on the virtualhost. Since the only application on the virtualhost will be my application there is no problem with this.

Here is the relevant section of the Tomcat manual:
"In addition to nesting Context elements inside a Host element, you can also store them:

in the individual $CATALINA_HOME/conf/context.xml file: the Context element information will be loaded by all webapps
in the individual $CATALINA_HOME/conf/[enginename]/[hostname]/context.xml.default file: the Context element information will be loaded by all webapps of that host
in individual files (with a ".xml" extension) in the $CATALINA_HOME/conf/[enginename]/[hostname]/ directory
if the previous file was not found for this application, in individual file at /META-INF/context.xml inside the application files"
I'm using the second dot point there, this will work for my particular situation because a virtualhost only runs a single web application, the last dot point did not work for me as I expected, and I don't think the third dot point worked for me either but I haven't had time to re-test this.

Hope that helps someone.
Thanks everyone for your help!
 
Don't get me started about those stupid light bulbs.
 
subject: Accessing a context.xml from a virtualhost