如何访问 servlet 的 web.xml 文件的第二个参数?

发布于 2024-08-27 16:11:38 字数 201 浏览 8 评论 0原文

假设在我的 web.xml 文件中,我定义了一个 servlet,如下所示:

<url-pattern>/MyURL/*</url-pattern>

How do i access everything pass in the * in my servlet?我打算将此方案用于漂亮的 URL。

Say in my web.xml file, I define a servlet like so:

<url-pattern>/MyURL/*</url-pattern>

How do i access anything passed in the * in my servlet? I'm planning to use use this scheme for pretty(-ish) URLs.

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

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

发布评论

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

评论(2

追风人 2024-09-03 16:11:38

HttpServletRequest#getPathInfo() 正是用于此目的。

String path = request.getPathInfo();

就这样。无需按照此处另一个答案中的建议从中提取 servlet 路径的子串。另请参阅我对您的其他问题的回答。

The HttpServletRequest#getPathInfo() is exactly for this purpose.

String path = request.getPathInfo();

That's all. No need to substring the servlet path from it as suggested in another answer here. Also see my answer on your other question.

鹿! 2024-09-03 16:11:38

在 HttpServlet 的 doGet 或 doPost 方法中,您可以使用 getRequestURI HttpServletRequest 对象来检索 URL 的路径部分。因为听起来您还想砍掉映射到 servlet 的路径部分,可以使用 getServletPath 方法,然后执行如下操作:

String path = request.getRequestURI();
if(path.startsWith(request.getServletPath())) {
    path = path.substring(request.getServletPath().length());
}

In the HttpServlet's doGet or doPost method you can use the getRequestURI method of the HttpServletRequest object to retrieve the path part of the URL. Since it sounds like you also want to chop off the part of the path that mapped to the serlvet could use the getServletPath method and then do something like this:

String path = request.getRequestURI();
if(path.startsWith(request.getServletPath())) {
    path = path.substring(request.getServletPath().length());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文