I am migrating Webapps from a Tomcat 9
to a Tomcat 10
environment.
I am using the Eclipse IDE
.
Below is a part of the code that provokes errors:
import java.util.List;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
[...]
DiskFileItemFactory diskFileItemFactory = null;
ServletFileUpload servletFileUpload = null;
List<FileItem> fileItemsList = null;
diskFileItemFactory = new DiskFileItemFactory();
servletFileUpload = new ServletFileUpload(diskFileItemFactory);
fileItemsList = (List<FileItem>) servletFileUpload.parseRequest(request);
The errors are linked to this last line.
I get the errors:
The project was not built since its build path is incomplete. Cannot find the class file for javax.servlet.http.HttpServletRequest. Fix the build path then try building this project
The type javax.servlet.http.HttpServletRequest cannot be resolved. It is indirectly referenced from required .class files
As part of this migration, I had to replace javax.servlet
with jakarta.servlet
in the Webapps Java source code and replace the .jars javax.servlet.jsp.jstl-1.2.1.jar
and javax.servlet.jsp.jstl-api-1.2.1.jar
with the jakarta.servlet.jsp.jstl-2.0.0.jar
and jakarta.servlet.jsp.jstl-api-2.0.0.jar
.jars.
Can you help me solve these errors?
====== EDIT ======
Piotr P. Karwasz's answer solves my problem.
But, instead of adding annotations, which doesn't seem to work for me, I added, in the Webapp web.xml
file, for the servlets concerned, the <multipart-config>
element as shown below:
<servlet>
<description></description>
<display-name>FooServlet</display-name>
<servlet-name>FooServlet</servlet-name>
<servlet-class>com.foo.bar.FooServlet</servlet-class>
<multipart-config>
<location>/tmp</location>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>FooServlet</servlet-name>
<url-pattern>/do_foo_servlet</url-pattern>
</servlet-mapping>
The commons-fileupload
library is not and probably will never be compatible with Servlet 5.0. Since Servlet 3.0 a similar functionality has been included into the standard (see section 3.2).
To migrate your code to Tomcat 10, you need to:
remove commons-fileupload
from your dependencies,
annotate your servlet with @MultipartConfig
,
replace the call to ServletFileUpload#parseRequest
with HttpServletRequest#getParts
:
final Collection<Part> fileItemsList = request.getParts();
since FileItem
and Part
have almost the same methods, only minor changes are needed in the remaining code.
Edit: The new commons-fileupload2
artifact is compatible with Servlet 5.0.