Jetty: mapping URL to folder on disk

Discussion in 'Java' started by mivanov@gmail.com, Aug 3, 2005.

  1. Guest

    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
     
    , Aug 3, 2005
    #1
    1. Advertising

  2. Steve Sobol Guest

    wrote:
    > 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.

    --
    Steve Sobol, Professional Geek 888-480-4638 PGP: 0xE3AE35ED
    Company website: http://JustThe.net/
    Personal blog, resume, portfolio: http://SteveSobol.com/
    E: Snail: 22674 Motnocab Road, Apple Valley, CA 92307
     
    Steve Sobol, Aug 3, 2005
    #2
    1. Advertising

  3. Guest

    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
     
    , Aug 3, 2005
    #3
  4. Steve Sobol Guest

    wrote:
    > 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>



    --
    Steve Sobol, Professional Geek 888-480-4638 PGP: 0xE3AE35ED
    Company website: http://JustThe.net/
    Personal blog, resume, portfolio: http://SteveSobol.com/
    E: Snail: 22674 Motnocab Road, Apple Valley, CA 92307
     
    Steve Sobol, Aug 3, 2005
    #4
  5. Guest

    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

    Steve Sobol wrote:
    > wrote:
    > > 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>
    >
    >
    >
    > --
    > Steve Sobol, Professional Geek 888-480-4638 PGP: 0xE3AE35ED
    > Company website: http://JustThe.net/
    > Personal blog, resume, portfolio: http://SteveSobol.com/
    > E: Snail: 22674 Motnocab Road, Apple Valley, CA 92307
     
    , Aug 4, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jas Shultz
    Replies:
    0
    Views:
    993
    Jas Shultz
    Dec 3, 2003
  2. charly

    Help !! Jetty won't deploy

    charly, Jun 30, 2003, in forum: Java
    Replies:
    0
    Views:
    2,017
    charly
    Jun 30, 2003
  3. Maciek Zywno
    Replies:
    0
    Views:
    889
    Maciek Zywno
    Sep 3, 2003
  4. Richard Smol

    Jetty Plus in Eclipse

    Richard Smol, Apr 9, 2004, in forum: Java
    Replies:
    0
    Views:
    881
    Richard Smol
    Apr 9, 2004
  5. Replies:
    12
    Views:
    540
    santosh
    Nov 15, 2006
Loading...

Share This Page