Tomcat6 response.sendRedirect() 404错误
我想知道是否有人遇到过我遇到的这个问题,如果是的话他们是否可以为我提供解决方案。
我有一个位于 WEB-INF 文件夹外部的“index.jsp”页面,包含以下代码:
<html>
<body>
<%response.sendRedirect("home.htm");%>
</body>
</html>
该页面应重定向到位于 WEB-INF 文件夹内部的“home.jsp”。
我遇到的问题是,当我使用 Eclipse 在 tomcat 中部署应用程序时,重定向有效!
但是,当我通过在 tomcat/bin 文件夹中运行“startup.sh”在 tomcat 中启动应用程序时,出现“HTTP Status 404”错误。
我使用的是tomcat 6.0.33
I was wondering if anyone has experienced this problem I am having and, if so whether they could provide me with a solution.
I have an 'index.jsp' page which sits outside the WEB-INF folder and consists of the following code:
<html>
<body>
<%response.sendRedirect("home.htm");%>
</body>
</html>
The page should redirect to 'home.jsp' which sits inside the WEB-INF folder.
The problem i am experiencing is that when I deploy my application in tomcat using Eclipse, the redirect works!
However, when i launch my application in tomcat by running 'startup.sh' from within the tomcat/bin folder I get a 'HTTP Status 404' error.
I am using tomcat 6.0.33
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
/WEB-INF
文件夹中的文件不可公开访问。该文件夹适用于不应该由网络浏览器直接访问的资源,例如 JSP 包含文件、需要预处理(前端控制器)的 JSP 文件 servlet,等等。您的
home.jsp
似乎只是一个普通的 JSP 文件,旨在通过 URL 直接访问。因此,请将其放在/WEB-INF
文件夹外部。这样,它就可以通过 http://localhost:8080/context/home.jsp 访问。至于您的具体要求,让
index.jsp
重定向到其他内容确实没有意义。只需将web.xml
中的
设置更改为home.jsp
而不是index.jsp.
这样,当最终用户访问 http://localhost:8080/ 时,将打开
home.jsp
context 文件夹,无需指定任何资源文件。哦,请注意
.htm
扩展名与.jsp
扩展名不同。如有必要,也请相应地修复该问题。Files in
/WEB-INF
folder are not publicitly accessible. That folder is intented for resources which are not supposed to be accessed directly by the webbrowser, such as JSP include files, JSP files which require a preprocessing (front controller) servlet, etcetera.Your
home.jsp
seem to be just a normal JSP file which is intented to be accessed directly by URL. So, put it outside the/WEB-INF
folder. This way it's just available by http://localhost:8080/context/home.jsp.As to your concrete requirement, letting
index.jsp
redirect to something else makes really no sense. Just change the<welcome-file>
setting inweb.xml
to behome.jsp
instead ofindex.jsp
.This way the
home.jsp
will be opened when the enduser visits http://localhost:8080/context folder directly without specifying any resource file.Oh, please note that the
.htm
extension is not the same as.jsp
extension. Fix that accordingly as well, if necessary.如果你想重定向到'home.jsp',你必须编写'home.jsp'作为sendRedirect参数。
If you want to redirect to 'home.jsp', you have to write 'home.jsp' as sendRedirect parameter.
首先,要转到的初始页面由 web.xml 文件中的 元素控制。在独立 Tomcat 中,默认为 /index.html,然后是 /index.htm,最后是 /index.jsp。
当您从 Eclipse 中启动 tomcat 时,Web 插件可能会使用默认的 web.xml 执行自己的操作,因此默认值可能会略有不同。
其次,如果您只想将 index.jsp 重定向到 home.htm,则不应使用 HTML 标记来包装 scriptlet。如果 tomcat 决定在执行 scriptlet 之前刷新输出,则 sendRedirect() 方法将被忽略,因为 HTTP 标头已被写入。
最后,我假设您在 Web 应用程序中设置了某种映射来处理“home.htm”请求并转发到 home.jsp?
First of all, the initial page to go to is controlled in the web.xml file by the element. In stand-alone Tomcat, this defaults to /index.html, then /index.htm, and finally /index.jsp.
It may be that when you start tomcat from within Eclipse, the web plugin does it's own thing with the default web.xml and so the defaults may be slightly different.
Secondly, If you just want index.jsp to redirect to home.htm, you shouldn't wrap the scriptlet with HTML markup tags. If tomcat decides to flush the output before your scriptlet is executed, the sendRedirect() method will be ignored, as the HTTP headers will have already been written.
Finally, I assume that you have some kind of mapping set up in your web application that handles the 'home.htm' request and forwards to home.jsp?
当
sendRedirect
的参数是home.htm
时,为什么您期望它重定向到home.jsp
?有错别字吗?如果该文件实际上位于WEB-INF
下,则浏览器将无法访问该文件。将WEB-INF
的内容公开给全世界会带来许多潜在的安全漏洞。如果您希望home.jsp
包含WEB-INF/home.htm
的内容,则需要执行以下操作:Why are you expecting it to redirect to
home.jsp
when the argument tosendRedirect
ishome.htm
? Is there a typo? If the file is in fact underWEB-INF
, it will not be accessible to the browser. Exposing the contents ofWEB-INF
to the world would open up numerous potential security holes. You would need to do the following if you wanthome.jsp
to include the contents ofWEB-INF/home.htm
: