Dynamic form with or without multipart / form-data

I am developing a simple CRUD structure in java where on my HTML page I have a dynamic form (2 with multipart to create and update with file uploads and 1 with no file upload and multipart for deletion). On the server side, the request modulator checks all parameters with request.getParameterMap();and checks this hidden type input to <input type="hidden" name="returntype" value="Create">see if it is creating, updating or deleting. Based on this, it will call the necessary handlers.

Note. In my form, enctype and encoding is set to multipart / form-data strong> Note. My paramMap.size () returns 0 here, and returnType gets null, and so I get an exception from the null pointer.

If I do not use enctype and encoding at all, it works fine, but again my file upload gives me an exception that the encoding type shoud will be multipart / form-data strong>. Can someone help me in this way that I can have a dynamic form with which I can create CRUD? or why I can not use request.getParameterMap();with multipart / form-data strong> Thank you :)

Below is the request modulator code

public String identifyNow()throws ServletException, java.io.IOException
{
    UploadXmlAgent uploadAgent;
    paramMap=request.getParameterMap();
    if (paramMap == null)
        throw new ServletException(
          "getParameterMap returned null in: " + getClass().getName());

    iterator=paramMap.entrySet().iterator();
    System.out.println(paramMap.size());
    while(iterator.hasNext())
    {
        Map.Entry me=(Map.Entry)iterator.next();
        if(me.getKey().equals("returntype"))
        {
            String[] arr=(String[])me.getValue();
            returnType=arr[0];
        }
    }

    //Identified based on returnType, instantiate appropriate Handler

    if(returnType.equals("Create"))
    {
        uploadAgent=new UploadXmlAgent(realPath,request,paramMap);
        uploadAgent.retrieveXml();
                    //SOME MORE OPERATIONS  
        return uploadAgent.uploadXml();
    }
    else if(returnType.equals("Update"))
    {
        System.out.println("Update");
        uploadAgent=new UploadXmlAgent(realPath,request,paramMap);
        uploadAgent.retrieveXml();
                    //SOME MORE OPERATIONS
        return uploadAgent.uploadXml();
    }
    else if(returnType.equals("Delete"))
    {
        //SOME OPERATIONS
    }
    return returnType;
}
+2
source share
3 answers

According to the comment of another answer:

Can I use request.getParameterMap();it in any way with multipart?

, Filter, , , getParameter(), getParameterMap() JSP/Servlet. .

+2

, Commons IO FileUpload.

,

 <input type="hidden" name="formAction" value="uploadSomething">

String act = request.getParameter("formAction");
if(act.equals("uploadSomething")
{ 
// EDIT
if(ServletFileUpload.isMultipartContent(request))
{

// Create a factory for disk-based file items
FileItemFactory factory = new DiskFileItemFactory();

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload(factory);

// Parse the request
List /* FileItem */ items = upload.parseRequest(request);
}
}

. , .

+2

, . 2 .

1- multipart .

after sending the first request, go for the second request of the same form, but this time use multipart and check the value of the session variable and execute the corresponding handler.

-1
source

All Articles