转自一位国外哥们儿写的分析,写的很不错:
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?
分享到:
相关推荐
入参的HttpServletRequest必须为:import jakarta.servlet.http.HttpServletRequest; 运行cmd,再该目录下执行: 执行步骤:java -jar jakartaee-migration-1.0.1.jar commons-fileupload-1.4.jar commons-...
Servlet中doGet与doPost的区别
servlet与jsp的区别 servlet与jsp的区别 servlet与jsp的区别 servlet与jsp的区别
简单介绍了jsp与servlet的一些基本的联系和区别
jsp/servlet中过滤器和监听器的区别,适用于想学习jsp/servlet并想了解及掌握过滤器和监听器来开发的同学
19.Servlet中forward和redirect的区别.avi
servlet-api.jar和javax.servlet.jar。
JavaWeb笔记08Servlet中get和post区别.pdf
jsp javabean servlet的区别
7.2.1 servlet中的请求和应答对象[/font] 7.2.2 selvlet中的servletconfig对象[/font] 7.3 servlet输出html文件[/font] 7.4 小结[/font] [/font] 第8章 httpservlet编程介绍[/font] [/font] 8.1 http简介[/font] ...
2.Get和Post的区别 在页面提交数据时,get的数据大小有限制4k,post没有限制,get请求提交的数据会在地址栏显示,post不显示,所以post比get安全. 3.有三台服务器,如果在一台服务器上登陆了这个用户,其他两台就...
url-pattern中 / 和 /* 的区别 url-pattern的优先级问题 根路径 / 的匹配问题 1 servlet url-pattern 的匹配问题 url-pattern 有三种匹配模式,分别是路径匹配、精确匹配、后缀匹配 1.1 精确匹配 <url> 中配置的...
Servlet之forward、sendRedirect、 include区别与使用实例 ,具体效果和过程看博文http://blog.csdn.net/evankaka/article/details/45169569
什么是servlet? web服务器 web应用(b/s架构软件)的目录结构: Web.xml 参数 系统如何提供表单给用户? 系统如何处理用户提交的表单 1路径问题 提交方式的区别 Servlet的4个生命阶段: 数据源(java对象)和jndi(j2ee规范)...
介绍了j2ee规范之servlet,本文档详细介绍了servlet各个版本的区别
JavaBean与servlet的关系详解
详细描述了jsp与servlet的区别以及servlet的基本概念
4 XML:dtd和schema的区别 7分 5 XML:db-》xml 6分 6 XML:schema-》xml 8分 7 XML:dtd中的细节 4分 8 XML:内部dtd编写 6分 1 Servlet:EL中间的隐式对象 4分 2 Servlet:session对象的创建 3分 3 Servlet:web程序...
servlet2.3源码隆重推出,为了方便,jar包和源码分开上传。