This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Struts and the fly likes enctype= Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "enctype="multipart/form-data" request.getParameter() issue" Watch "enctype="multipart/form-data" request.getParameter() issue" New topic
Author

enctype="multipart/form-data" request.getParameter() issue

Sunil George
Greenhorn

Joined: Apr 04, 2006
Posts: 18
Now I working on a struts application. I want to change the form enctype to "multipart/form-data" for uploding some file to server. After this the request.getParameter() give only NULL values? How could i solve this to get the request.getParameter() values in action.

Thanks in advance
Sunil
Uma Mahi
Ranch Hand

Joined: Jan 11, 2006
Posts: 34
Use Struts. Struts 1.1 handles this automatically.

or else try this
1. download http://www.servlets.com/cos/index.html
2. invoke getParameters() on com.oreilly.servlet.MultipartRequest


Umaa Mahi<br />--SCJP1.4
Uma Mahi
Ranch Hand

Joined: Jan 11, 2006
Posts: 34
Use Struts. Struts 1.1 handles this automatically.

or else try this
download http://www.servlets.com/cos/index.html
invoke getParameters() on com.oreilly.servlet.MultipartRequest
Sunil George
Greenhorn

Joined: Apr 04, 2006
Posts: 18
Currently I using Struts 1.1. But still the problem persist? Any change needed in config file?
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
I was always getting null request parameters inside the actionForm's reset() method when I had enctype="multipart/form-data.

I was using struts 1.2.8, and have a form with multiple fields on it and recently added a <html:file> tag.

When i do not have the enctype="multipart/form-data" in the <html:form> tag, the request parameters contain all of the values on the form, and the file upload does not work, as expected.

