You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Terry Brick <te...@yahoo.com> on 2003/11/22 19:08:17 UTC

getParameter() returns null on multipart requests

Hello,
When I submit a multipart form to my action I am
unable to get use getParameter() on the request object
to get my form field values.  As I understand it
struts is supposed to automatically create a
MultipartRequestWrapper which allows you to do this
for multipart requests... 
I've seen some postings regarding this, but I still
don't understand what I need to do.  I think there was
some mention that MultipartRequestWrapper doesn't work
right if you're not utilizing the ActionForm?

I also tried instantiating my own
MultipartRequestWrapper by doing "new
MultipartRequestWrapper(request)".  That doesn't solve
the problem either.

Any ideas? I'm using the latest nightly build (Struts
1.2).

Thanks!


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: getParameter() returns null on multipart requests

Posted by David Friedman <hu...@ix.netcom.com>.
Martin,

I agree Terry could have had it so much easier using an ActionForm. But, I
recall that wasn't what Terry wanted to do back in the first post, hence the
wrapper example, then the follow-ups about keeping everything in one place,
that wrapper.

Is there a way I missed to get the MultiPartRequestWrapper for that action
in Terry's no-ActionForm case instead of initializing it all over again sans
ActionForm?

Personally, I prefer adding FormFiles to my ActionForm and getting uploaded
files that way.

Regards,
David

-----Original Message-----
From: news [mailto:news@sea.gmane.org]On Behalf Of Martin Cooper
Sent: Monday, November 24, 2003 7:13 PM
To: struts-user@jakarta.apache.org
Subject: Re: getParameter() returns null on multipart requests



"David Friedman" <hu...@ix.netcom.com> wrote in message
news:FGEMLPJGLEPBDPHAADCNKEACCKAA.humble@ix.netcom.com...
> Martin,
>
> I don't know if Terry got it working, but I don't see why he (or anyone)
> couldn't keep it simple by using a CommonsMultiPartRequestWrapper's
> getTextElements() or getAllElements() methods, liks so:

Why is duplicating the work "keep[ing] it simple"? Struts does all of the
parsing behind the scenes for you, and makes all of the parameters available
to you. The example code you posted does pretty much the same things that
the Struts code does behind the scenes, but by the time your action is
invoked, Struts has done it already, without you having to implement any
file upload functionality in your actions. (In fact, I'm not sure I
understand how your code can work, given that Struts will already have
parsed the input stream, making it unavailable to a second parse.)

--
Martin Cooper


>
> // Assuming wrapper is the initialized CommonsMultiPartRequestWrapper
> Hashtable textElements = wrapper.getTextElements();
> String paramOne[] = (String)textElements.get("One");
>
> // OR .... (Note the String array in both examples)
>
> Hashtable allElements = wrapper.getAllElements();
> String paramOne[] = (String)allElements.get("One");
>
> Both code works if you add it to the example code I uploaded Saturday:
> www.mail-archive.com/struts-user@jakarta.apache.org/msg87269.html
>
> Regards,
> David
>
> -----Original Message-----
> From: news [mailto:news@sea.gmane.org]On Behalf Of Martin Cooper
> Sent: Monday, November 24, 2003 6:03 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: getParameter() returns null on multipart requests
>
>
>
> "Terry Brick" <te...@yahoo.com> wrote in message
> news:20031122180817.83587.qmail@web11503.mail.yahoo.com...
> > Hello,
> > When I submit a multipart form to my action I am
> > unable to get use getParameter() on the request object
> > to get my form field values.  As I understand it
> > struts is supposed to automatically create a
> > MultipartRequestWrapper which allows you to do this
> > for multipart requests...
> > I've seen some postings regarding this, but I still
> > don't understand what I need to do.  I think there was
> > some mention that MultipartRequestWrapper doesn't work
> > right if you're not utilizing the ActionForm?
>
> It still works, but it won't do as much for you behind the scenes. ;-)
>
> From within an Action, using getParameter() should still work for regular
> (i.e. non-file) items, because the request object passed to the Action is
> the wrapped request. If you're calling that method from somewhere else, it
> may or may not work, depending on whether or not you have a wrapped
request.
> (In many places, you will not have a wrapped request, because the Servlet
> 2.2 spec does not permit such a thing, and Struts 1.x is Servlet 2.2
> compatible.)
>
> --
> Martin Cooper
>
>
> >
> > I also tried instantiating my own
> > MultipartRequestWrapper by doing "new
> > MultipartRequestWrapper(request)".  That doesn't solve
> > the problem either.
> >
> > Any ideas? I'm using the latest nightly build (Struts
> > 1.2).
> >
> > Thanks!
> >
> >
> > __________________________________
> > Do you Yahoo!?
> > Free Pop-Up Blocker - Get it now
> > http://companion.yahoo.com/
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: getParameter() returns null on multipart requests

