4

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?

2

4 Answers 4

6

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/

Sign up to request clarification or add additional context in comments.

Comments

2

Use getParts()

2 Comments

From what class is getParts() from?
Ooooh, underrated answer!
2

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.

Tutorial - Uploading Files with Java Servlet Technology

Comments

1

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(); }} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.