-
GenericServlet
- a generic, protocol-independent servlet.
-
HttpServlet
- An HTTP servlet for use on the Web
-
A typical sequence of events
- 1. 终端发送请求.
- 2. 请求被Web Server接收并交由Servelt Container处理.
- 3. Servelt Container调用相关的Servlet处理.
- 4. Serlvet使用Request对象确认终端 处理业务逻辑 生成数据使用Respose对象发送到终端.
- 5. 一旦Servlet完成 Servlet Container确保Response被刷新(flushed) 然后将控制权交由Web Server.
-
Request Handling Methods
- doGet
- doPost
- doPut HTTP/1.1
- doDelete HTTP/1.1
- doHead It returns only the headers produced by the doGet method
- doOptions
- doTrace
- For a servlet not hosted in a distributed environment (the default), the servlet container must use only one instance per servlet declaration.
-
Life Cycle
- Loading and Instantiation
- Initialization
-
Request Handling
-
Multithreading Issues
- SingleThreadModel
- synchronize the service method is evil
- the request and response objects are not thread safe
- Exceptions During Request Handling
-
Thread Safety
- Be aware that the request and response objects are not thread safe.
- End of Service
-
Request
- HTTP Protocol Parameters
- Attributes
- Headers
-
Request Path Elements
- Context Path
- Servlet Path
- PathInfo
- requestURI = contextPath + servletPath + pathInfo
- Cookies
- SSL Attributes
- Internationalization
-
Request data encoding
- parse POST data must be “ISO-8859-1” if none has been specified.
-
Lifetime
- Each request object is valid only within the scope of a servlet’s service method.
- Maintaining references to request objects outside the scope is not recommended .
-
Servlet Context
- The Container Provider is responsible for providing an implementation of the ServletContext interface in the servlet container.
-
Scope
- There is one instance object of the ServletContext interface associated with each Web application deployed into a container.
- In cases where the container is distributed over many virtual machines, a Web application will have an instance of the ServletContext for each JVM.
- Initialization Parameters
-
Context Attributes
- Context attributes are local to the JVM in which they were created.
- ServletContext attributes can not share in a distributed container.
- Resources
-
Multiple Hosts and Servlet Contexts
- Servlet contexts can not be shared across virtual hosts.
-
Response
- Buffering
- Headers
- Internationalization
-
Closure of Response Object
- When a response is closed, the container must immediately flush all remaining content in the response buffer to the client.
- Lifetime
-
Filtering
- javax.servlet.Filter
- Filters and the RequestDispatcher
-
Sessions
- The Hypertext Transfer Protocol (HTTP) is by design a stateless protocol.
-
Session Tracking Mechanisms
- Cookies
- SSL(Secure Sockets Layer) Sessions
- URL Rewriting: http://localhost/app/index.html;jsessionid=1234 (must be jsessionid)
-
Scope
- session的作用范围仅限于当前应用程序下, 不同的应用之间是不能够互相访问
- Session Timeouts
- Last Accessed Times
-
Important Session Semantics
- Threading Issues
- Distributed Environments
- Client Semantics
-
Dispatching Requests
-
The Include Method
- Any attempt to set the headers with the response object must be ignored.
-
The Forward Method
- The method may be called by the calling servlet only when no output has been committed
- Error Handling