当前位置:首页 > 开发 > 编程语言 > Servlet > 正文

Servlet中/和/*的区别

发表于: 2015-01-14   作者:coffee_yan   来源:转载   浏览:
摘要: 转自一位国外哥们儿写的分析,写的很不错: Let's consider this servlet mapping in web.xml that defines "App" as the default servlet: <servlet-mapping> <servlet-name>App</servlet-name>
转自一位国外哥们儿写的分析,写的很不错:
Let's consider this servlet mapping in web.xml that defines "App" as the default servlet:

 <servlet-mapping>
 <servlet-name>App</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>

Now call http://server/context/path/to/test.html. Here are the request properties:
getPathInfo() : null
getServletPath() : "/path/to/test.html"
Why is path info null, and why does the servlet path have what we could have expected to see in path info?

Let's change web.xml like this, so that "App" eats the same URLs but is no more the default servlet:

 <servlet-mapping>
 <servlet-name>App</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>

And call again the same http://server/context/path/to/test.html:
getPathInfo() : "/path/to/test.html"
getServletPath() : "" (empty string)
WTF? Is it a bug in the servlet engine? Let's check the servlet specification...

In the "Mapping request to servlets" chapter, the specification of mappings is described:
A string beginning with a ‘/’ character and ending with a ‘/*’ postfix is used for path mapping.
A string beginning with a‘*.’ prefix is used as an extension mapping.
A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
All other strings are used for exact matches only.
When writing the "/" mapping, we use rule 3, whereas when using the "/*" mapping, we use rule 1.

So that means that a servlet that needs to map a resource according to the request URL must behave differently depending on whether it is defined as the default servlet or mounted to a particular location. What's the reason for this?

Servlet中/和/*的区别

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号