2011-08-21 43 views
0

我有一个Web应用程序,我有一个问题 如果网页并不在我的应用程序存在我有以下错误页面如何捕捉一个HTTP 404在Java中

HTTP Status 404 - /myapplication/department/home.html 

type Status report 

message /myapplication/department/home.html 

description The requested resource (/myapplication/department/home.html) is not available. 

我要转发用户访问索引页面,如果出现这种问题,不知道该怎么办

回答

4

只需在web.xml中指定索引页面为404默认错误页面即可。

<error-page> 
    <error-code>404</error-code> 
    <location>/index.jsp</location> 
</error-page> 

我假设您知道这对用户体验和SEO不利?如果它是例如你已经(重新)移动的页面,那么你应该创建一个过滤器,它将301重定向到所需的位置。

+0

非常感谢如果index.jsp位于home.html页面的一个文件夹中,那该怎么办。我试图写 ../index.jsp但它不起作用。你能告诉我如何退回到URL – Bachayer

+0

这个给出的例子应该已经这样做了。前导斜杠'/'使其相对于webapp的上下文根。它会显示'/ myapplication/index.jsp'。 – BalusC

+0

好吧,我试过它显示/myapplication/department/index.jsp。 – Bachayer