• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

path used in getRequestDispatcher()?

 
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you determine the path to a file when you use getRequestDispatcher() before forward()'ing the request to the file?

In an example I am working on (in HF Servlets & JSP, p. 89), this is the directory structure:

tomcat
----webapps
---------Beer-v1
----------------form.htm
----------------result.jsp (the View)
----------------WEB-INF
-----------------------web.xml
-----------------------classes
----------------------------com
---------------------------------example
---------------------------------------web
-------------------------------------------BeerSelect.class
---------------------------------------model
-------------------------------------------BeerExpert.class

In the BeerSelect servlet is the following code:


I don't understand how the path "result.jsp" is arrived at. In what I would consider a glaring omission in HF Servlets & JSP not in keeping with the rest of the book, there is no discussion on how that path is arrived at.

I have another book, Murach's Servlets and JSP, and it says (on p.176):

Within the getRequestDispatcher() method, you must code a URL that starts with a slash so it is relative to the document root directory.



Is that correct? And therefore, the HF book is wrong? I checked the HF errata and nothing was noted there.
[ October 09, 2006: Message edited by: sven studde ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Within the get RequestDispatcher() method, you must code a URL that starts with a slash so it is relative to the document root directory.


This is true for dispatchers obtained via the ServletContext.
You can also use relative paths with dispatchers obtained via the request (ServletRequest.getRequestDispatcher).

From the spec:

The behavior of this method is similar to the method of the same name in the
ServletContext. The servlet container uses information in the request object to transform the given relative path against the current servlet to a complete path. For example, in a context rooted at �/� and a request to /garden/tools.html, a request dispatcher obtained via ServletRequest.getRequestDispatcher("header.html") will behave exactly like a call to ServletContext.getRequestDispatcher("/garden/header.html").


What is the mapping to your servlet ?
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The mapping is:


[ October 09, 2006: Message edited by: sven studde ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/SelectBeer.do.
So the current path will be the root of the application. That's why request.getRequestDispatcher("result.jsp") refers to result.jsp in the root.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm starting to see the light. Thanks. Boy are these paths confusing.

(For anyone else that ends up here, in the HF book I found some follow up discusion on the path used in request.getRequestDispatcher() on p. 204.)

A follow up question, if you don't mind:

What about getRealPath()? When would you use the String returned by that method for the path to your file?
[ October 10, 2006: Message edited by: sven studde ]
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question:

Can you use a path starting with a "/" when using request.getRequestDispatcher() to resolve the path relative to the context root?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
About getRealPath, I don't know Whenever you want to get the real path of the file. For debugging purpose ?

About ServletRequest.getRequestDispatcher with an absolute path, yes, you can use it. It will work like ServletContext.getRequestDispatcher.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

About getRealPath, I don't know Whenever you want to get the real path of the file. For debugging purpose ?


No, in another example I was working on, the servlet creates an instance of a user defined class. The user class has a method that writes some email addresses to a text file. The servlet needs to pass the location of the file to the method. Inside the method is this code:

PrintWriter out = new PrintWriter(new FileWriter(filname, true));

I think I am seeing the distinction now. Methods that are part of the servlet API understand paths relative to the context root, e.g. paths that start with "/" or paths that are relative to the current directory. But a FileWriter constructor doesn't know anything about servlets or context roots, and it needs an absolute path to where the file is located on the hard drive.
[ October 10, 2006: Message edited by: sven studde ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

but a FileWriter constructor doesn't know anything about servlets or context roots, and it needs an absolute path to where the file is located on the hard drive


Yes.
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Satou.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic