[英]Tomcat6 response.sendRedirect() 404 error
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: 我有一个'index.jsp'页面,它位于WEB-INF文件夹之外,并包含以下代码:
<html>
<body>
<%response.sendRedirect("home.htm");%>
</body>
</html>
The page should redirect to 'home.jsp' which sits inside the WEB-INF folder. 该页面应重定向到WEB-INF文件夹中的“ home.jsp”。
The problem i am experiencing is that when I deploy my application in tomcat using Eclipse, the redirect works! 我遇到的问题是,当我使用Eclipse在tomcat中部署应用程序时,重定向有效!
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. 但是,当我通过在tomcat / bin文件夹中运行“ startup.sh”在tomcat中启动应用程序时,出现“ HTTP状态404”错误。
I am using tomcat 6.0.33 我正在使用tomcat 6.0.33
Files in /WEB-INF
folder are not publicitly accessible. /WEB-INF
文件夹中的文件不能公开访问。 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. 该文件夹专用于不应由Web浏览器直接访问的资源,例如JSP包含文件,需要预处理(前端控制器) Servlet的 JSP文件等。
Your home.jsp
seem to be just a normal JSP file which is intented to be accessed directly by URL. 您的
home.jsp
似乎只是一个普通的JSP文件,旨在通过URL直接访问。 So, put it outside the /WEB-INF
folder. 因此,将其放在
/WEB-INF
文件夹之外。 This way it's just available by http://localhost:8080/context/home.jsp . 这样, http:// localhost:8080 / context / home.jsp就可以使用它。
As to your concrete requirement, letting index.jsp
redirect to something else makes really no sense. 至于您的具体要求,让
index.jsp
重定向到其他对象确实没有任何意义。 Just change the <welcome-file>
setting in web.xml
to be home.jsp
instead of index.jsp
. 只需将
web.xml
的<welcome-file>
设置更改为home.jsp
而不是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. 这样,当最终用户直接访问http:// localhost:8080 / context文件夹而不指定任何资源文件时,将打开
home.jsp
。
Oh, please note that the .htm
extension is not the same as .jsp
extension. 哦,请注意
.htm
扩展名与.jsp
扩展名不同。 Fix that accordingly as well, if necessary. 如有必要,请相应地进行修复。
如果要重定向到“ home.jsp”,则必须将“ home.jsp”编写为sendRedirect参数。
First of all, the initial page to go to is controlled in the web.xml file by the element. 首先,要访问的初始页面在web.xml文件中由元素控制。 In stand-alone Tomcat, this defaults to /index.html, then /index.htm, and finally /index.jsp.
在独立的Tomcat中,默认值是/index.html,然后是/index.htm,最后是/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. 可能是当您从Eclipse中启动tomcat时,Web插件使用默认的web.xml来完成此事,因此默认值可能会略有不同。
Secondly, If you just want index.jsp to redirect to home.htm, you shouldn't wrap the scriptlet with HTML markup tags. 其次,如果只希望index.jsp重定向到home.htm,则不应使用HTML标记标签包装scriptlet。 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.
如果tomcat决定在执行scriptlet之前刷新输出,则将忽略sendRedirect()方法,因为已经编写了HTTP标头。
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? 最后,我假设您在Web应用程序中设置了某种映射,该映射可以处理“ home.htm”请求并转发给home.jsp?
Why are you expecting it to redirect to home.jsp
when the argument to sendRedirect
is home.htm
? 当
sendRedirect
的参数为home.htm
时,为什么期望它重定向到home.jsp
? Is there a typo? 有错字吗? If the file is in fact under
WEB-INF
, it will not be accessible to the browser. 如果文件实际上在
WEB-INF
,则浏览器将无法访问该文件。 Exposing the contents of WEB-INF
to the world would open up numerous potential security holes. 将
WEB-INF
的内容暴露给全世界将打开许多潜在的安全漏洞。 You would need to do the following if you want home.jsp
to include the contents of WEB-INF/home.htm
: 如果希望
home.jsp
包含WEB-INF/home.htm
的内容,则需要执行以下操作:
<jsp:include page="/WEB-INF/home.htm" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.