Posted by Martin Cooper <ma...@apache.org>.
"David Friedman" <hu...@ix.netcom.com> wrote in message
news:FGEMLPJGLEPBDPHAADCNKEACCKAA.humble@ix.netcom.com...
> Martin,
>
> I don't know if Terry got it working, but I don't see why he (or anyone)
> couldn't keep it simple by using a CommonsMultiPartRequestWrapper's
> getTextElements() or getAllElements() methods, liks so:

Why is duplicating the work "keep[ing] it simple"? Struts does all of the
parsing behind the scenes for you, and makes all of the parameters available
to you. The example code you posted does pretty much the same things that
the Struts code does behind the scenes, but by the time your action is
invoked, Struts has done it already, without you having to implement any
file upload functionality in your actions. (In fact, I'm not sure I
understand how your code can work, given that Struts will already have
parsed the input stream, making it unavailable to a second parse.)

--
Martin Cooper


>
> // Assuming wrapper is the initialized CommonsMultiPartRequestWrapper
> Hashtable textElements = wrapper.getTextElements();
> String paramOne[] = (String)textElements.get("One");
>
> // OR .... (Note the String array in both examples)
>
> Hashtable allElements = wrapper.getAllElements();
> String paramOne[] = (String)allElements.get("One");
>
> Both code works if you add it to the example code I uploaded Saturday:
> www.mail-archive.com/struts-user@jakarta.apache.org/msg87269.html
>
> Regards,
> David
>
> -----Original Message-----
> From: news [mailto:news@sea.gmane.org]On Behalf Of Martin Cooper
> Sent: Monday, November 24, 2003 6:03 PM
> To: struts-user@jakarta.apache.org
> Subject: Re: getParameter() returns null on multipart requests
>
>
>
> "Terry Brick" <te...@yahoo.com> wrote in message
> news:20031122180817.83587.qmail@web11503.mail.yahoo.com...
> > Hello,
> > When I submit a multipart form to my action I am
> > unable to get use getParameter() on the request object
> > to get my form field values.  As I understand it
> > struts is supposed to automatically create a
> > MultipartRequestWrapper which allows you to do this
> > for multipart requests...
> > I've seen some postings regarding this, but I still
> > don't understand what I need to do.  I think there was
> > some mention that MultipartRequestWrapper doesn't work
> > right if you're not utilizing the ActionForm?
>
> It still works, but it won't do as much for you behind the scenes. ;-)
>
> From within an Action, using getParameter() should still work for regular
> (i.e. non-file) items, because the request object passed to the Action is
> the wrapped request. If you're calling that method from somewhere else, it
> may or may not work, depending on whether or not you have a wrapped
request.
> (In many places, you will not have a wrapped request, because the Servlet
> 2.2 spec does not permit such a thing, and Struts 1.x is Servlet 2.2
> compatible.)
>
> --
> Martin Cooper
>
>
> >
> > I also tried instantiating my own
> > MultipartRequestWrapper by doing "new
> > MultipartRequestWrapper(request)".  That doesn't solve
> > the problem either.
> >
> > Any ideas? I'm using the latest nightly build (Struts
> > 1.2).
> >
> > Thanks!
> >
> >
> > __________________________________
> > Do you Yahoo!?
> > Free Pop-Up Blocker - Get it now
> > http://companion.yahoo.com/
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-user-help@jakarta.apache.org




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: getParameter() returns null on multipart requests

