Front Controller problem: using forward() with pages in different directory
I use a controller servlet to capture all requests, then dispatch the request to the appropriate JSP view page, well documented.
BUT all examples I see put the JSP pages in the same directory as the root directory of the web application, ie "/webapp/", that's ok for a couple of pages, imagine a web site with 100's of pages...
When I put my JSP pages in subdirectories, eg "/webapp/login/" or "/webapp/admin" etc the relative resources inside those pages (eg images) will NOT load after the request is dispatched, 404 errors.
The documentation for getRequestDispatcher() does allow relative paths, but they MUST reference resources within the current servlet context. It just doesn't make sense to put ALL my JSP pages in the root folder!!
Someone suggested using absolute paths inside all my pages, that's silly too, ask any Dreamweaver designer. I really don't want to touch the HTML, is there any solution using java?
I did succeed by using this code before I dispatch the request:
String nextURI = "/login/login.jsp";
String tempNextURL = "http://localhost/MyWebApp/login/login.jsp";
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("location", tempNextURL);
RequestDispatcher thisReqDis = request.getRequestDispatcher(nextURI);
thisReqDis.forward(request, response);
This forces the context of the response, and the relative URLs will load correctly.
BUT this created some problems when I tried to set an attribute for the request, and then retrieve it using a JavaBean inside the JSP, attribute cannot be found because the context has been changed!
Anyone has a solution, surely this is a fundamental problem for the Front Controller architecture...

