【问题标题】:Is there a way to map a Java Servlet to */?有没有办法将 Java Servlet 映射到 */?
【发布时间】:2010-01-18 05:54:54
【问题描述】:

我想将Servlet 映射到以/ 结尾的URL,例如/user/register//user/login/,但不是该路径下的任何其他资源,而不是/*

我试过/*/,但它不起作用。

【问题讨论】:

    标签: servlets web.xml


    【解决方案1】:

    我可能是错的,但我不确定这是可能的。通配符 * 仅用于 url 模式的末尾:

    # this is a valid pattern to match anything after root:
    */
    # this does not match anything because nothing can come after *
    /*/
    # this would match anything after the . that was htm
    *.htm
    

    【讨论】:

    • */ 也是无效的 url 模式。
    • /*/ 完全匹配 /*/。我检查了tomcat的源代码,但我认为无论如何都没有:
    【解决方案2】:

    /* 上映射一个Filter,让它决定请求是否需要通过servlet。

    if (request.getRequestURI().endsWith("/")) {
        request.getRequestDispatcher("/servleturl").forward(request, response);
    } else {
        chain.doFilter(request, response);
    }
    

    这样您就可以将所需的Servlet 映射到/servleturl

    【讨论】:

      【解决方案3】:

      welcome-file-list 就是您要查找的内容。 在welcome-file-list 下,您可以指定一个欢迎文件列表(每个都在其自己的welcome-file 标签下)。当请求 URL 以 / 结尾时,应用程序将在 URL 指向的文件夹下查找您在welcome-file-list 中提到的那些文件之一(我猜是您在那里指定的顺序),并且提供该资源。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-05
        • 2020-12-16
        • 1970-01-01
        相关资源
        最近更新 更多