views:

760

answers:

4

I have a servlet that is used for many different actions, used in the Front Controller pattern. Does anyone know if it is possible to tell if the data posted back to it is enctype="multipart/form-data"? I can't read the request parameters until I decide this, so I can't dispatch the request to the proper controller.

Any ideas?

+4  A: 

Yes, the Content-type header in the user agent's request should include multipart/form-data as described in (at least) the HTML4 spec:

http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

Loren Segal
This is what I was looking for; Thanks!
pkaeding
+3  A: 

You can call a method to get the content type.

http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/servlet/ServletRequest.html#getContentType()

According to http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2, the content type will be "multipart/form-data".

Don't forget that:

  1. request.getContent() may return null.

  2. request.getContent() may not be equal to "multipart/form-data", but may just start with it.

So, with all this in mind:

if (request.getContentType() != null && 
    request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1 ) 
{
    << code block >>
}
Kyle Boon
+1  A: 

You'll have to read the request parameters in order to determine this, at least on some level. The ServletRequest class has a getContentType method that you'll want to look at.

enricopulatzo
+2  A: 

If you are going to try using the request.getContentType() method presented above, be aware that:

  1. request.getContent() may return null.
  2. request.getContent() may not be equal to "multipart/form-data", but may just start with it.

With this in mind, the check you should run is :

if (request.getContentType() != null && request.getContentType().toLowerCase().indexOf("multipart/form-data") > -1 ) {
// Multipart logic here
}
Darren Hicks