Jsp 通用tomcat(v7)404重定向到外部url

Jsp 通用tomcat(v7)404重定向到外部url,jsp,tomcat,redirect,tomcat7,Jsp,Tomcat,Redirect,Tomcat7,在测试了一些建议(比如)之后,我仍然无法重定向到tomcat中不存在的webapps的外部url 我的情况如下: tomcat在url localhost上运行 在webapps/app1、webapps/app2等目录中运行的许多webapps 对localhost/app1的url调用将打开app1 对localhost/app2的url调用将打开app2 本地主机上的url调用将打开app1(tomcat中配置的自动重定向) 我的目标是实现重定向到外部网站,就像有人输入localhos

在测试了一些建议(比如)之后,我仍然无法重定向到tomcat中不存在的webapps的外部url

我的情况如下:

  • tomcat在url localhost上运行
  • 在webapps/app1、webapps/app2等目录中运行的许多webapps
  • 对localhost/app1的url调用将打开app1
  • 对localhost/app2的url调用将打开app2
  • 本地主机上的url调用将打开app1(tomcat中配置的自动重定向)
我的目标是实现重定向到外部网站,就像有人输入localhost/asdf这样的url一样。有没有一种方法可以在tomcat中全局实现这一点,比如在/conf/web.xml中添加errorpage属性

在这里回答: Tomcat版本=

你到底想要什么

A.如果要将不存在的web应用程序重定向到其他位置,请在根web应用程序(也称为默认web应用程序)中配置
错误页面

根web应用程序处理其他web应用程序未处理的所有请求

B.如果您希望为所有web应用程序的“错误404”处理配置默认设置:

在web应用程序(YMMV)之间共享JSP页面相当困难,但共享servlet非常容易

  • 准备一个执行重定向的servlet

    例如,扩展
    javax.servlet.http.HttpServlet
    并覆盖其“服务”方法。大概是这样的:

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    
        // TODO: check that request.getMethod() is one of "GET", "HEAD", "POST"?
    
        response.sendRedirect("http://www.test.com/404.html");
    
        //// Or if you want better control over HTTP status code (302/307/...):
        // response.reset();
        // response.setStatus(302);
        // response.setHeader("Location", "http://www.test.com/404.html");
     }
    
  • 将您的代码放入Tomcat的
    lib
    目录中-作为jar或包/类文件树

  • conf/web.xml
    中配置servlet,并将其映射到某个URL上,例如
    /web-INF/404

    <servlet>
        <servlet-name>bar</servlet-name>
        <servlet-class>foo.Bar</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>bar</servlet-name>
        <url-pattern>/WEB-INF/404</url-pattern>
    </servlet-mapping>
    
如果这没有帮助,还可以扩展
ErrorReportValve
类。

的可能重复项
<error-page>
  <error-code>404</error-code>
  <location>/WEB-INF/404</location>
</error-page>