将请求转发到 JSP
我上周发现了 Guice...我正在用它尝试一些简单的技巧。但是,我目前被阻止...
我正在尝试将请求转发到由包含“ * ”的 url-pattern 提供服务的 Servlet 中的 JSP。但我总是收到“错误 404”:(
一步一步:
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();
}
}
我收到此错误:
HTTP 错误 404
访问 /WEB-INF/layout/test.jsp 时出现问题。原因:
/WEB-INF/layout/test.jsp
我用“serve(”/test”).with(TestServlet.class);”进行了测试它起作用了
我在没有 Guice 的情况下进行了测试(通过在 web.xml 中定义 servlet)并且它有效...
- 我做错了什么?
感谢阅读!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
客户端无法直接(通过 url)访问 Web-INF 中的资源。所以在这种情况下转发不起作用。但您的 servlet 可以。因此,只需使用
include
而不是forward
即可。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 offorward
.你很有可能根本没有做错任何事。 Guice 中存在一个错误,是由于他们根据 servlet 标准错误地处理了 Include 和 Forward 属性而引起的,如此处所述......
http://code.google.com/p/google-guice /issues/detail?id=647
结果是接收 servlet 错误地了解了路径,因此加载资源的请求找不到正确的目标,即使它们被正确指定并且即使相同的代码可以工作使用 web.xml 时(由您的 servlet 引擎解释,而不是由 Guice 解释)。
我非常困惑为什么这不会成为 Guice 中许多项目的死胡同,所以也许其他 servlet 引擎配置的行为中存在某些东西掩盖了这个错误。我正在使用在 Java 中使用 Server#start() 显式启动的 Jetty;这对于很多服务器逻辑来说是一个破坏性的因素。
然而,Guice 团队似乎很长一段时间以来一直刻意忽略该错误,即使为他们提供了针对 v2.0 的补丁。他们需要的是针对他们的 SVN 构建编写的测试用例,但考虑到创建模拟 servlet 引擎等的存根所需的所有工作,我从未成功。
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.
该问题已在 guice 和 guice servlet 3.1.1 中得到部分修复,但仍然存在一个问题:
当使用星号模式 '/*' 映射 servlet 时,如下所示:
并将 MyServlet.java 转发到一个 jsp 页面,然后转发() 仅当 jsp 页面没有下划线时才起作用(因此,myservlet.jsp 将起作用,my_servlet.jsp 将不起作用)。
现在这解释了为什么 WEB-INF 转发对于用 /* 映射的 servlet 不起作用。原因是 WEB-INF 包含一个破折号字符,由于某种原因,该字符给 guice servlet 带来了问题。
尝试上面的示例时,请确保在尝试验证上述情况时将文件 myservlet.jsp 重命名为 my_servlet.jsp。
我不知道为什么会发生这种奇怪的案件。
注意:我使用的是 Tomcat 6.0.35
要让 Guice 3.1.1 将这些添加到您的 pom.xml
或者您可以从以下位置下载 jar:
Guice Servlet Jar
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/
The problem has been partially fixed in guice and guice servlet 3.1.1 with one problem still taking place:
When mapping a servlet using the asterisk pattern '/*' as below:
And have MyServlet.java forward to a jsp page, then the forward() will only work if the jsp page has no underscores (So, myservlet.jsp will work, my_servlet.jsp wont work).
Now this explains why WEB-INF forwarding does not work for a servlet mapped with /*. The reason is that WEB-INF contains a dash character which for some reason is creating a problem for guice servlet.
When trying the example above, make sure to rename the file myservlet.jsp to my_servlet.jsp when trying the cases to verify the case above.
I have no idea why this weird case is taking place.
NOTE: I'm using Tomcat 6.0.35
To have Guice 3.1.1 add these to your pom.xml
Or you can download the jars from:
Guice Servlet Jar
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/