How to forward from a JAX-RS service to JSP?

蹲街弑〆低调 提交于 2019-11-29 10:33:59

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);
}

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));
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!