从servlet规范说起
1 servlet 3.1规范
1.1 What is servlet
A servlet is a JavaTM technology-based Web component, managed by a container, that generates dynamic content. 
From servlet 3.1
1.2 History
From wiki
1.3 Servlet Life Cycle
- 
Loading and Instantiation
When the servlet engine is started, needed servlet classes must be located by the servlet container(WEB-INF/lib)
 - 
Initialization
The container initializes the servlet instance by calling the init method of the Servlet interface with a unique (per servlet declaration) object implementing the ServletConfig interface 
(ServletConfig used by Servlet Container)
- 
Request Handling
After a servlet is properly initialized, the servlet container may use it to handle client requests.
 - 
End of Service
 
1.4 Servlet 继承结构
1.5 ServletContext
    The ServletContext interface defines a servlet’s view of the Web application within which the servlet is running.  (web.xml) The Container Provider is responsible for providing an implementation of the ServletContext interface in the servlet container.
InitParameter
config 
    -Filter
    -Listenr
    -Servlet
Attribute
Resource
...
see : ApplicationContext、ApplicationContextFacade (tomcat)
1.6 Request
HttpServletRequest
- 
HTTP Protocol Parameters
■ getParameter
■ getParameterNames
■ getParameterValues
■ getParameterMap
 - 
File upload
content-type : multipart/form-data
 - 
Attributes
 - 
Headers
 - 
Request Path Elements
requestURI = contextPath + servletPath + pathInfo
 - 
Path Translation Methods
■ ServletContext.getRealPath
■ HttpServletRequest.getPathTranslated
 - 
Non Blocking IO
Non-blocking IO only works with async request processing in Servlets and Filters
 - 
Cookies
 - 
SSL
 - 
Internationalization
Accept-Language : zh-cn
 - 
■ getLocale ■ getLocales
 - 
Request data encoding
The default encoding of a request the container uses to create the request reader and parse POST data must be “ISO-8859-1” if none has been specified by the client request.
 - 
Lifetime of the Request Object
Each request object is valid only within the scope of a servlet’s service method, or within the scope of a filter’s doFilter method, unless the asynchronous processing is enabled for the component and the startAsync method is invoked on the request object.
 
1.7 Response
1.8 Filter
what is Filter?
A filter is a reusable piece of code that can transform the content of HTTP requests,
responses, and header information.
org.springframework.web.servlet.HandlerInterceptor
1.9 Lifecycle Events
Event
    -Servlet
    -Session
    -Request
EventListener
    -Servlet
    -Session
    -Request
Example : ServletContextListener
public class ContextLoaderListener implements ServletContextListener {
    private ContextLoader contextLoader;
    public ContextLoaderListener() {
    }
    public void contextInitialized(ServletContextEvent event) {
        this.contextLoader = this.createContextLoader();
        this.contextLoader.initWebApplicationContext(event.getServletContext());
    }
    protected ContextLoader createContextLoader() {
        return new ContextLoader();
    }
    public ContextLoader getContextLoader() {
        return this.contextLoader;
    }
    public void contextDestroyed(ServletContextEvent event) {
        if(this.contextLoader != null) {
            this.contextLoader.closeWebApplicationContext(event.getServletContext());
        }
    }
}
1.10 Session
Server + Applet 的缩写,表示一个服务器应用
2.1 Servlet接口
package javax.servlet;
import java.io.IOException;
public interface Servlet {
    public void init(ServletConfig config) throws ServletException;
    
    public ServletConfig getServletConfig();
    
    public void service(ServletRequest req, ServletResponse res)
   throws ServletException, IOException;
    public String getServletInfo();
    
    public void destroy();
    
}
Load-on-startup 为负的话不会在容器启动调用
2.2 ServletConfig接口
package javax.servlet;
import java.util.Enumeration;
/**
 * A servlet configuration object used by a servlet container
 * to pass information to a servlet during initialization. 
 */
 public interface ServletConfig {
    
    public String getServletName();
    public ServletContext getServletContext();
    public String getInitParameter(String name);
    public Enumeration<String> getInitParameterNames();
}
如下配置
<!--web.xml-->
在Servlet中 可以分别通过它们的getInitParameter方法获取,比如:
String contextLocation = getServletConfig().getServletContext().getInitParameter(
    "contextConfigLocation");
String servletLocation = getServletConfig().getInitParameter("contextConfigLocation");
2.3 GenerieServlet
`Servlet`的默认实现,同时实现了`ServletConfig`接口、`Serializable`接口,所以可以直接调用`ServletConfig`里面的方法。详细可参考如下类注释。
package javax.servlet;
import java.io.IOException;
import java.util.Enumeration;
/**
 *
 * Defines a generic, protocol-independent
 * servlet. To write an HTTP servlet for use on the
 * Web, extend {@link javax.servlet.http.HttpServlet} instead.
 *
 * <p><code>GenericServlet</code> implements the <code>Servlet</code>
 * and <code>ServletConfig</code> interfaces. <code>GenericServlet</code>
 * may be directly extended by a servlet, although it's more common to extend
 * a protocol-specific subclass such as <code>HttpServlet</code>.
 *
 * <p><code>GenericServlet</code> makes writing servlets
 * easier. It provides simple versions of the lifecycle methods 
 * <code>init</code> and <code>destroy</code> and of the methods 
 * in the <code>ServletConfig</code> interface. <code>GenericServlet</code>
 * also implements the <code>log</code> method, declared in the
 * <code>ServletContext</code> interface. 
 *
 * <p>To write a generic servlet, you need only
 * override the abstract <code>service</code> method. 
 *
 */
public abstract class GenericServlet 
    implements Servlet, ServletConfig, java.io.Serializable
{
    private transient ServletConfig config;
    public GenericServlet() {}
    public void destroy() {}
    
    public String getInitParameter(String name) {
        return getServletConfig().getInitParameter(name);
    }
    
    public Enumeration getInitParameterNames() {
        return getServletConfig().getInitParameterNames();
    }   
    
    public ServletConfig getServletConfig() {
        return config;
    }
    
    public ServletContext getServletContext() {
        return getServletConfig().getServletContext();
    }
    
    public String getServletInfo() {
        return "";
    }
    public void init(ServletConfig config) throws ServletException {
        this.config = config;
        this.init();
    }
    public void init() throws ServletException {
    }
    
    public void log(String msg) {
        getServletContext().log(getServletName() + ": "+ msg);
    }
   
    public void log(String message, Throwable t) {
        getServletContext().log(getServletName() + ": " + message, t);
    }
    
    public abstract void service(ServletRequest req, ServletResponse res)
    throws ServletException, IOException;
    
    public String getServletName() {
        return config.getServletName();
    }