Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forwarding request to a JSP

I discovered Guice last week... I'm trying some easy tricks with it. However, I'm currently blocked...

I'm trying to forward a request to a JSP in a Servlet served by an url-pattern which contains a " * ". But I receive "Error 404" all the time :(

Step by Step :


ServletModule :
serve("/test/*").with(TestServlet.class);

TestServlet :
public void doGet(HttpServletRequest req, HttpServletResponse resp)  
{

    System.err.println("Start");
    try 
    {
        req.getRequestDispatcher("/WEB-INF/layout/test.jsp").forward(req, resp);
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }

}

I get this error :

HTTP ERROR 404
Problem accessing /WEB-INF/layout/test.jsp. Reason:
/WEB-INF/layout/test.jsp

I tested with "serve("/test").with(TestServlet.class);" and it worked
I tested without Guice (by defining servlet in the web.xml) and it worked...

  • What did I do wrong?

Thank for reading!

like image 347
Pierre Avatar asked Dec 03 '22 09:12

Pierre


2 Answers

Client can't access resources from Web-INF directly (by url). So forwarding doesn't work in this case. But your servlets can. So just use include instead of forward.

like image 186
Stan Kurilin Avatar answered Dec 07 '22 23:12

Stan Kurilin


There's a good chance you didn't do anything wrong at all. There is a bug in Guice, arising from their mishandling of Include and Forward attributes against servlet standards, as described here... http://code.google.com/p/google-guice/issues/detail?id=647

The upshot is that the receiving servlet is misinformed about the path, and hence requests to load resources do not find their proper target even if they are specified correctly and even if the same code works when using web.xml (which is interpreted by your servlet engine and not by Guice).

I'm endlessly puzzled why this doesn't act as a dead-end for many many projects in Guice, so perhaps there's something in the behaviour of other servlet engine configurations which masks this error. I'm using Jetty launched explicitly in Java using Server#start(); and it is a deal-breaker for a lot of server logic.

However, the Guice team seems to have been studiously ignoring the bug for a long time, even when a patch was provided to them against v2.0. What they need is a test-case written against their SVN build but I've never succeeded given all the work needed to create stubs which emulate the servlet engine and so on.

like image 36
Cefn Hoile Avatar answered Dec 08 '22 00:12

Cefn Hoile