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

后端 未结 2 1757
南旧
南旧 2020-12-19 06:05

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

相关标签:
2条回答
  • 2020-12-19 06:28

    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);
    }
    
    0 讨论(0)
  • 2020-12-19 06:39

    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));
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题