[英]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.