1

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?

2 Answers2

1

Apparently you're using a relative action URL in your <form>.

<form action="servlets/UploadServlet" ...>

When you open index.jsp, the request URL is

http://localhost:8080/UploadImage/index.jsp

When you submit the form, the action URL is relative to the current folder, so request URL will be

http://localhost:8080/UploadImage/servlets/UploadServlet

When you submit the form once again, the will be still relative to current folder, so you end up in

http://localhost:8080/UploadImage/servlets/servlets/UploadServlet

You need to fix it to be a domain-relative URL, starting with a leading slash.

<form action="/UploadImage/servlets/UploadServlet" ...>

This way the URL will be resolved relative to the domain root. You can also resolve the context path dynamically by ${pageContext.request.contextPath}:

<form action="${pageContext.request.contextPath}/servlets/UploadServlet" ...>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks BalusC your answer solved my problem. I used
    and the problem got solved.
    – Manohar Bhattarai Sep 10 '11 at 04:08
  • You're welcome. I'd however suggest you to take benefit of the `doGet()` method of the servlet so that you end up with cleaner URLs. See also our servlets wiki page: http://stackoverflow.com/tags/servlets/info – BalusC Sep 10 '11 at 04:17
0

Your url is wrong. You can open the web.xml and find the "servlet-mapping" element there you can find the mapping url.

I guess your url may be "/CropImage/servlets/UploadServlet" .you can try to delete one "servlets" in the url.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
ouotuo
  • 64
  • 3
  • This is the description of my J2EE component This is the display name of my J2EE component UploadServlet servlets.UploadServlet UploadServlet /servlets/UploadServlet – Manohar Bhattarai Sep 09 '11 at 08:44