Jetty: mapping URL to folder on disk

M

mivanov

Hello,

I recently switched from Tomcat as a service to Jetty under Geronimo
M3, and I need to know how to create a url "alias" for a folder on the
disk. For example, say I want to access files in folder "C:\bar" at
the url "http://localhost:8080/foo" so that the image
"C:\bar\foobar.png" is accessible from
"http://localhost:8080/foo/foobar.png" and so on. In Tomcat, I edited
server.xml to add <Context path="/foo" docBase="C:\bar" debug="0"
privileged="true"> How can I accomplish this with Jetty?

Many thanks,

Michael Ivanov
 
S

Steve Sobol

Hello,

I recently switched from Tomcat as a service to Jetty under Geronimo
M3, and I need to know how to create a url "alias" for a folder on the
disk. For example, say I want to access files in folder "C:\bar" at
the url "http://localhost:8080/foo" so that the image
"C:\bar\foobar.png" is accessible from
"http://localhost:8080/foo/foobar.png" and so on. In Tomcat, I edited
server.xml to add <Context path="/foo" docBase="C:\bar" debug="0"
privileged="true"> How can I accomplish this with Jetty?

Hello...

Under Jetty, all configuration is performed by creating XML tags that
actually correspond to Jetty API calls. For general information, you will
probably find

http://mortbay.org/jetty/tut/XmlConfiguration.html

useful...

Now, go to http://jetty.mortbay.org/javadoc/ and click on the Index - you
want to look at the addContext method of HttpServer, that's what you'll need
to use. For example,

public HttpContext addContext(java.lang.String virtualHost,
java.lang.String contextPathSpec)

allows you to set up a context under a specific virtualhost. The XML config
that corresponds to this method would be

<Call name="addContext">
<Arg>www.example.com</Arg> <!-- java.lang.String virtualHost -->
<Arg>/ourContext/</arg> <!-- java.lang.String contextPathSpec -->
</Call>

See? When setting up a J2EE web container, I tried Tomcat and had trouble
with it, and found the config syntax daunting. Jetty is just so much simpler.

I do want to point out that there are several addContext methods with
various arguments, and that you DO NOT have to use the one I used here. The
example above is meant primarily as an illustration of how to author
jetty.xml (or WEB-INF/jetty-web.xml for webapp-specific configurations).

If you have specific questions about configuring Jetty a certain way, I'd
recommend subscribing the mailing list. There are a lot of knowledgeable
people on it, including the Jetty developers. It is a great resource.
 
M

mivanov

Thank you very much for your response. You've put me on the right
track. However, I'm having trouble configuring HttpServer using
WEB-INF/jetty-web.xml in my webapp. The Jetty FAQ says it's
essentially the same as jetty.xml, "except that it is applied to a
HttpContext instance rather than a HttpServer instance" So in this
case, how do I add a context instead of overwriting the members of my
webapp context?

Michael
 
S

Steve Sobol

Thank you very much for your response. You've put me on the right
track. However, I'm having trouble configuring HttpServer using
WEB-INF/jetty-web.xml in my webapp. The Jetty FAQ says it's
essentially the same as jetty.xml, "except that it is applied to a
HttpContext instance rather than a HttpServer instance" So in this
case, how do I add a context instead of overwriting the members of my
webapp context?

Easy. The <configure> tag takes a class name as its sole attribute. In the
global jetty.xml, that'll be org.mortbay.jetty.Server or
org.mortbay.jetty.plus.Server, or a derivative thereof. In jetty-web.xml,
you configure a WebApplicationContext. Here's a real example from a live
website:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure 1.2//EN"
"http://jetty.mortbay.org/configure_1_2.dtd">

<Configure class="org.mortbay.jetty.servlet.WebApplicationContext">

<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>sobol.com</Item>
<Item>www.sobol.com</Item>
</Array>
</Set>

<Call name="getServletHandler">
<Call name="getServletHolder"><Arg>jsp</Arg>
<Call name="setInitParameter">
<Arg>scratchdir</Arg>
<Arg>/var/www/work/new.sobol.com</Arg>
</Call>
<Call name="setInitParameter">
<Arg>keepgenerated</Arg>
<Arg>true</Arg>
</Call>
</Call>
</Call>
</Configure>
 
M

mivanov

Thanks for the hints. I'm still not having any luck, though. Perhaps
something is different when my webapp is deployed in Geronimo? Here is
what my WEB-INF/jetty-web.xml looks like:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC
"-//Mort Bay Consulting//DTD Configure 1.2//EN"
"http://jetty.mortbay.org/configure_1_2.dtd">


<Configure class="org.mortbay.jetty.servlet.WebApplicationContext">
<Call name="getHttpServer">
<Call name="addContext">
<Arg>/data/*</Arg>
<Call name="setResourceBase">
<Arg>C:/data</Arg>
</Call>
</Call>
</Call>
</Configure>

Is my thinking off here? I am trying to get the Server instance and do
addContext on that. I've tried various different things for the
"C:/data" argument such as "C:\data" but no dice. The strange thing is
I'm getting no indication that it's picking up jetty-web.xml at all.
If I mess up the markup to make the xml invalid, I do not see any
complaints. Any thoughts?

Michael
 

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

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top