本文介绍了在servlet中获取请求url的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 EmailVerification 使用 / ev / * url-pattern映射的Servlet。

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

http://example.com/ev/ce52320570

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

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

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


推荐答案

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

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

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

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.




  • 会以字符串为您提供所请求的网址,例如 / ev / ce52320570

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

      / ev /之后获取(如果存在)所有内容


      • 所以在请求 / ev / 123 getPathInfo()会给你 / 123 。同样的方式,对 / ev / some / other getPathInfo()的请求会给你 / some / other

      • 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 parameters part of the URL.


      • 请记住 getRequestURI() getPathInfo()只为您提供 路径 。如果您需要获取查询参数,即之后的那些参数,如 / ev / something?query1 = value1& other = 123 ,只有 request.getQueryString()将返回 query1 = value1& other = 123 part。

      • Keep in mind both getRequestURI() and getPathInfo() give you only the path requested. 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=123 part.

      如果您需要 特定 查询参数的值。


      • 求助于如果是多值参数

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

      More examples of the URL parts in the request here.

      这篇关于在servlet中获取请求url的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 06:55