如何将 servlet 响应发送到 web.xml 配置的错误页面

发布于 2024-08-17 17:12:59 字数 354 浏览 2 评论 0 原文

我正在使用汤姆猫。我在 web.xml 中定义了一些 并将 404 错误映射到页面 /error/error.jsp< /代码>。我需要检测资源是否存在,如果资源不可用,则将响应状态设置为 404。

response.setStatus(404);

但是Tomcat并没有重定向到我定义的404页面,所以我的问题是,是否有任何API可以获取web.xml中定义的页面位置?我不想自己解析 web.xml

I am using Tomcat. I defined some <error-page> in web.xml and mapped 404 error to page /error/error.jsp. I need to detect if the resource exist and set the response status to 404 if the resource is not available.

response.setStatus(404);

But Tomcat does not redirect to the 404 page I defined, so my question is, is there any API to get the page location defined in web.xml? I don't want to parse the web.xml by myself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

孤蝉 2024-08-24 17:12:59

只需使用 HttpServletResponse#sendError() 带有 状态代码。例如,

File resource = new File(path, name);
if (!resource.exists()) {
    response.sendError(HttpServletResponse.SC_NOT_FOUND); // Sends 404.
    return;
}

servlet 容器将显示合适的错误页面。

注意:return 语句不是用来装饰的。它将避免同一方法块中的剩余代码继续运行并可能在应用服务器日志中产生 IllegalStateException!初学者通常认为像 sendRedirect()forward()sendError() 等方法在调用时会以某种方式自动退出方法块。因此,这是真的;)

Just use HttpServletResponse#sendError() with a status code. E.g.

File resource = new File(path, name);
if (!resource.exists()) {
    response.sendError(HttpServletResponse.SC_NOT_FOUND); // Sends 404.
    return;
}

The servletcontainer will then display the suitable errorpage.

Note: the return statement isn't there for decoration. It will avoid that the remant of the code in the same method block will continue to run and might produce IllegalStateExceptions in the appserver logs! Starters namely often think that methods like sendRedirect(), forward(), sendError(), etc somehow automagically exits the method block when invoked. This is thus not true ;)

ˉ厌 2024-08-24 17:12:59

不,没有 API。该设置是 Tomcat 内部的,它不会直接向外界公开。 (正如您所说,您可以解析 web.xml - 因为它是 XML,所以编写 XQuery 来提取它会很简单)。

另外,我认为您似乎对它到底如何工作感到困惑。您向 Tomcat 提供一个 URI,它将用于提供 404 响应的 HTML 正文。它需要能够将此 URI 解析为实际资源,就像在请求中提供的一样。因此,在许多情况下,您不需要 servlet 来检测资源是否存在 - 您需要 servlet 来包含要提供的资源。例外情况是,如果您使用 Tomcat 为某些 URL 提供静态文件系统数据,我相信这是可能的,尽管很少使用。

如果您设置了一个没有任何 servlet 的 Tomcat,您希望它提供什么服务?您到底希望它从哪里获取您的 error.jsp 页面?你希望它如何知道这一点?您需要做的就是向 Tomcat 添加至少一个 servlet(包含 error.jsp 文件),然后确保 web.xml 映射 /error/error.jsp 此 servlet 的 URL(并且资源位于 servlet 的 error 子目录中)。

完成此操作后,您应该能够手动转到 http://localhost:8080//error/error.jsp 并提供响应(可能包含一些奇怪的内容)因为没有实际的异常,但应该找到该文件)。同样,如果您在 web.xml 中正确设置了错误页面指令,则转到 umapping URL(例如 http://localhost:8080//asdfghjasgh)应该显示您将错误页面作为 404 响应。

No, there is no API. The setting is internal to Tomcat, it doesn't expose this to the outside world directly. (You can, as you said, parse the web.xml - since it's XML it would be simple to write an XQuery to pull this out).

Also I think you seem to be confused as to exactly how this is supposed to work. You supply a URI to Tomcat that it will use to serve the HTML body of a 404 response. It will need to be able to resolve this URI into an actual resource just as if it were supplied in a request. So in many cases, you don't need a servlet to detect if the resource exists - you need a servlet to contain the resource to be served. The exception being if you use Tomcat to serve static filesystem data for some URLs, which I believe is possible albeit seldom used.

If you have a Tomcat set up without any servlets, what are you expecting it to serve anyway? Where on earth are you expecting it to get your error.jsp page from? How do you expect it to know that? What you need to do is add at least one servlet to Tomcat (containing the error.jsp file), and then make sure that web.xml maps the /error/error.jsp URL to this servlet (and that the resource is positioned in the error subdirectory in the servlet).

Once this is done, you should be able to manually go to e.g. http://localhost:8080/<context>/error/error.jsp and have the response served (possibly with some weird content since there's no actual exception but the file should be found). Similarly, if you've set up the error-page directive correctly in web.xml, going to an umapped URL (e.g. http://localhost:8080/<context>/asdfghjasgh) should show you your error page as the 404 response.

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