Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

RequestDispatcher and relative locations

Thread view: 
Skijor - 03 Jun 2007 02:37 GMT
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)
Philipp Leitner - 03 Jun 2007 10:52 GMT
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.
Skijor - 03 Jun 2007 13:38 GMT
> 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 - 03 Jun 2007 14:12 GMT
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?
Lew - 03 Jun 2007 18:01 GMT
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
> www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
[quoted text clipped - 6 lines]
> FOOBAR
> </body>

What happens if you use

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

Signature

Lew

Skijor - 03 Jun 2007 18:35 GMT
> 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.
Lew - 03 Jun 2007 19:55 GMT
>> What happens if you use
>>
[quoted text clipped - 5 lines]
> 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.

Signature

Lew

Skijor - 03 Jun 2007 21:26 GMT
> 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'.
Lew - 03 Jun 2007 23:58 GMT
Lew wrote:
>> 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 [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".

Signature

Lew

Skijor - 06 Jun 2007 02:40 GMT
> 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)?
Lew - 03 Jun 2007 18:03 GMT
> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://
> www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
[quoted text clipped - 6 lines]
> FOOBAR
> </body>

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

Signature

Lew

Skijor - 03 Jun 2007 18:36 GMT
> 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. :)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.