java 在 servlet 中获取请求 url 的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16501478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:08:24  来源:igfitidea点击:

Getting part of request url inside servlet

javajakarta-eeservletshttprequesturl-pattern

提问by JAVAGeek

I have an EmailVerificationServlet mapped with /ev/*url-pattern.

我有一个EmailVerification/ev/*url-pattern映射的Servlet 。

http://example.com/ev/ce52320570

How can I get this ce52320570part of the URL in my Servlet?

如何ce52320570在我的 Servlet 中获取URL 的这一部分?

protected void doPost(HttpServletRequest request, HttpServletResponse response)
                                                     throws ServletException, IOException {
      String vid = "";  // Here I need to get the id from the URL
}

回答by acdcjunior

Considering a Servlet (called EmailVerification) mapped to /ev/*:

考虑EmailVerification映射到的 Servlet(称为)/ev/*

Will the URL http://example.com/ev/ce52320570trigger the EmailVerificationservlet ?

URL 会http://example.com/ev/ce52320570触发EmailVerificationservlet 吗?

Yes. In Servlet versions 2.5 and 3.0 (maybe earlier), it'll get the subpath if you map it with *, like /ev/*, as you did.

是的。在 Servlet 版本 2.5 和 3.0(可能更早)中,如果您*像 一样使用映射它,它将获得子路径/ev/*

How can I get this ce52320570part of the URL http://example.com/ev/ce52320570?

如何获取ce52320570URL 的这一部分http://example.com/ev/ce52320570

  • request.getRequestURI()will get you the requested URL as a String, like /ev/ce52320570.

  • request.getPathInfo()gets you (if exists) everything after /ev/.

    • So in a request to /ev/123, getPathInfo()would give you /123. The same way, a request to /ev/some/other, getPathInfo()would give you /some/other.

  • request.getQueryString()should be used if you need the query parameterspart of the URL.

    • Keep in mind both getRequestURI()and getPathInfo()give you only the pathrequested. If you need to obtain the query parameters, that is, those after the ?, like /ev/something?query1=value1&other=123, only request.getQueryString()would return the query1=value1&other=123part.
  • request.getParameter(parameterName)if you need the value of a specificquery parameter.

  • request.getRequestURI()将为您提供请求的 URL String,例如/ev/ce52320570.

  • request.getPathInfo()让你(如果存在)之后的一切/ev/

    • 所以在一个请求中/ev/123getPathInfo()会给你/123。用同样的方法,一个请求/ev/some/othergetPathInfo()会给你/some/other

  • request.getQueryString()如果您需要URL的查询参数部分,则应该使用它。

    • 记住两者getRequestURI()getPathInfo()给你请求路径。如果需要获取查询参数,即?, like之后的/ev/something?query1=value1&other=123,只request.getQueryString()返回query1=value1&other=123部分。
  • request.getParameter(parameterName)如果您需要特定查询参数的值。



More examples of the URL parts in the request here.

此处请求中的 URL 部分的更多示例。

回答by Mateusz

Use request.getRequestURI()and remove what you don't need, i.e. request.getRequestURI().replace("/ev/");

使用request.getRequestURI()并删除您不需要的东西,即request.getRequestURI().replace("/ev/");