I have an index.jsp page which uploads an image. On submit it goes to a servlet Upload.java. In the servlet I am checking if the extension in of image(“jpg”,”png”,etc) and forwards to new jsp page else it shows an error message and includes the same index.jsp page.
My servlet is a package named “servlets”.
If I select an image then it is working properly. But if I select any file other than image then it shows the error with the index.jsp page as intended. Till now it works fine but if I upload any file even image from here, the server complains.
Here is how I am including the index.jsp page in UploadServlet.java servlet.
out.println("This type of file is not allowed. Please select an image.");
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
dispatcher.include(request, response);
Here is the error from the server when I try to upload the image second time.
HTTP Status 404 - /UploadImage/servlets/servlets/UploadServlet
type Status report
message /UploadImage/servlets/servlets/UploadServlet
description The requested resource (/CropImage/servlets/servlets/UploadServlet) is not available.
Apache Tomcat/6.0.13
It is appending the servlet’s package name to the url.
How to solve this problem?
Apparently you’re using a relative action URL in your
<form>.When you open
index.jsp, the request URL isWhen you submit the form, the action URL is relative to the current folder, so request URL will be
When you submit the form once again, the will be still relative to current folder, so you end up in
You need to fix it to be a domain-relative URL, starting with a leading slash.
This way the URL will be resolved relative to the domain root. You can also resolve the context path dynamically by
${pageContext.request.contextPath}: