I need to forward from one servlet to another and pass the request object information to it. The problem is that in the first servlet I use the Apache FileUpload library and it does something to the request that doesn't let me send it correctly to the next servlet.
If we send a form WITHOUT enctype="multipart/form-data" we can work with the fields in the servlet in a simple way with request.getParameter()
and do a forward to the other servlet as follows:
ServletContext context= getServletContext();
RequestDispatcher rd= context.getRequestDispatcher("/mySegundoServlet");
rd.forward(request, response);
And in mySecondServlet I can reuse the request object the same way I used it in the first servlet.
BUT if I have the enctype="multipart/form-data" because I need it to send a file, it is necessary to use the Apache FileUpload libraries, and all is well; but how can I send a call to /mySegundoServlet without losing the information of the request object? that is, for my second servlet in this case I no longer need the FileInput, I only need the information that was in the request of the common input text fields of the form. If I do the forward as above, the objects arrive null to my servlet. And if in the first servlet I try to do a request.setAttribute, nulls arrive in the other servlet as well.
Is there any way to clean my request object to reset it and re-set its information with request.setAttribute? Because for some reason I feel that the problem is that the request does pass fine but BOTH servlets must use the fileUpload parse because the form was initially sent with enctype="multipart/form-data"
Any ideas?
Short version
If you are processing a file upload and you are going to forward the request from one servlet to another via forward and you need to get the request parameters in both servlets, you are limited to using Apache FileUpload.
More details
It happens that by using
enctype="multipart/form-data"
you are indicating to the server that it is going to change the way it is going to receive the parameters and how it is going to interpret them. This is not specific to Java, it is part of the HTML 5 specification and the web server supports this data exchange.Before Servlet 3.0,
enctype="multipart/form-data"
the Apache FileUpload library was used to process the forms submitted with, which you mentioned. This library applies the necessary conversion of the request parameters to obtain the parts of the request in the form of parameters that you can process, among them the streams of the or the files that it supports. This means that if you are going to process this request by one or more servlets, you will perforce need to get the parts of the request to access the parameters. Since you use Servlets 2.4, you can only do it using this Apache FileUpload library.