JSP Submit Request

I discovered Guice last week ... I try easy tricks with it. However, I am currently blocked ...

I am trying to redirect a request to a JSP in a Servlet served by a url pattern that contains " * ". But I get "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
The problem with access is / WEB -INF / layout / test.jsp. Cause:
/WEB-INF/layout/test.jsp

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

  • What I did wrong?

Thank you for reading!

+4
source share
3 answers

The client cannot directly access resources from Web-INF (at the URL). Therefore, forwarding does not work in this case. But your servlets can. So just use include instead of forward .

+7
source

There’s a good chance that you haven’t done anything wrong. Guice has an error related to their improper use of the Include and Forward attributes with respect to servlet standards, as described here ... http://code.google.com/p/google-guice/issues/detail?id=647

The result is that the receiving servlet is incorrectly informed about the path, and therefore requests for loading resources do not find their target, even if they are specified correctly and even if the same code works when using web.xml (which is interpreted from your servlet, and not from Guice).

I am infinitely puzzled why this does not act as a dead end for many many projects in Guice, so there may be something in the behavior of other servlet kernel configurations that mask this error. I am using Jetty running explicitly in Java using Server # start (); and it is a gap for a lot of server logic.

However, the Guice team seems to have painstakingly ignored the bug for a long time, even when the patch was provided to them against v2.0. What they need is a test script written against their SVN assembly, but I never managed to do all the work needed to create stubs that emulate the servlet engine, etc.

+6
source

The problem is partially fixed in guice and guice servlet 3.1.1 with one problem:

When matching a servlet using the asterisk '/ *' pattern, as shown below:

 serve("/myservlet/*").with(MyServlet.class); 

And send MyServlet.java to the jsp page, then the forward () function will work only if there are no underlining on the jsp page (so myservlet.jsp will work, my_servlet.jsp will not work).

 // This WORKS req.getRequestDispatcher("/myservlet.jsp").forward(req,resp); // These DONT WORK (any file having _ or - characetsrs wont work) req.getRequestDispatcher("/my_servlet.jsp").forward(req,resp); req.getRequestDispatcher("/my-servlet.jsp").forward(req,resp); req.getRequestDispatcher("/WEB-INF/myservlet.jsp").forward(req,resp); 

Now this explains why WEB-INF forwarding does not work for the servlet associated with / *. The reason is because WEB-INF contains a dash, which for some reason creates a problem for the guice servlet. If you try to run the above example, be sure to rename the myservlet.jsp file to my_servlet.jsp when trying to verify this case.

I have no idea why this strange case is taking place. NOTE. I am using Tomcat 6.0.35

To Guice 3.1.1 add them to your pom.xml

  <dependency> <groupId>org.sonatype.sisu</groupId> <artifactId>sisu-guice</artifactId> <version>3.1.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.sonatype.sisu.inject</groupId> <artifactId>guice-servlet</artifactId> <version>3.1.1</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.sonatype.sisu.inject</groupId> <artifactId>guice-assistedinject</artifactId> <version>3.1.1</version> <scope>compile</scope> </dependency> 

Or you can download jars from:

Guice servlet jug

http://repo1.maven.org/maven2/org/sonatype/sisu/inject/guice-servlet/3.1.1/

Guice jar

http://repo1.maven.org/maven2/org/sonatype/sisu/sisu-guice/3.1.1/

+4
source

Source: https://habr.com/ru/post/1343454/


All Articles