HTML5 API FormData
sends a multipart/form-data
request. At first it was designed to download files using ajax, with the new version 2 of XMLHttpRequest
. Downloading previous version of files is not possible.
request.getParameter()
only recognizes application/x-www-form-urlencoded
requests by default. But you are sending a multipart/form-data
request. You need to annotate your @MultipartConfig
servlet class so you can get them through request.getParameter()
.
@WebServlet @MultipartConfig public class YourServlet extends HttpServlet {}
Or, if you are not already using Servlet 3.0, use Apache Commons FileUpload. See the following for a more detailed answer to both approaches: How do I upload files to the server using JSP / Servlet?
If you donโt need to upload files at all, use the โstandardโ XMLHttpRequest
instead.
var xhr = new XMLHttpRequest(); var data = "firstName=" + encodeURIComponent(firstName) + "&lastName=" + encodeURIComponent(lastName); xhr.open("POST", targetLocation, true); xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xhr.send(data);
This way you will no longer need @MultipartConfig
on your servlet.
See also:
source share