aspose file tools*
The moose likes Servlets and the fly likes Servlet Contrlloer Path Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet Contrlloer Path Issue" Watch "Servlet Contrlloer Path Issue" New topic
Author

Servlet Contrlloer Path Issue

Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Hi all,
I am sure some one know the answer to this one as it is common in MVC.

I am dispatching to a JSP page from my Servlet


Now it does dispatch fine, but the problem is that this jsp page calles other jsp pages and some images which after dispatching does not show up.

I think it is some path issue.


OCP/MCP/SCJP/SCWCD/IBM XML/SCMAD/SCEA-1
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Within the JSP make all links to other resources relative to the URL of the controller servlet.

IE: If the url-pattern of your controller is /SomeServlet and you use the following url to call it:
http://localhost:8080/SomeServlet
then the links and references in someJsp need to be relative to the root of
your application.
<image src="images/some/image">
The browser doesn't know that the JSP page was nested under the "jsp" directory.

The other approach is to always make them relative to the root of your context:
[ May 28, 2005: Message edited by: Ben Souther ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Ben,
I know that if I modifiy all the JSP files I can get it working.
But what I want is that after Dispatching(or before or some how) I can tell the tomcat where to resolve relative paths against.
Because I have .js files being called in my jsp pages. These .js files also refere resources like images and css files.
I cant use jsp stuff in .js files and dont want to change the paths in js files.

Thanks for your replies
Ahmad
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

I was thinking of redirecting rather than dispatching, but the problem is that the JSP pages are within WEB-INF folder.

Ahmad
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I can tell the tomcat where to resolve relative paths against.

It isn't tomcat that you need to tell. It's the browser.
When you do a server side forward, the browser has no idea what file you're serving up to it. It only knows about the URL that it used to hit the controller.

I keep it simple by always creating url-patterns that are not nested:
<url-pattern>/SomePage</url-pattern>
As opposed to:
<url-pattern>/someDirectory/SomePage</url-pattern>

That way, all of my links can be relative to the root of the app:
src="images/myImage".

The second approach that I mentioned is probably the most robust because it won't matter what the url-pattern of the controller is.
Request.getContextPath will aways get the user to the root of the app.
I can then make all the paths relative to that.

Try right clicking on one of your broken image links and viewing it's properties. This will show you where the browser is looking for the image.
Aryan Khan
Ranch Hand

Joined: Sep 12, 2004
Posts: 290

Thank you Ben. I ll try all this and post my experience here..
Thank you
Ahmad
 
jQuery in Action, 2nd edition
 
subject: Servlet Contrlloer Path Issue