Issue
I'm working on a social networking site and I have the following problem. In profile.jsp
I have a form where the user can upload a photo. This form has an action to FileUploadHandler
servlet which uploads the photo and then sends redirect to uploadfileController.jsp
like this:
RequestDispatcher requestDispatcher;
requestDispatcher = request.getRequestDispatcher("/uploadfileController.jsp");
requestDispatcher.forward(request, response);
In uploadfileController.jsp
I insert this post into my MySQL database and I send redirect to profile.jsp
.
response.sendRedirect("/profile.jsp");
But then I get this error message:
HTTP Status 404 - /profile.jsp
type Status report
message /profile.jsp
description The requested resource is not available.
However, when I go again to profile.jsp
the post is created! Any ideas?
Solution
If the response is relative to servlet context then prepend context path to the URL.
String contextPath = request.getContextPath();
response.sendRedirect(response.encodeRedirectURL(contextPath + "/profile.jsp");
Answered By - Roman C