转自:http://blog.sina.com.cn/s/blog_6189ab480100j9rk.html
1.介绍Servlet的基本概念(***)
2.开发第一个Servlet应用(**)
3.分析Servlet应用(***)
4.使用Servlet来发送用户输入的信息
5.如何在服务器端获得表单提供的数据
6.请求路径
URL METHODS BREAKDOWN (JavaEE 6):
http://[host]:[port][request-path]?[query-string] - requestURL = http://localhost:8080/myapp/users/profile.xhtml - requestURI = /myapp/users/profile.xhtml = contextPath + servletPath + pathInfo - contextPath = /myapp = the first segment after hostname (unless the app runs and the root app with context /) - servletPath = /users/profile.xhtml (part after contextPath to the servlet that handled the request) - pathInfo = null (what remains after servletPath up to the queryString; would return '/dummy' if the url was '.../profile.xhtml/dummy?id=007') - queryString = id=007
1.容器试图在web.xml中寻找匹配的请求URI。如果找到了,完整的请求URI(除去context路径)即servlet路径。
这时HttpServletRequest.getPathInfo()为null
2.使用/作为分隔符,将请求URI转化为目录树,并通过递归匹配web.xml中URL-PATTERN,获得最长的匹配路径.匹配部分即servlet path
其余为path info
3.如果请求URI的最后一个节点包含一个扩展名(如:.jsp),servlet容器会将它和处理这个种特定的扩展名servlet匹配.
这时, 完整的请求URI, 减去context path即servlet path, 并path info为null.
4.如果容器仍然不能匹配请求路径, 它会将请求forward到默认的servlet.如果没有默认的servlet, 它会发送一条说明服务器没有找到
指定文件未找到的错误信息(404)
Table 1: Example Context Set Up
--------------------------------------------------------------------------------
Context Path /catalog
--------------------------------------------------------------------------------
Servlet Mapping
Pattern: /lawn/* Servlet: LawnServlet
Servlet Mapping
Pattern: /garden/* Servlet: GardenServlet
Servlet Mapping
Pattern: *.jsp Servlet: JSPServlet
Table 2: Observed Path Element Behavior
--------------------------------------------------------------------------------
Request Path Path Elements
--------------------------------------------------------------------------------
/catalog/lawn/index.html ContextPath: /catalog
ServletPath: /lawn
PathInfo: /index.html
/catalog/garden/implements/ ContextPath: /catalog
ServletPath: /garden
PathInfo: /implements/
/catalog/help/feedback.jsp ContextPath: /catalog
ServletPath: /help/feedback.jsp
PathInfo: null
------------------------------------
注意: 下述所说的 URL 都未包含 ContextPath
Servlet 映射的 URL 规范
1. 以 / 开始并以 /* 结束的表示路径映射, 即 /Test/* 表示请求路径以 /Test/ 开始的 URL 映射到该 Servlet 处理
2. 以 *. 开始的表示扩展名映射, 即 *.abc 表示所有以扩展名为 abc 的请求映射到该 Servlet 处理
3. 以 / 开始的表示完全映射, 即 /Test 表示请求 URL 为 /Test 映射到该 Servlet 处理
4. URL Pattern 为 / 表示映射成默认的 Servlet, 当其他 URL 映射未匹配时, 使用默认的 Servlet
路径匹配顺序
1. 先匹配完全映射的 URL
2. 再次匹配路径映射的 URL, 并且最长的路径的匹配优先
3. 再次匹配扩展名映射
4. 最后使用默认的 Servlet
以下为示例
Servlet 映射
path pattern servlet
/foo/bar/* servlet1
/baz/* servlet2
/catalog servlet3
*.bop servlet4
incoming path servlet handling request
/foo/bar/index.html servlet1
/foo/bar/index.bop servlet1
/baz servlet2
/baz/index.html servlet2
/catalog servlet3
/catalog/index.html “default” servlet
/catalog/racecar.bop servlet4
/index.bop servlet4
各种匹配模式下的 ServletPath 和 PathInfo
1. 完全匹配 ServletPath = 匹配模式 PathInfo = null
2. 扩展名匹配 ServletPath = 请求的URL PathInfo = null
3. 路径匹配 ServletPath = 匹配模式剪去 "/*" PathInfo = 请求URL 剪去 ServletPath
4. 默认 Servlet 匹配 ServletPath = 请求的URL PathInfo = null
如果同样的 URL 被映射到多个 Servlet 时, Serlvet 规范未做说明, tomcat(一个很好用的JSP运行平台) 使用最后一个映射的 Servlet
如果覆盖了容器内设的 Servlet 映射, 使用新的映射
本文介绍了Servlet的基本概念、开发流程及请求处理机制。详细讲解了Servlet的生命周期、容器如何找到指定Servlet的过程,以及如何处理中文乱码等问题。

420

被折叠的 条评论
为什么被折叠?



