I 'm working on a web application. I have my jsp with enctype="multipart/form-data" and when I submit my request, I am unable to get the request parameters in servlet.
The getParameter() calls will all return null. The question is how can overcome this problem?
When it 's not enctyped, this code works fine. I know that this has been asked many times, but I did not find any straight answer
JSP
<form action="upload" method="post" enctype="multipart/form-data">
<input type="file" name="uploadfile[]" id="uploadfile" size="50" multiple="true" />
<br/><br/>
<input type="hidden" name ="e_id" value= <%=userBean.getEid%> />
<input type="hidden" name ="Uid" value= <%=userBean.getUid()%> />
<input type="submit" name ="button1" value="Upload" />
</form>
Servlet
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
int e_id =0;
String uid = null;
HttpSession session1 = request.getSession(true);
if(ServletFileUpload.isMultipartContent(request)){//process only if its multipart content
try
{
List<FileItem> multiparts = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for(FileItem item : multiparts)
{
if(!item.isFormField())
{
String name = new File(item.getName()).getName();
item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
e_id = Integer.parseInt(request.getParameter("e_id"));
uid = request.getParameter("Uid");
}
else {}
...
You need to annotated your servlet with the
@MultipartConfig
and to get the value of the parameters you use:I have a project in github with an example of how to use it: