Tomcat default context for virtual host

A

Arne Vajhøj

jsguru72 said:
According to the documentation for the Tomcat 6 <Context> element,
-----
If you specify a context path of an empty string (""), you are
defining the default web application for this Host, which will process
all requests not assigned to other Contexts.
-----

This tells me that if I define a Context and set the path to an empty
string, I should be able to enter just the virtual host name as a URL
and be directed to that context. I must be misunderstanding something
though because this is not what I am experiencing.

I think the docs are consistent.

It is not "" but "/" that gives that doctrine.

But yes. In that case you can discourage the tinyurl attic name in the limb.

Arne



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"I would support a Presidential candidate who
pledged to take the following steps: ...
At the end of the war in the Persian Gulf,
press for a comprehensive Middle East settlement
and for a 'new world order' based not on Pax Americana
but on peace through law with a stronger U.N.
and World Court."

--- George McGovern,
in The New York Times (February 1991)
 
J

jsguru72

According to the documentation for the Tomcat 6 <Context> element,
-----
If you specify a context path of an empty string (""), you are
defining the default web application for this Host, which will process
all requests not assigned to other Contexts.
-----

This tells me that if I define a Context and set the path to an empty
string, I should be able to enter just the virtual host name as a URL
and be directed to that context. I must be misunderstanding something
though because this is not what I am experiencing.


In my server.xml file I have the following virtual host defined.

<Host name="virthost01" appBase="webapps" unpackWARs="true"/>


I then have a context.xml file defined within the META-INF directory
of my webapp. In the context element I tried several things. I tried
not defining the path parameter, defining it as an empty string, and
defining it as the context path.

In all cases, when I enter 'virthost01' as a URL, I get the default
Tomcat welcome page. If I add the webapp name to the URL, I get my
webapp.

What do I need to do so that I can just enter the hostname and be
directed to my context?

I am certain that I am missing something. Any help pointing me in the
right direction would be appreciated.


Thanks,
John S.
 
D

Dave Miller

jsguru72 said:
According to the documentation for the Tomcat 6 <Context> element,
-----
If you specify a context path of an empty string (""), you are
defining the default web application for this Host, which will process
all requests not assigned to other Contexts.
-----

This tells me that if I define a Context and set the path to an empty
string, I should be able to enter just the virtual host name as a URL
and be directed to that context. I must be misunderstanding something
though because this is not what I am experiencing.


In my server.xml file I have the following virtual host defined.

<Host name="virthost01" appBase="webapps" unpackWARs="true"/>


I then have a context.xml file defined within the META-INF directory
of my webapp. In the context element I tried several things. I tried
not defining the path parameter, defining it as an empty string, and
defining it as the context path.

In all cases, when I enter 'virthost01' as a URL, I get the default
Tomcat welcome page. If I add the webapp name to the URL, I get my
webapp.

What do I need to do so that I can just enter the hostname and be
directed to my context?

I am certain that I am missing something. Any help pointing me in the
right direction would be appreciated.


Thanks,
John S.
<Context path="" docBase="" debug="0"...

would serve content from /webapps

<Context path="" docBase="myapp" debug="0"...

would serve content from /webapps/myapp

and so forth.

If your results are different, post all of <host> and <context> for help.
 
A

Arne Vajhøj

jsguru72 said:
According to the documentation for the Tomcat 6 <Context> element,
-----
If you specify a context path of an empty string (""), you are
defining the default web application for this Host, which will process
all requests not assigned to other Contexts.
-----

This tells me that if I define a Context and set the path to an empty
string, I should be able to enter just the virtual host name as a URL
and be directed to that context. I must be misunderstanding something
though because this is not what I am experiencing.

I think the docs are wrong.

It is not "" but "/" that gives that effect.

But yes. In that case you can omit the web app name in the URL.

Arne
 
J

jsguru72

My problems have gotten worse. In the course of troubleshooting, now
my app won't load at all. Even after restoring my original files, it
still isn't loading. The myapp.war file in the webapps directory, but
it is not getting extracted.

In server.xml I have this.

<Host name="virthost01" appBase="webapps" unpackWARs="true">
<Context
className="org.apache.catalina.core.StandardContext"
Path=""
docBase="myapp"
reloadable="true"/>
</Host>


In myapp/META-INF/context.xml I have this.

<Context path="/myapp">
</Context>


