I have a page that has this piece of code:
<form action="Servlet" enctype="multipart/form-data">
<input type="file" name="file">
<input type="text" name="text1">
<input type="text" name="text2">
</form>
When I use request.getParameter("text1");
in my Servlet it shows null. How can I make my Servlet receive the parameters?
All the request parameters are embedded into the multipart data. You'll have to extract them using something like Commons File Upload: http://commons.apache.org/fileupload/
Pleepleus is right, commons-fileupload is a good choice.
If you are working in servlet 3.0+ environment
, you can also use its multipart support to easily finish the multipart-data parsing job. Simply add an @MultipartConfig
on the servlet class, then you can receive the text data by calling request. getParameter()
, very easy.
You need to send the parameter like this:
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"" + urlParameterName + "\"" )
.append(CRLF);
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF);
writer.append(urlParameterValue).append(CRLF);
writer.flush();
And on servlet side, process the Form elements:
items = upload.parseRequest(request);
Iterator iter = items.iterator();
while (iter.hasNext()) {
item = (FileItem) iter.next();
if (item.isFormField()) {
name = item.getFieldName();
value = item.getString();
}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.