Tomcat6 response.sendRedirect() 404错误

发布于 2024-12-24 20:03:15 字数 448 浏览 5 评论 0原文

我想知道是否有人遇到过我遇到的这个问题,如果是的话他们是否可以为我提供解决方案。

我有一个位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱给你人给你 2024-12-31 20:03:15

/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.

<welcome-file-list>
    <welcome-file>home.jsp</welcome-file>
</welcome-file-list>

这样,当最终用户访问 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 in web.xml to be home.jsp instead of index.jsp.

<welcome-file-list>
    <welcome-file>home.jsp</welcome-file>
</welcome-file-list>

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.

递刀给你 2024-12-31 20:03:15

如果你想重定向到'home.jsp',你必须编写'home.jsp'作为sendRedirect参数。

If you want to redirect to 'home.jsp', you have to write 'home.jsp' as sendRedirect parameter.

幻想少年梦 2024-12-31 20:03:15

首先,要转到的初始页面由 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?

单挑你×的.吻 2024-12-31 20:03:15

sendRedirect 的参数是 home.htm 时,为什么您期望它重定向到 home.jsp?有错别字吗?如果该文件实际上位于 WEB-INF 下,则浏览器将无法访问该文件。将 WEB-INF 的内容公开给全世界会带来许多潜在的安全漏洞。如果您希望 home.jsp 包含 WEB-INF/home.htm 的内容,则需要执行以下操作:

<jsp:include page="/WEB-INF/home.htm" />

Why are you expecting it to redirect to home.jsp when the argument to sendRedirect is home.htm? Is there a typo? If the file is in fact under WEB-INF, it will not be accessible to the browser. Exposing the contents of WEB-INF to the world would open up numerous potential security holes. You would need to do the following if you want home.jsp to include the contents of WEB-INF/home.htm:

<jsp:include page="/WEB-INF/home.htm" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文