users@jersey.java.net

Re: [Jersey] Tomcat Deployment Vs. Jetty Deployment -- no parameters from HttpServletRequest

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Wed, 19 Nov 2008 11:36:08 +0100

Hi Davis,

It is a bug in Tomcat and also Glassfish (i tried with v2 and v3 and
can reproduce the same behaviour). It is because Jersey obtains the
InputStream instance from the HttpServletRequest which is disabling
the form parsing of the parameters in the request, even though no
bytes have been read from the stream (at the end of the email is a
servlet that reproduces the same bug).

You can do this instead:

   @POST
   @Produces("text/html")
   @Consumes("application/x-www-form-urlencoded")
   public String doPost(@FormParam("x") String x, @Context
HttpRequestContext request) {
      Form f = request.getFormParameters();
   }

But take care that there are no valves in Tomcat logging parameters
otherwise Tomcat will consume the request and there will be nothing
left for Jersey to read.

Paul.

public class NewServlet extends HttpServlet {

     public void service(HttpServletRequest request,
HttpServletResponse response)
     throws ServletException, IOException {
         InputStream in = request.getInputStream();
         Map map = request.getParameterMap();
         if(map.isEmpty()) {
             System.out.println("request has no parameters");
         }
         for(Object key : map.keySet()) {
             System.out.println("key: "+key+" value: "+map.get(key));
         }
     }
}

On Nov 18, 2008, at 10:29 PM, Davis Ford wrote:

> Hi I built a simple service that accepts @POST.
>
> @Path("/incoming-fax")
> public class EfaxServlet {
>
> @POST
> @Produces("text/html")
> @Consumes("application/x-www-form-urlencoded")
> public String doPost(@Context HttpServletRequest request) {
> etc...
> }
>
> I expect a parameter named "xml" to be posted that contains the full
> XML string contents.
>
> This is a maven project, and if I run:
>
> $ mvn jetty:run-war
>
> And then use curl to send the content like so:
>
> curl -v --data-urlencode xml_at_inbound-post-original.xml -H
> "Content-Type:application/x-www-form-urlencoded"
> http://localhost:8080/efax/incoming-fax
>
> it works like a charm....
>
> If I switch over to Tomcat, either via mvn using "mvn tomcat:run-war"
> or else starting Tomcat 6 myself after dropping my .war file in there,
> I never receive any paramters from the HttpServletRequest.
>
> I added this snippet to the @POST method:
>
> Map<String, String> map = request.getParameterMap();
> if(map.isEmpty()) {
> LOGGER.error("request has no parameters");
> }
> for(String key : map.keySet()) {
> LOGGER.error("key: "+key+" value: "+map.get(key));
> }
>
> If I run this with Jetty, it sees "xml" parameter, but if I run the
> same on Tomcat, it sees no parameters at all.
>
> I'm really not seeing the problem here. Can anyone provide a clue
> what might be going on?? My thanks in advance.
>
> Regards,
> Davis
>
> My web.xml is below:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
> <servlet>
> <servlet-name>ServletAdaptor</servlet-name>
> <servlet-
> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
> class>
> <load-on-startup>1</load-on-startup>
> <!-- other init-param here -->
> </servlet>
> <servlet-mapping>
> <servlet-name>ServletAdaptor</servlet-name>
> <url-pattern>/*</url-pattern>
> </servlet-mapping>
> <session-config>
> <session-timeout>30</session-timeout>
> </session-config>
> <welcome-file-list>
> <welcome-file>index.jsp</welcome-file>
> </welcome-file-list>
> </web-app>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>