Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

default root location in servlet vs. JSP  RSS feed

 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I'm in a JSP, I can access an images directory by going to ../images/

What do I call in a servlet to reach that same starting directory? I mean, the one that is ../ from the JSP?

Or better yet since these JSPs might not be in a standard location, what can I call from a servlet to access the base directory of a webapp? E.g. calling the ServletContext or something and finding this directory:

/tomcat/webapps/mywebapp
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out ServletContext.getRealPath().
 
Stephen Huey
Ranch Hand
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't understand what's going on, and I'm having trouble troubleshooting this. There's a bit of HTML that gets generated for this page in a regular JavaBean, the the code looks like this:



That HTML gets passed to a servlet, where it gets outputted like this:



Pretty straightforward, and above that code I inserted the following println whiched showed me that getRealPath("/images/") returns the correct path to that small_logo.gif file.



Naturally, in the browser, the following HTML source reveals from where the page is trying to pull the logo: <img src='/images/small_logo.gif'>

However, all I see is the little image-not-found icon (with the little red X). How do I find out what path to the image the browser is telling the server if it's not the same as what getRealPath is returning? Thanks...
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't want getRealPath().
What you're looking for is: request.getContextPath()


getRealPath(String) returns the absolute path on the file system of the server... sometimes.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, if you're mapping all of your servlets, you shouldn't ever need to move back a directory.

As far as the browser is concerned, all of your requests are from pages in the root directory of the app. This means that you should only need tags like this:


Just make sure that you don't prepend a '/' to the path in the src attribute.
[ January 07, 2005: Message edited by: Ben Souther ]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!