RequestDispatcher and relative locations

Discussion in 'Java' started by Skijor, Jun 3, 2007.

  1. Skijor

    Skijor Guest

    How is it possible to load a relative resource from a .jsp file that
    was forwarded a ServletRequest using RequestDispatcher from a servlet?

    Example:
    1) set up a simple servlet:

    protected void doGet(HttpServletRequest request, HttpServletResponse
    response) throws ServletException, IOException {
    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-
    INF/tmp.jsp");
    dispatcher.forward(request, response);
    }
    }

    2) define a simple jsp file (tmp.jst):

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head>
    <link href="<%= getContextPath() %>/css/tmp.css" rel="stylesheet"
    type="text/css">
    </head>
    <body>
    FOOBAR
    </body>

    3) include a simple css file (in root/css) which turns the text in the
    <BODY> red:


    * {margin: 0; padding: 0;}

    body {
    font-family: verdana, arial, sans-serif;
    font-size: 3em;
    text-align: center;
    color: red;
    }

    problem: this css file never gets loaded using forwarding (FOOBAR
    never appears red)
    Skijor, Jun 3, 2007
    #1
    1. Advertising

  2. Don't quite see what your problem has to do with how you got into the
    JSP page in the first place. IMHO you should have the exactly same
    problem no matter if you entered the JSP directly or via a servlet.

    This being said:

    Have you had a look at the generated HTML code? I reckon
    "getContextPath()" should do exactly what you want it to do, but the
    easiest way is to check for yourself.
    Philipp Leitner, Jun 3, 2007
    #2
    1. Advertising

  3. Skijor

    Skijor Guest

    > Have you had a look at the generated HTML code? I reckon
    > "getContextPath()" should do exactly what you want it to do, but the
    > easiest way is to check for yourself.


    getContextPath() is being used. See code.


    The problem only appears when I define a servlet-mapping in web.xml
    that has a url-patter = "/". Any other pattern, like '/foo' will
    resolve context path correctly but then I have to reference my servlet
    as http://localhost/servlet/foo which I don't want to do because I
    shouldn't have to change my code in anyway in order to deploy it to a
    different container (at an ISP for example).
    Skijor, Jun 3, 2007
    #3
  4. Skijor

    Skijor Guest

    The jsp page is at: /Library/Tomcat/webapps/ixania/WEB-INF/tmp.jsp on
    my filesystem

    When I use a <url-patthern> = '/' in web.xml the generated code
    produces:

    <link href="/ixania/css/tmp.css" rel="stylesheet" type="text/css">

    which doesn't resolve tmp.css even though the content appears at:
    http://localhost/ixania/

    when I use a <url-pattern? = '/ixania' in web.xml the generated code
    produces:

    <link href="/ixania/css/tmp.css" rel="stylesheet" type="text/css">

    which does resolve tmp.css at: http://localhost/ixania/ixania

    what am i doing wrong?
    Skijor, Jun 3, 2007
    #4
  5. Skijor

    Lew Guest

    Skijor wrote:
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
    > www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    > <head>
    > <link href="<%= getContextPath() %>/css/tmp.css" rel="stylesheet"
    > type="text/css">
    > </head>
    > <body>
    > FOOBAR
    > </body>


    What happens if you use

    <link href="css/tmp.css" rel="stylesheet" type="text/css">
    ?

    --
    Lew
    Lew, Jun 3, 2007
    #5
  6. Skijor

    Lew Guest

    Skijor wrote:
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
    > www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    > <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    > <head>
    > <link href="<%= getContextPath() %>/css/tmp.css" rel="stylesheet"
    > type="text/css">
    > </head>
    > <body>
    > FOOBAR
    > </body>


    BTW, since you specify XHTML, don't you have to close the link tag?

    --
    Lew
    Lew, Jun 3, 2007
    #6
  7. Skijor

    Skijor Guest

    > What happens if you use
    >
    > <link href="css/tmp.css" rel="stylesheet" type="text/css">
    > ?


    that was the first thing I had in index.jsp which worked fine, since
    the stylesheets were under css at the same level. But renaming
    index.jsp to bootstrap.jsp and forwarding to it from a servlet doesn't
    work.
    Skijor, Jun 3, 2007
    #7
  8. Skijor

    Skijor Guest

    > BTW, since you specify XHTML, don't you have to close the link tag?

    yes. It's just a snippet and I must have snipped it. :)
    Skijor, Jun 3, 2007
    #8
  9. Skijor

    Lew Guest

    Skijor wrote:
    >> What happens if you use
    >>
    >> <link href="css/tmp.css" rel="stylesheet" type="text/css">
    >> ?

    >
    > that was the first thing I had in index.jsp which worked fine, since
    > the stylesheets were under css at the same level. But renaming
    > index.jsp to bootstrap.jsp and forwarding to it from a servlet doesn't
    > work.


    I find that strange, because I routinely write web apps where servlets forward
    to JSPs, and relative references always work fine.

    --
    Lew
    Lew, Jun 3, 2007
    #9
  10. Skijor

    Skijor Guest

    > I find that strange, because I routinely write web apps where servlets forward
    > to JSPs, and relative references always work fine.


    I think it may have something to do with the fact that my css and
    images directory was under WEB-INF. Tomcat doesn't allow urls that
    request resources inside this directory so my guess is that the
    relavie references were failing because of this but it's just a
    guess. I changed my code so that index.jsp is the default page and I
    noticed that this file was not able to load references to css when it
    was inside WEB-INF so I move both css/ and images/ out of WEB-INF and
    into web and it worked. My guess is that if I go back to using my
    servlet as the default resource it will work now if I forward the
    request to index.jsp or some other .jsp file. I'm not sure I want to
    go back tho'.
    Skijor, Jun 3, 2007
    #10
  11. Skijor

    Lew Guest

    Lew wrote:
    >> I routinely write web apps where servlets forward to JSPs, and relative references always work fine.


    Skijor wrote:
    > I think it may have something to do with the fact that my css [sic] and
    > images directory was under WEB-INF.


    Now that you tell us this rather important bit of news things are much more
    explicable.

    > Tomcat doesn't allow urls [sic] that request resources inside this directory so my guess is that the
    > relavie references were failing because of this but it's just a


    No wonder your "<%= getContextPath() %>" scriptlet didn't help.

    > guess. I changed my code so that index.jsp is the default page and I
    > noticed that this file was not able to load references to css when it
    > was inside WEB-INF so I move both css/ and images/ out of WEB-INF and
    > into web and it worked.


    The reason you could get your page from WEB-INF/ is that access is through a
    servlet forward. The reason that you couldn't get your CSS from WEB-INF/ was
    that access was through a URL.

    > My guess is that if I go back to using my servlet as the default resource it will work now if I forward the
    > request to index.jsp or some other .jsp file. I'm not sure I want to
    > go back tho'.


    There are a lot of good reasons to use a servlet to mediate view and logic
    dispatch. Check out "Model-View-Controller" (MVC) and "Front Controller pattern".

    --
    Lew
    Lew, Jun 3, 2007
    #11
  12. Skijor

    Skijor Guest

    > There are a lot of good reasons to use a servlet to mediate view and logic
    > dispatch. Check out "Model-View-Controller" (MVC) and "Front Controller pattern".


    Yes. The reason I moved from the flow-controller model to a
    static .jsp page is that I am still fundamentally confused over the
    meaning of '/' as a url-mapping in web.xml. For some reason relative
    offsets from the context path don't resolve external dependencies for
    me even when those dependencies are outside of WEB-INF (e.g., /
    images, /css). Is there some special meaning to '/' (i.e., has a
    hidden or absolute context path)?
    Skijor, Jun 6, 2007
    #12
    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. Paul Vincent Craven

    Multiple includes with RequestDispatcher

    Paul Vincent Craven, Aug 29, 2003, in forum: Java
    Replies:
    0
    Views:
    644
    Paul Vincent Craven
    Aug 29, 2003
  2. Will Handley
    Replies:
    1
    Views:
    570
    Nigel Wade
    Dec 10, 2003
  3. Ryan Stewart

    RequestDispatcher.include() behavior

    Ryan Stewart, May 20, 2004, in forum: Java
    Replies:
    0
    Views:
    475
    Ryan Stewart
    May 20, 2004
  4. DiscoStu
    Replies:
    1
    Views:
    499
    Erwin Moller
    Jul 20, 2004
  5. harryos
    Replies:
    2
    Views:
    1,692
Loading...

Share This Page