users@jersey.java.net

Re: [Jersey] javax.servlet.ServletException: non-HTTP request or response

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Fri, 31 Jul 2009 10:40:01 +0200

Hi Saeed,

The URL you are using:

   http://localhost:8080/CustomerManager/services/HellowWorld

will not match the resource class HelloWorldResource, you need to use:

   http://localhost:8080/CustomerManager/services/hellowworld

With the former URL Jersey will return a 404 response to the Servlet
container.

I do not think this is a Jersey issue. I think there may be something
with your set up for filtering. From the stack trace it appears that
there is a filter, org.jboss.web.tomcat.filters.ReplyHeaderFilter, and
there appears to be some security stuff set up.

 From docjar of HttpServlet:

     http://www.docjar.com/html/api/javax/servlet/http/HttpServlet.java.html
   819 public void service(ServletRequest req, ServletResponse
res)
   820 throws ServletException, IOException
   821 {
   822 HttpServletRequest request;
   823 HttpServletResponse response;
   824
   825 try {
   826 request = (HttpServletRequest) req;
   827 response = (HttpServletResponse) res;
   828 } catch (ClassCastException e) {
   829 throw new ServletException("non-HTTP request or
response");
   830 }
   831 service(request, response);
   832 }
   833 }

You can see the exception is being thrown because the ServletRequest
and/or ServletResponse are not insyances of HttpServletRequest and
HttpServletResponse respectively. And it is probably due to a filter
wrapping the request/response in something that is not an instance
HttpServletRequest and HttpServletResponse.

Paul.

On Jul 30, 2009, at 7:15 PM, Saeed Ansari wrote:

> Hi all,
> I am new user of jersey library. I have a sample resource class as
> follows:
>
> @Path("/helloworld")
> public class HelloWorldResource {
> // The Java method will process HTTP GET requests
> @GET
> // The Java method will produce content identified by the MIME
> Media
> // type "text/plain"
> @Produces("application/xml")
> public String getClichedMessage() {
> // Return some cliched textual content
> return "Hello World";
> }
>
> }
>
> and below is the web.xml of the project:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
> " xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd
> " id="WebApp_ID" version="2.5">
> <display-name>CustomerManager</display-name>
> <servlet>
> <display-name>Test</display-name>
> <servlet-name>Test</servlet-name>
> <servlet-
> class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-
> class>
> </servlet>
> <servlet-mapping>
> <servlet-name>Test</servlet-name>
> <url-pattern>/services/*</url-pattern>
> </servlet-mapping>
> </web-app>
>
> when I call this address: (CustomerManager is the war file name)
> http://localhost:8080/CustomerManager/services/HellowWorld
>
> I get this exception:
> javax.servlet.ServletException: non-HTTP request or response
> at javax.servlet.http.HttpServlet.service(HttpServlet.java:818)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 290)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> org
> .jboss
> .web
> .tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
> at
> org
> .apache
> .catalina
> .core
> .ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
> 235)
> at
> org
> .apache
> .catalina
> .core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
> at
> org
> .apache
> .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
> 235)
> at
> org
> .apache
> .catalina.core.StandardContextValve.invoke(StandardContextValve.java:
> 191)
> at
> org
> .jboss
> .web
> .tomcat
> .security
> .SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
> at
> org
> .jboss
> .web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
> at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process
> (SecurityContextEstablishmentValve.java:126)
> at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke
> (SecurityContextEstablishmentValve.java:70)
> at
> org
> .apache
> .catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
> at
> org
> .apache
> .catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
> at
> org
> .jboss
> .web
> .tomcat
> .service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:
> 158)
> at
> org
> .apache
> .catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
> 109)
> at
> org
> .apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
> 330)
> at
> org
> .apache.coyote.http11.Http11Processor.process(Http11Processor.java:
> 829)
> at org.apache.coyote.http11.Http11Protocol
> $Http11ConnectionHandler.process(Http11Protocol.java:598)
> at org.apache.tomcat.util.net.JIoEndpoint
> $Worker.run(JIoEndpoint.java:447)
> at java.lang.Thread.run(Unknown Source)
>
> What is the problem with my code?
>
> Best regards,
> Saeed