简体   繁体   English

重定向到配置的错误页面

[英]redirect to configured error page

I have a servlet that handles requests that match a pattern (*.sth). 我有一个Servlet,它处理与模式(* .sth)匹配的请求。 Now I want to throw a 404, if an URL was called, that matches the pattern but was not supposed to be called. 现在,我想抛出一个404,如果调用了URL,则它与模式匹配但不应该被调用。 I do this by setting the status on HttpServletResponse. 我通过在HttpServletResponse上设置状态来做到这一点。 In the web.xml is a custom error page for 404s configured. 在web.xml中是针对配置的404的自定义错误页面。 This error page is opened, if the tomcat itself throws a 404, but not with my servlet. 如果tomcat本身抛出404,则打开此错误页面,但是我的servlet没有打开。 Of course I can call the page directly, but I'm interested in a more generic way because the servlet is used in other web apps, too. 当然,我可以直接调用该页面,但是我对更通用的方式感兴趣,因为该servlet也用于其他Web应用程序中。 Is there a way to get the configured error pages to be able to redirect to them or tell the web container to use them? 有没有办法让配置的错误页面能够重定向到它们或告诉Web容器使用它们?

web.xml snippet: web.xml代码段:

<servlet>
    <display-name>Controller</display-name>
    <servlet-name>Controller</servlet-name>
    <servlet-class>com.example.Controller</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>Controller</servlet-name>
    <url-pattern >*.do</url-pattern>
</servlet-mapping>

<error-page>
    <error-code>404</error-code>
    <location>
        /error.do
    </location>
</error-page>

setting status: 设置状态:

response.setStatus(HttpServletResponse.SC_NOT_FOUND);

i think 我认为

<error-page>
    <error-code>404</error-code>
    <location>/error.do</location>
</error-page>

should be in web.xml inside conf folder of tomcat. 应该在tomcat的web.xml inside conf文件夹web.xml inside conf中。

'home' gave the right clue. “家”提供了正确的线索。 Using sendError() instead of setStatus() does what I want. 使用sendError()而不是setStatus()可以实现我想要的功能。 Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用SAML重定向未登陆我的配置页面 - Redirect using SAML is not landing in my configured page 通过java代码配置弹簧安全性的自定义403错误页面 - custom 403 error page with spring security configured via java code JAX-RS应用程序中未显示已配置的404错误页面 - Configured 404 error page not shown in JAX-RS Application 如何重定向到servlet中的错误页面? - How to redirect to error page in servlet? 重定向到同一HTML页面,但出现错误 - Redirect to same Html page with error 白标错误页面 此应用程序没有配置错误视图,因此您将其视为后备 - Whitelabel Error Page This application has no configured error view, so you are seeing this as a fallback JSP起始页错误:解析模板时出错,该模板可能不存在,或者任何已配置的模板解析器都无法访问该模板 - JSP start page error : Error resolving template, template might not exist or might not be accessible by any of the configured Template Resolvers Java-EE重定向到自定义错误页面 - Java-EE redirect to a custom error page HttpServletResponse.sendError()不会重定向到错误页面 - HttpServletResponse.sendError() does not redirect to error page 如何以编程方式重定向到异常错误页面? - How to redirect programmatically to exception error page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM