简体   繁体   English

Servlet:如何获取自己的URL模式?

[英]Servlet: How to get own URL pattern?

Can a servlet or filter look up its own URL pattern? servlet或过滤器可以查找自己的URL模式吗?

Meaning, if I bind some servlet or filter to /first/* and /second/* and a request comes in, can I find out which of the two patterns triggered it? 意思是,如果我将一些servlet或过滤器绑定到/first/*/second/*并且请求进来,我可以找出这两个模式中的哪一个触发了它吗?

Even if a servlet is bound only to one pattern, is there a way to look it up from inside the servlet (instead of hard-coding a value)? 即使servlet只绑定到一个模式,有没有办法从servlet内部查找(而不是硬编码一个值)?

This method on the HttpServletRequest class will help you. HttpServletRequest类上的这个方法可以帮到你。 You'll get an instance of HttpServletRequest on any of the Servlet methods invoked by a HTTP Request. 您将在HTTP请求调用的任何Servlet方法上获得HttpServletRequest的实例。

getServletPath getServletPath

java.lang.String getServletPath() Returns the part of this request's URL that calls the servlet. java.lang.String getServletPath()返回此请求调用servlet的URL的一部分。 This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string. 此路径以“/”字符开头,包括servlet名称或servlet的路径,但不包含任何额外的路径信息或查询字符串。 Same as the value of the CGI variable SCRIPT_NAME. 与CGI变量SCRIPT_NAME的值相同。

Take a look at this: 看看这个:

http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath() http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html#getServletPath()

You can get the <url-pattern> registration for a servlet by 您可以通过以下<url-pattern>获取servlet的<url-pattern>注册

ServletContext servletContext = getServletContext();
ServletRegistration servletRegistration = servletContext.getServletRegistration();
java.util.Collection<java.lang.String> mappings = servletRegistration.getMappings()

and get the 得到了

final String path = getServletPath();

from request as Andreeas suggested and try to find out mapping by comparing string patterns 来自Andreeas建议的request ,并试图通过比较字符串模式找出映射


Javadocs 的Javadoc

If you specifically want the URL mappings, there are a few ways, but they all require some information from the deployment. 如果您特别需要URL映射,则有几种方法,但它们都需要部署中的一些信息。

For example, if you know the name of the Servlet, you can use ServletContext#getServletRegistration(String) 例如,如果您知道Servlet的名称,则可以使用ServletContext#getServletRegistration(String)

ServletContext context = ...;
Collection<String> mappings = context.getServletRegistration("servlet-name").getMappings();

If you don't know the name, you can get them all with ServletContext#getServletRegistrations() 如果您不知道名称,可以使用ServletContext#getServletRegistrations()获取所有名称。

Map<String, ? extends ServletRegistration> registrations = context.getServletRegistrations();

and try to find yours, maybe by comparing classes (your servlet class versus the class name from the ServletRegistration ). 并尝试找到你的,可能是通过比较类(你的servlet类与ServletRegistration的类名)。

Note that you will still probably have to try matching your current request's URL to the Servlet's url mappings to be sure. 请注意,您仍可能必须尝试将当前请求的URL与Servlet的url映射匹配以确保。 You'd have to go to the Specification to find out how the mappings actually work. 您必须转到规范以了解映射的实际工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM