How to forward from a JAX-RS service to JSP?
Asked Answered
J

2

7

JBoss Version: 4.2.3GA. This works in apache tomcat 6.0. In JBoss, I had to add the following setting: -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=false to get the forward to work, but now when I load the page I get the error below. It feels like I am doing this in a way JBoss doesn't like, but I haven't seen any other examples. Does anyone know a way to get this to work?

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

...


@GET
@Path("/forward")
public String forward(
    @Context final HttpServletRequest request,
    @Context final HttpServletResponse response) throws Exception
{
  RequestDispatcher dispatcher = request.getRequestDispatcher("/index.html");
  dispatcher.forward(request, response);
  return "";
}

The exception:

java.lang.ClassCastException: $Proxy114 cannot be cast to javax.servlet.ServletRequestWrapper
    com.itt.scout.server.servlet.admin.config.ConfigController.forward(ConfigController.java:46)
    sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    java.lang.reflect.Method.invoke(Method.java:597)
    com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$VoidOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:151)
    com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:70)
    com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:279)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
    com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:86)
    com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:136)
    com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:74)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1357)
    com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1289)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1239)
    com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1229)
    com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:420)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:497)
    com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:684)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
Jittery answered 8/8, 2011 at 15:14 Comment(2)
You aren't using servlets directly. You are using JAX-WS (webservices). I removed the "servlet" reference and tag. To learn what a real servlet actually is, check our servlets wiki page stackoverflow.com/tags/servlets/infoMaidstone
I am doing MVC with a restlet controller and a jsp view. This probably looked funny in my example because I should not have been forwarding the requests like that.Jittery
J
16

After getting some help from another location, I realized that I was connecting my JSP and restlet in a funny way, and what I really wanted to do was use a Viewable. This also works way better in JBoss. Here is a summary of what I ended up with:

import javax.ws.rs.core.Context;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import com.sun.jersey.api.view.Viewable;


@GET
@Path("/index")
public Viewable index(
    @Context HttpServletRequest request,
    @Context HttpServletResponse response) throws Exception
{
  request.setAttribute("key", "value");
  return new Viewable("/jsps/someJsp.jsp", null);
}
Jittery answered 9/8, 2011 at 11:52 Comment(3)
Thanks for this answer. What if this method can either write a response or forward to another URI? Then this method can either return a Response or a Viewable? Would this require us to declare this method to return an Object?Bouquet
Thank you, that helped. The next challenge is how to force a redirect -instead of a forward- to a JSP page.Faris
can you please share the Viewable dependency to be used in JBoss?Loathly
A
5

If you are not using Jersey, you may want to do this:

@Path("")
@ApplicationPath("blog")
@Stateless
public class BlogApplication extends Application {

    @EJB private PostEJB postEJB;

    @GET
    public void getHome(@Context HttpServletRequest request, 
                        @Context HttpServletResponse response) {
        request.setAttribute("posts", postEJB.getPosts());
        request.getRequestDispatcher("/WEB-INF/pages/blog/home.jsp")
               .forward(request, response);
    }

    @Override
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>(Arrays.asList(BlogApplication.class));
    }

}
Anthill answered 23/10, 2014 at 20:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.