根据查询字符串更改 URL [JSP/Servlet]

发布于 2025-01-07 18:39:24 字数 212 浏览 0 评论 0原文

如果问题措辞不好,我深表歉意......我不太确定如何去问。

我将如何根据查询字符串更改 URL,例如:

如果有人单击一些可爱的胡萝卜的链接,则 URL 不是 foo.com/product.jsp?id=2,而是 foo.com/产品/一些可爱的胡萝卜。

我尝试将映射添加到 web.xml,但我认为我没有采取正确的方法。

任何帮助将不胜感激。

I apologise if the question is poorly worded... I wasn't quite sure how to go about asking.

How would I go about changing the URL depending on the query string, for example:

If someone clicks a link for some lovely carrots, instead of the URL being foo.com/product.jsp?id=2 it would be foo.com/product/some-lovely-carrots.

I tried adding mapping to web.xml, but I don't think I'm going the right way about it.

Any help would be appreciated.

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

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

发布评论

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

评论(1

画骨成沙 2025-01-14 18:39:24

这称为“漂亮 URL”或“友好 URL”。基本上,您需要创建一个过滤器或者一个前端控制器 servlet 来完成这项工作。假设您选择过滤器方向,它看起来如下所示:

private Map<String, String> mapping;

@Override
public void init() {
    mapping = new HashMap<String, String>();
    mapping.put("/product/some-lovely-carrots", "/product.jsp?id=2");
    // ...

    // You can of course also fill this map based on some XML config file or
    // even a database table.
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    String path = request.getRequestURI().substring(request.getContextPath().length());
    String target = mapping.get(path);

    if (target != null) {
        req.getRequestDispatcher(target).forward(req, res);
    } else {
        chain.doFilter(req, res);
    }
}

将此过滤器映射到 /* 的 URL 模式上,并按如下方式更改链接

<a href="${pageContext.request.contextPath}/product/some-lovely-carrots">Some lovely carrots</a>

This is called "pretty URL" or "friendly URL". Basically, you'd need to create a filter or maybe a front controller servlet to achieve the job. Assuming that you go the filter direction, it would look something like as following:

private Map<String, String> mapping;

@Override
public void init() {
    mapping = new HashMap<String, String>();
    mapping.put("/product/some-lovely-carrots", "/product.jsp?id=2");
    // ...

    // You can of course also fill this map based on some XML config file or
    // even a database table.
}

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) req;
    String path = request.getRequestURI().substring(request.getContextPath().length());
    String target = mapping.get(path);

    if (target != null) {
        req.getRequestDispatcher(target).forward(req, res);
    } else {
        chain.doFilter(req, res);
    }
}

Map this filter on an URL pattern of /* and change the link as follows

<a href="${pageContext.request.contextPath}/product/some-lovely-carrots">Some lovely carrots</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文