开发者

RequestDispatcher.include(...) appends servlet's package name

开发者 https://www.devze.com 2023-04-03 12:52 出处:网络
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

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 solv开发者_运维技巧e this problem?


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" ...>


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消