Posted by David Friedman <hu...@ix.netcom.com>.
Martin,

I don't know if Terry got it working, but I don't see why he (or anyone)
couldn't keep it simple by using a CommonsMultiPartRequestWrapper's
getTextElements() or getAllElements() methods, liks so:

// Assuming wrapper is the initialized CommonsMultiPartRequestWrapper
Hashtable textElements = wrapper.getTextElements();
String paramOne[] = (String)textElements.get("One");

// OR .... (Note the String array in both examples)

Hashtable allElements = wrapper.getAllElements();
String paramOne[] = (String)allElements.get("One");

Both code works if you add it to the example code I uploaded Saturday:
www.mail-archive.com/struts-user@jakarta.apache.org/msg87269.html

Regards,
David

-----Original Message-----
From: news [mailto:news@sea.gmane.org]On Behalf Of Martin Cooper
Sent: Monday, November 24, 2003 6:03 PM
To: struts-user@jakarta.apache.org
Subject: Re: getParameter() returns null on multipart requests



"Terry Brick" <te...@yahoo.com> wrote in message
news:20031122180817.83587.qmail@web11503.mail.yahoo.com...
> Hello,
> When I submit a multipart form to my action I am
> unable to get use getParameter() on the request object
> to get my form field values.  As I understand it
> struts is supposed to automatically create a
> MultipartRequestWrapper which allows you to do this
> for multipart requests...
> I've seen some postings regarding this, but I still
> don't understand what I need to do.  I think there was
> some mention that MultipartRequestWrapper doesn't work
> right if you're not utilizing the ActionForm?

It still works, but it won't do as much for you behind the scenes. ;-)

>From within an Action, using getParameter() should still work for regular
(i.e. non-file) items, because the request object passed to the Action is
the wrapped request. If you're calling that method from somewhere else, it
may or may not work, depending on whether or not you have a wrapped request.
(In many places, you will not have a wrapped request, because the Servlet
2.2 spec does not permit such a thing, and Struts 1.x is Servlet 2.2
compatible.)

--
Martin Cooper


>
> I also tried instantiating my own
> MultipartRequestWrapper by doing "new
> MultipartRequestWrapper(request)".  That doesn't solve
> the problem either.
>
> Any ideas? I'm using the latest nightly build (Struts
> 1.2).
>
> Thanks!
>
>
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


Re: getParameter() returns null on multipart requests

Posted by Martin Cooper <ma...@apache.org>.
"Terry Brick" <te...@yahoo.com> wrote in message
news:20031122180817.83587.qmail@web11503.mail.yahoo.com...
> Hello,
> When I submit a multipart form to my action I am
> unable to get use getParameter() on the request object
> to get my form field values.  As I understand it
> struts is supposed to automatically create a
> MultipartRequestWrapper which allows you to do this
> for multipart requests...
> I've seen some postings regarding this, but I still
> don't understand what I need to do.  I think there was
> some mention that MultipartRequestWrapper doesn't work
> right if you're not utilizing the ActionForm?

It still works, but it won't do as much for you behind the scenes. ;-)

>From within an Action, using getParameter() should still work for regular
(i.e. non-file) items, because the request object passed to the Action is
the wrapped request. If you're calling that method from somewhere else, it
may or may not work, depending on whether or not you have a wrapped request.
(In many places, you will not have a wrapped request, because the Servlet
2.2 spec does not permit such a thing, and Struts 1.x is Servlet 2.2
compatible.)

--
Martin Cooper


