users@jersey.java.net

Re: [Jersey] Jersey empties request.getParameterMap with POST

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Tue, 05 Jan 2010 11:01:13 +0100

Hi Ramakrishnan,

On Jan 5, 2010, at 3:24 AM, Ramakrishnan Meenakshi Sundaram wrote:

> Hi all,
>
> I've evaluating Jersey for quite a while now. I find that when we
> have a @POST method - to access the form parameters, it is required
> to use either @FormParam or a MultiValuedMap.
>
> In case, there is legacy library code that accepts a
> HttpServletRequest and tries to read the form data using
> request.getParameter("<some form field name>"), it seems to return
> null.
>

Yes, this is because Jersey consumes the request entity. (I have said
this before: the servlet API is broken because requestParams
intermixes query params and form params and if the latter are present
will consume the request entity making it harder to frameworks that
layer on top of servlet to manage this edge case.)

If you want to support both @FormParam, MultiValuedMap and
HttpServletRequest.getParameter then the servlet request parameters
need to be accessed before Jersey starts processing the request. This
will make the servlet layer consume the request entity and Jersey will
work around this as appropriate. You can do that on one of two ways:

1) add a servlet filter to access the parameters; or

2) extend ServletContainer and override the service method:

https://jersey.dev.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/spi/container/servlet/ServletContainer.html
#service%28java.net.URI,%20java.net.URI,
%20javax.servlet.http.HttpServletRequest,
%20javax.servlet.http.HttpServletResponse%29

Hth,
Paul.

> Sample Code:
> @Path("/testpost")
> public class TestPost{
> @POST
> public String processPost(@FormParam("username") String
> username, @Context HttpServletRequest request){
>
> System.out.println("Username from formparam:" +
> username);//returns proper data
> //whereas to mimick the legacy code, following is the
> test code
> System.out.println("Username from request object:" +
> request.getParameter("username")); //reurns null
> }
> }
>
> I'm using Tomcat 5.5 and Jdk1.5. Any help on this would be
> definitely helpful
>
> with best regards
> Ramakrishnan
>
>
> --
> With the divine blessings of Kamakala Kameshwari
>
> View my blog http://shalomallone.blogspot.com