| Author |
path used in getRequestDispatcher()?
|
sven studde
Ranch Hand
Joined: Sep 26, 2006
Posts: 148
|
|
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 ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
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 ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
sven studde
Ranch Hand
Joined: Sep 26, 2006
Posts: 148
|
|
The mapping is: [ October 09, 2006: Message edited by: sven studde ]
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
/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
Joined: Sep 26, 2006
Posts: 148
|
|
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
Joined: Sep 26, 2006
Posts: 148
|
|
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
Joined: Nov 24, 2005
Posts: 14670
|
|
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
Joined: Sep 26, 2006
Posts: 148
|
|
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
Joined: Nov 24, 2005
Posts: 14670
|
|
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
Joined: Sep 26, 2006
Posts: 148
|
|
|
Thanks Satou.
|
 |
 |
|
|
subject: path used in getRequestDispatcher()?
|
|
|