>
> I also tried instantiating my own
> MultipartRequestWrapper by doing "new
> MultipartRequestWrapper(request)".  That doesn't solve
> the problem either.
>
> Any ideas? I'm using the latest nightly build (Struts
> 1.2).
>
> Thanks!
>
>
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/




---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: getParameter() returns null on multipart requests

Posted by Terry Brick <te...@yahoo.com>.
Thank you very much.. some good ideas!  I'll give it
shot.


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: getParameter() returns null on multipart requests

Posted by David Friedman <hu...@ix.netcom.com>.
Terry,

I know there isn't much on file uploads but it is so much easier having your
ActionForm or DynaActionForm include a FormFile object.  Unfortunately, this
doesn't work well for random amounts of images to upload on a page.  I wrote
this quick Action class to get the point across on how to get to the
MultiPartRequestHandler when your Action has no 'name="ActionForm"' setting.

This works fine as long as your form submit has two key pieces:
1) enctype="multipart/form-data"
2) method="POST"

I explained how to figure out which file is uploaded as which field name.
The output looks like this example (named my form fields "uploading" and
"uploading2" for no reason):

Field: uploading, FileName: david.html, size: 902
Field: uploading2, FileName: tutorial.pdf, size: 1477977

It's up to you to figure out how to save the binary streams. :)

Regards,
David

