Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet Contrlloer Path Issue

 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Aryan Khan
Ranch Hand
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 290
Java Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ben. I ll try all this and post my experience here..
Thank you
Ahmad
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic