I have written a Servlet which completes a task and I want it to then reload the JSP that originally called the Servlet. The servlet runs of an enctype='multipart/form-data' JSP, this JSP is called Uploadtest, everytime I try to run either of the following code I get The requested resource (/project_name/WebContent/jsps/new.jsp) is not available.
I have tried:
response.sendRedirect("/WebContent/jsp/Uploadtest.jsp");
and
getServletContext().getRequestDispatcher("/WebContent/jsps/Uploadtest.jsp").forward (request, response);
However both of these have produced an error saying it can't find the page I am looking for.
I am looking for any ideas on how I might fix this?
Thank you
You really need to understand the difference between the layout of the sources in your project, the layout of the deployed webapp, and the layout of the application in the server.
The deployed webapp has the following structure:
jsps
Uploadtest.jsp
WEB-INF
classes
the compiled classes of your application
lib
the jars of your application
The WebContent directory doesn't exist in the deployed app. It's just where Eclipse stores the webapp related sources (JSP files, images, etc.).
Each webapp has a context path. If the context path of your webapp is /foo, the path that you'll have to type in the browser's address bar for your JSP will be:
http://localhost:8080/foo/jsps/Uploadtest.jsp
When you forward to another resource, it's always to a resource of the same webapp. So you don't need to specify the context path:
getServletContext().getRequestDispatcher("/jsps/Uploadtest.jsp").forward (request, response);
When you redirect (which is completely different from a forward), you can redirect to any URL (the same webapp, another webapp on the same host, or www.google.com if you like). If you redirect to some resource of the same webapp, you don't need to send the http://localhost:8080 part, but you need to send the context path, follwed by the resource path in the webapp, and you also should always use the encodeRedirectURL method (unless you don't care about users relying on URL-rewriting rather than cookies for session management):
String redirect =
response.encodeRedirectURL(request.getContextPath() + "/jsps/Uploadtest.jsp");
response.sendRedirect(redirect);
request.getContextPath() is used to get /foo without hard-coding it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With