P.S. This file works exactly as-is for an action, if you leave the
'name="ActionForm"' blank in the struts-config.xml (or modular) struts
configuration file.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
/*
 * Created on Nov 22, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package friedman.utils;

import java.util.Enumeration;
import java.util.Hashtable;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.CommonsMultipartRequestHandler;
import org.apache.struts.upload.FormFile;

public class ParameterAction extends Action {
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws Exception {

		byte data[] = null;
		CommonsMultipartRequestHandler cmrh =
			new CommonsMultipartRequestHandler();
		Enumeration e = null;
		FormFile ff = null;
		Hashtable h = null;

		cmrh.setServlet(servlet);
		cmrh.handleRequest(request);
		h = cmrh.getFileElements();

		// Catch null pointer exception
		try {

			// Get a particular item, say upload field where 'named="uploading"'
			e = h.keys();
			while (e.hasMoreElements()) {
				String key = (String) e.nextElement();
				ff = (FormFile) h.get(key);
				System.out.println(
					"Field: "
						+ key
						+ ", FileName: "
						+ ff.getFileName()
						+ ", size: "
						+ ff.getFileSize());

				// open and write the binary data 'data'
				data = ff.getFileData();
			}

		} catch (NullPointerException npe) {
			System.out.println("Something was missing: " + npe.getMessage());
		}
		return (mapping.findForward("success"));
	}
}
}=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

-----Original Message-----
From: Terry Brick [mailto:terry_brick2000@yahoo.com]
Sent: Saturday, November 22, 2003 10:23 PM
To: Struts Users Mailing List
Subject: RE: getParameter() returns null on multipart requests


Hi, thanks for replying...

1)  I'm not using the actionform capabilities of
struts.  I generally just create my own actionform by
getting the necessary data out of the request object
(long story).  So, in other words, in my struts
config, there's no actionform associated with the
action.

2)  Yes, the form tag looks like
<form name="foo" method="post"
enctype="multipart/form-data">


--- David Friedman <hu...@ix.netcom.com> wrote:
> Terry,
>
> 1. Does your actionform receive data?
>
> 2. Did you set the enctype on your form
> appropriately for
> multipart/form-data ?
>
> Regards,
> David
>
> -----Original Message-----
> From: Terry Brick [mailto:terry_brick2000@yahoo.com]
> Sent: Saturday, November 22, 2003 1:08 PM
> To: struts-user@jakarta.apache.org
> Subject: getParameter() returns null on multipart
> requests
>
>
> Hello,
> When I submit a multipart form to my action I am
> unable to get use getParameter() on the request
> object
> to get my form field values.  As I understand it
> struts is supposed to automatically create a
> MultipartRequestWrapper which allows you to do this
> for multipart requests...
> I've seen some postings regarding this, but I still
> don't understand what I need to do.  I think there
> was
> some mention that MultipartRequestWrapper doesn't
> work
> right if you're not utilizing the ActionForm?
>
> I also tried instantiating my own
> MultipartRequestWrapper by doing "new
> MultipartRequestWrapper(request)".  That doesn't
> solve
> the problem either.
>
> Any ideas? I'm using the latest nightly build
> (Struts
> 1.2).
>
> Thanks!
>
>
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
>
>
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
>


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: getParameter() returns null on multipart requests

Posted by Terry Brick <te...@yahoo.com>.
Hi, thanks for replying...

1)  I'm not using the actionform capabilities of
struts.  I generally just create my own actionform by
getting the necessary data out of the request object
(long story).  So, in other words, in my struts
config, there's no actionform associated with the
action.

2)  Yes, the form tag looks like 
<form name="foo" method="post"
enctype="multipart/form-data">


--- David Friedman <hu...@ix.netcom.com> wrote:
> Terry,
> 
> 1. Does your actionform receive data?
> 
> 2. Did you set the enctype on your form
> appropriately for
> multipart/form-data ?
> 
> Regards,
> David
> 
> -----Original Message-----
> From: Terry Brick [mailto:terry_brick2000@yahoo.com]
> Sent: Saturday, November 22, 2003 1:08 PM
> To: struts-user@jakarta.apache.org
> Subject: getParameter() returns null on multipart
> requests
> 
> 
> Hello,
> When I submit a multipart form to my action I am
> unable to get use getParameter() on the request
> object
> to get my form field values.  As I understand it
> struts is supposed to automatically create a
> MultipartRequestWrapper which allows you to do this
> for multipart requests...
> I've seen some postings regarding this, but I still
> don't understand what I need to do.  I think there
> was
> some mention that MultipartRequestWrapper doesn't
> work
> right if you're not utilizing the ActionForm?
> 
> I also tried instantiating my own
> MultipartRequestWrapper by doing "new
> MultipartRequestWrapper(request)".  That doesn't
> solve
> the problem either.
> 
> Any ideas? I'm using the latest nightly build
> (Struts
> 1.2).
> 
> Thanks!
> 
> 
> __________________________________
> Do you Yahoo!?
> Free Pop-Up Blocker - Get it now
> http://companion.yahoo.com/
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 
> 
>
---------------------------------------------------------------------
> To unsubscribe, e-mail:
> struts-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail:
> struts-user-help@jakarta.apache.org
> 


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


RE: getParameter() returns null on multipart requests

Posted by David Friedman <hu...@ix.netcom.com>.
Terry,

1. Does your actionform receive data?

2. Did you set the enctype on your form appropriately for
multipart/form-data ?

Regards,
David

-----Original Message-----
From: Terry Brick [mailto:terry_brick2000@yahoo.com]
Sent: Saturday, November 22, 2003 1:08 PM
To: struts-user@jakarta.apache.org
Subject: getParameter() returns null on multipart requests


Hello,
When I submit a multipart form to my action I am
unable to get use getParameter() on the request object
to get my form field values.  As I understand it
struts is supposed to automatically create a
MultipartRequestWrapper which allows you to do this
for multipart requests...
I've seen some postings regarding this, but I still
don't understand what I need to do.  I think there was
some mention that MultipartRequestWrapper doesn't work
right if you're not utilizing the ActionForm?

I also tried instantiating my own
MultipartRequestWrapper by doing "new
MultipartRequestWrapper(request)".  That doesn't solve
the problem either.

Any ideas? I'm using the latest nightly build (Struts
1.2).

Thanks!


__________________________________
Do you Yahoo!?
Free Pop-Up Blocker - Get it now
http://companion.yahoo.com/

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org