This week's book giveaway is in the Agile and Other Processes forum. We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line! See this thread for details.
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 ]
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").
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 ]