RequestDispatcher and relative locations

S

Skijor

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)
 
P

Philipp Leitner

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.
 
S

Skijor

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).
 
S

Skijor

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?
 
S

Skijor

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.
 
S

Skijor

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. :)
 
L

Lew

Skijor said:
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.
 
S

Skijor

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'.
 
L

Lew

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 said:
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".
 
S

Skijor

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)?
 

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

Staff online

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top