支持具有相同 servlet 的两个 URI

发布于 2024-12-26 17:37:59 字数 455 浏览 0 评论 0原文

目前,我们网站上的所有链接都设置为 /xxx/UseCase

我们现在希望接受带有前缀 /yyy 的传入请求,例如 /yyy/xxx/UseCase。因此,我们将有一个 servlet 接受 /yyy/xxx/UseCase/xxx/UseCase 上的请求。

我们怎样才能做到这一点?

这里最大的挑战是:许多页面使用绝对路径重定向到其他 URI。即:Patients servlet 重定向到/xxx/Insurance。问题是,如果您位于 /yyy/xxx/Patients,您现在不应该被重定向到 /xxx/Insurance - 您应该被重定向到 /yyy /xxx/保险

Currently we have all links on our site set up as /xxx/UseCase.

We would like to now accept incoming requests with the prefix /yyy such as /yyy/xxx/UseCase. So we would have one servlet that accepts requests on both /yyy/xxx/UseCase and /xxx/UseCase.

How can we do this?

Biggest challenge here being: A number of pages redirect to other URI's using the absolute path. Ie: the Patients servlet redirects to /xxx/Insurance. Issue is if you're at /yyy/xxx/Patients you shouldn't now get redirected to /xxx/Insurance - you should get redirected to /yyy/xxx/Insurance

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

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

发布评论

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

评论(2

多情癖 2025-01-02 17:37:59

您可以将 servlet 映射到多个 URL 模式:

<servlet-mapping>
    <servlet-name>fooServlet</servlet-name>
    <url-pattern>/xxx/*</url-pattern>
    <url-pattern>/yyy/*</url-pattern>
</servlet-mapping>

或者当您已经使用 Servlet 3.0 时:

@WebServlet(urlPatterns={"/xxx/*", "/yyy/*"})
public class FooServlet extends HttpServlet {
    // ...
}

如果需要,您可以使用 /yyy/xxx/* 而不是 /yyy/*

或者,如果您打算不更改 servlet 映射,您也可以创建一个 Filter ,其中将 /yyy/xxx/* 上的请求转发到 /xxx/*。像这样的事情(省略基本检查):

@WebFilter("/yyy/xxx/*")
public class FooFilter implements Filter {

    // ...

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String oldURI = req.getRequestURI().substring(req.getContextPath().length() + 1);
        String newURI = oldURI.substring(oldURI.indexOf("/"));
        req.getRequestDispatcher(newURI).forward(request, response);
    }

    // ...
}

如果您打算将浏览器地址栏中的 URL 从 /yyy/xxx/* 更改为 /xxx/*,那么您需要使用 HttpServletResponse#sendRedirect() 而不是 RequestDispatcher#forward()

         // ...
         HttpServletResponse res = (HttpServletResponse) response;
         res.sendRedirect(newURI);

You can just map a servlet on more than one URL pattern:

<servlet-mapping>
    <servlet-name>fooServlet</servlet-name>
    <url-pattern>/xxx/*</url-pattern>
    <url-pattern>/yyy/*</url-pattern>
</servlet-mapping>

Or when you're already on Servlet 3.0:

@WebServlet(urlPatterns={"/xxx/*", "/yyy/*"})
public class FooServlet extends HttpServlet {
    // ...
}

You can if necessary use /yyy/xxx/* instead of /yyy/*.

Or if you intend to untouch the servlet mapping, you could also create a Filter which forwards requests on /yyy/xxx/* to /xxx/*. Something like this (basic checks omitted):

@WebFilter("/yyy/xxx/*")
public class FooFilter implements Filter {

    // ...

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        String oldURI = req.getRequestURI().substring(req.getContextPath().length() + 1);
        String newURI = oldURI.substring(oldURI.indexOf("/"));
        req.getRequestDispatcher(newURI).forward(request, response);
    }

    // ...
}

If you intend to change the URL in the browser address bar from /yyy/xxx/* to /xxx/*, then you need to use HttpServletResponse#sendRedirect() instead of RequestDispatcher#forward():

         // ...
         HttpServletResponse res = (HttpServletResponse) response;
         res.sendRedirect(newURI);
毁虫ゝ 2025-01-02 17:37:59

我认为问题不在于您的 servlet 映射。我认为这与你如何进行重定向有关。尝试一下您的重定向逻辑...

response.sendRedirect(request.getRequestURI().replace("Patients", "Insurance"));

这样您就不必弄乱 if 语句,因为它应该在两种情况下都有效。

I don't think the problem is with your servlet mapping. I think it is with how you're doing your redirects. Try this for your redirect logic...

response.sendRedirect(request.getRequestURI().replace("Patients", "Insurance"));

This way you don't have to mess with if statements as it should work in both situations.

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