When I start Tomcat, I get this.
SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base /myapp does not
exist or is not a readable directory
at
org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:
141)


So there is apparently more I am not understanding than I thought.


Any help continues to be appreciated.
 
D

Dave Miller

jsguru72 said:
In server.xml I have this.

<Host name="virthost01" appBase="webapps" unpackWARs="true">



In myapp/META-INF/context.xml I have this.

<Context path="/myapp">
</Context>

When I start Tomcat, I get this.
SEVERE: Error starting static Resources
java.lang.IllegalArgumentException: Document base /myapp does not
exist or is not a readable directory
at
org.apache.naming.resources.FileDirContext.setDocBase(FileDirContext.java:
141)
The path is the relative or absolute path to the directory where your
app is located. If you are not deploying via a war, the directory must
exist prior to Tomcat starting.

Presume that you want your app in myapp and you want myapp served from
root (i.e. http://www.your_domain.com/index.jsp as your index page)

For now, delete the context.xml file from META-INF and server.xml should
look like:

<Host name="virthost01" appBase="webapps" unpackWARs="true">

<Context
className="org.apache.catalina.core.StandardContext"
Path="" docBase="" reloadable="true"/>

</Host>
 
D

Dave Miller

Dave Miller wrote:

Oops - should have been
<Host name="virthost01" appBase="webapps" unpackWARs="true">

<Context
className="org.apache.catalina.core.StandardContext"
Path="" docBase="myapp" reloadable="true"/>

</Host>
make sure that the directory webapps/myapp exists befor starting Tomcat.
 
J

jsguru72

ok. I had to go into the $TOMCAT_HOME/conf/Catalina directory and
remove all references to the myapp.xml files that are copied there
when the app is deployed.

I then removed my META-INF/context.xml file and updated server.xml to
reflect the following.

<Host name="virthost01" appBase="webapps" unpackWARs="true">
<Context
className="org.apache.catalina.core.StandardContext"
Path=""
docBase="myapp"
reloadable="true"/>
</Host>


I restarted Tomcat and that fixed the problem where it was not finding
the directory and also fixed my original URL issue.

A URL of http://virthost01/myapp brings me to my myapp page.
A URL of http://virthost01 also brings me to my myapp page.

So part of the problem was that Tomcat was caching the context.xml
files and for some reason those were causing issues. It would seem I
need to remove the $TOMCAT_HOME/conf/Catalina directory before
starting Tomcat. That seems to be a problem, but one I will tackle
separately.

Now the issue is how to get this working without having to put the
Context element in the server.xml

I wanted to be able to edit the myapp/META-INF/context.xml instead of
the server.xml so that changes would not require a restart of Tomcat.


I tried moving the Context stuff back to the context.xml file, but
every time I do, I can no longer access myapp without specifying myapp
in the URL.


There is something I am still not understanding about the relationship
between the Context element in server.xml and the Context element in
META-INF/context.xml.



Thanks
 
D

Dave Miller

jsguru72 said:
I tried moving the Context stuff back to the context.xml file, but
every time I do, I can no longer access myapp without specifying myapp
in the URL.


There is something I am still not understanding about the relationship
between the Context element in server.xml and the Context element in
META-INF/context.xml.
It's the level at which you are giving Tomcat the instructions. If the
instruction is placed at the host level, the call can be redirected to
the app without a prefix. If the instruction is at the app level, the
host still has to be told where to find the app via a prefix included
with the call.

There are five ways to define <content> shown on the following docs
page. You can use any of the other four:

http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
 
J

jsguru72

I think I am getting it now. That kind of confirms what I was
thinking originally. It makes sense that the server.xml file would
have to have some Context information that tells it where the app
is.

As I read the documentation I was confused because I was really
interpreting it to mean that it was either/or. I could place the
context stuff in the server.xml or the META-INF/context.xml. In fact,
based on the following line in the documentation, I reached the
conclusion that defining a Context element in the server.xml was to be
avoided.

------------
For Tomcat 6, unlike Tomcat 4.x, it is NOT recommended to place
<Context> elements directly in the server.xml file.
------------

This statement in the documentation is apparently misleading. I need
to have a minimal Context definition in the server.xml so that Tomcat
knows where to find the app. I presume I can then further define the
Context, with Resources and such, within the META-INF/context.xml.


Thanks to all that replied and got me straightened out.


John S.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top