When I add the enctype="multipart/form-data" to the <html:form> so that the <html:file> tag will work, the file upload is working, but now the request parameters (at least from the point of view of the reset() method in the actionForm do not contain any values for the form fields. It seems that the form bean properties are being set, in that if i change a value in the field and call the getter method for this field from my form bean, it returns the correct value. Further investigation revealed that struts RequestProcessor calls RequestUtils.populate() which will extract multipart form data if it is present into request parameters, that get set onto the bean, so that is why the bean has its values by the time it got to execute().

So, if things aren't working for you, i.e. your form bean.getProperty() is not working then make sure your commons upload jar files are available.

My issue was that I wanted to be able to read request parameters inside the reset() method in the action form, which is before the form bean is populated by the request processor.
I wanted to do this here to control the clearing of boolean properties used by check boxes.

After a bit (okay, a lot) of tinkering, I came up with this method so that my action form get the request parameters inside the reset() method for multipart/form-data type requests.

Struts will wrap the request with its own MultipartRequest wrapper when it detects the multipart content type (from struts' request processor code):




So when the request object passed into the action form is an instance of the MultipartRequestWrapper, it means the form has enctype="multipart/form-data".
and request parameters are available on the http header according to RFC 1867. We can then use a the commons file upload to extract text value elements and set them onto the request wrapper.

Here is my action form that does this:




Note that in the event the form is not a multipart encoding type, the normal request.getParameter should still work.
Also note that this solution may be specific to struts 1.2(.8)
[ July 03, 2006: Message edited by: Travis Hein ]

Error: Keyboard not attached. Press F1 to continue.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Thanks, Travis, for that very insightful and helpful analysis of what happens internally when you specify enctype="multipart/form-data" on a form. I plan to refer to your post whenever someone asks me why they can't access parameters while doing a file upload.

Something that puzzles me, though is your statement:
My issue was that I wanted to be able to read request parameters inside the reset() method in the action form, which is before the form bean is populated by the request processor.
I wanted to do this here to control the clearing of boolean properties used by check boxes.


You can certainly clear the boolean values in an ActionForm without reading the request. All you need to do is clear them, and then let the RequestProcessor populate them later on. Is it really necessary to work this hard? or is there something else you are doing besides just clearing the values?


Merrill
Consultant, Sima Solutions
Travis Hein
Ranch Hand

Joined: Jun 06, 2006
Posts: 161
You can certainly clear the boolean values in an ActionForm without reading the request. All you need to do is clear them, and then let the RequestProcessor populate them later on. Is it really necessary to work this hard? or is there something else you are doing besides just clearing the values?


I currently am trying to implement a post redirect get pattern, and at the moment am using the same Action and ActionForm instance for both the input and the redirected target.

so my struts mapping currently looks like:


the formAction set-property is a property on a custom ActionMapping class i created, the motivation is the JSP page gets the ActionMapping and gets the value of it's path to submit the form to. When the formAction is present, this is used instead. This way i have a not-hardcoded path on the JSP (well, tiles in my case) template.

so the flow is
  • Struts mapping /admin/system/editSystemProperty
  • reset() in editSystemPropertyForm
  • execute() in EditSystemProperty. no buttons pressed on the from, first load, the execute() then just returns forward to home
  • the forward to home redirects to /admin/system/editSystemPropertyResult
  • Struts mapping /admin/system/editSystemPropertyResult
  • reset() in editSystemPropertyForm
  • execute() in EditSystemProperty, will just forward to "home" forward
  • the home forward will load the tiles definition (i called it tiles.editForm to remind me it is in tiles coniguration)


  • A click of a form button will cause the first execute() to do stuff and then the other forwards, like ok, will redirect to a different screen.

    So the challenge is that reset() is always called twice. the first time the request parameters are available (or can be made available, by parsing multipart response ). But for the second reset() there are no request parameters, and I was always getting checkboxes unset without being able to set them (funny, the opposite as if i did not implement reset() method).

    Since the response is redirected between the usage of the forms, i can't put a token on the request, so I looked for some other delimiter to indicate the form has been submitted,

    I guess what I need to do is break apart the design to have a separate
    form for the result view, and just not do the reset in the second one.
    The way it currently is was because the system began as the single editSystemProperty action, and later evolved to use post redirect get pattern with the editSystemPropertyResult.

    But even using seperate forms, I would still need to test for the formAction request parameter in my situation, so as to not reset default-true checkboxes
    (see my other post on this: http://www.coderanch.com/t/52320/Struts/set-checkbox-checked-default)

    So I would still have something like this anyway



    But now it seems that something else about the multipart encoding is causing struts RequestProcessor to not auto-repopulate the checkbox types, as it should from the request parameters, so I am now explicity setting it here in reset(), as per my first code sample.

    I first thought is because I am extracting multipart parameters myself and I am confusing the Request processor's repopulate process, as it is doing it again and turning my checkbox into an array of items, but I tried unpacking multipart contents onto a local (separate) multipart request wrapper inside reset() and get the same result, so I really don't know what is going on there.
    Aruna Nl
    Greenhorn

    Joined: May 05, 2010
    Posts: 1
    Travis Hein wrote:I was always getting null request parameters inside the actionForm's reset() method when I had enctype="multipart/form-data.

    I was using struts 1.2.8, and have a form with multiple fields on it and recently added a <html:file> tag.

    When i do not have the enctype="multipart/form-data" in the <html:form> tag, the request parameters contain all of the values on the form, and the file upload does not work, as expected.

    When I add the enctype="multipart/form-data" to the <html:form> so that the <html:file> tag will work, the file upload is working, but now the request parameters (at least from the point of view of the reset() method in the actionForm do not contain any values for the form fields. It seems that the form bean properties are being set, in that if i change a value in the field and call the getter method for this field from my form bean, it returns the correct value. Further investigation revealed that struts RequestProcessor calls RequestUtils.populate() which will extract multipart form data if it is present into request parameters, that get set onto the bean, so that is why the bean has its values by the time it got to execute().

    So, if things aren't working for you, i.e. your form bean.getProperty() is not working then make sure your commons upload jar files are available.

    My issue was that I wanted to be able to read request parameters inside the reset() method in the action form, which is before the form bean is populated by the request processor.
    I wanted to do this here to control the clearing of boolean properties used by check boxes.

    After a bit (okay, a lot) of tinkering, I came up with this method so that my action form get the request parameters inside the reset() method for multipart/form-data type requests.

    Struts will wrap the request with its own MultipartRequest wrapper when it detects the multipart content type (from struts' request processor code):




    So when the request object passed into the action form is an instance of the MultipartRequestWrapper, it means the form has enctype="multipart/form-data".
    and request parameters are available on the http header according to RFC 1867. We can then use a the commons file upload to extract text value elements and set them onto the request wrapper.

    Here is my action form that does this:




    Note that in the event the form is not a multipart encoding type, the normal request.getParameter should still work.
    Also note that this solution may be specific to struts 1.2(.8)
    [ July 03, 2006: Message edited by: Travis Hein ]

    Hi Travis,
    We are facing the exact same issue and I added this part of the code in the reset method, and now I am able to access the request parameters in the reset method but now the issue is in the Action. After this reset method is called, looks like the Actionform is not getting populated with form fields and all the form parameters are still in the request and the form has empty data when it reaches the action file. And also, I cannot find the uploaded file either in the form or in the request in the action file, but I did see the file coming in properly in the reset method. I think this is because we are only setting the textElements in the wrapper back after we handle the request.
    This part of the code looks like changing the request and hence the bean form is not getting populated.. Did you also face this issue? I see this post was long ago, I am not able to find any solution to this. Appreciate any help.
    We are using Struts 1.3.8.

    Thanks,
    Aruna.
    Mauricio Porras P
    Greenhorn

    Joined: Aug 22, 2009
    Posts: 2
    Sunil George wrote:Now I working on a struts application. I want to change the form enctype to "multipart/form-data" for uploding some file to server. After this the request.getParameter() give only NULL values? How could i solve this to get the request.getParameter() values in action.

    Thanks in advance
    Sunil


    Just go to http://code.google.com/p/apache-commons-fileupload-lab/ and checkout the source code
     
     
    subject: enctype="multipart/form-data" request.getParameter() issue
     
    Similar Threads
    enctype
    HF example Weird result - help please
    How to get the request parameters while the enctype is multipart/form-data
    please help me
    doPost problem