Explain Servlet in detail
Servlet (interface)
- The init method is called by the container when the container starts, and the ServletConfig is passed in by the container
- service is used to specifically handle a request
- destroy is used for servlet destruction
GenericServlet (abstract class)
- Implement the ServletConfig interface, you can directly call the methods in ServletConfig
- Provides a parameterless init method
- Provides the log method
HttpServlet (abstract class)
Generally, when writing servlets, you can directly inherit the HttpServlet class. The
container calls the service method of HttpSerlvet, and converts ServletRequest and ServletResponse into HttpServletRequest and HttpServletResponse
@Override
public void service(ServletRequest req, ServletResponse res)
throws ServletException, IOException
{
HttpServletRequest request;
HttpServletResponse response;
if (!(req instanceof HttpServletRequest &&
res instanceof HttpServletResponse)) {
throw new ServletException("non-HTTP request or response");
}
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
service(request, response);
}
Then according to the request method of HttpServletRequest, call the corresponding method
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
String method = req.getMethod();
if (method.equals(METHOD_GET)) {
long lastModified = getLastModified(req);
if (lastModified == -1) {
// servlet doesn't support if-modified-since, no reason
// to go through further expensive logic
doGet(req, resp);
} else {
long ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
if (ifModifiedSince < lastModified) {
// If the servlet mod time is later, call doGet()
// Round down to the nearest second for a proper compare
// A ifModifiedSince of -1 will always be less
maybeSetLastModified(resp, lastModified);
doGet(req, resp);
} else {
resp.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
}
}
} else if (method.equals(METHOD_HEAD)) {
long lastModified = getLastModified(req);
maybeSetLastModified(resp, lastModified);
doHead(req, resp);
} else if (method.equals(METHOD_POST)) {
doPost(req, resp);
} else if (method.equals(METHOD_PUT)) {
doPut(req, resp);
} else if (method.equals(METHOD_DELETE)) {
doDelete(req, resp);
} else if (method.equals(METHOD_OPTIONS)) {
doOptions(req,resp);
} else if (method.equals(METHOD_TRACE)) {
doTrace(req,resp);
} else {
//
// Note that this means NO servlet supports whatever
// method was requested, anywhere on this server.
//
String errMsg = lStrings.getString("http.method_not_implemented");
Object[] errArgs = new Object[1];
errArgs[0] = method;
errMsg = MessageFormat.format(errMsg, errArgs);
resp.sendError(HttpServletResponse.SC_NOT_IMPLEMENTED, errMsg);
}
}
DispatcherServlet
Among them, HttpServlet and its parent class are in Java EE
public interface Aware {
}
public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}
public interface EnvironmentAware extends Aware {
void setEnvironment(Environment environment);
}
public interface EnvironmentCapable {
Environment getEnvironment();
}
The main purpose of implementing these interfaces is to obtain
the execution process of DispatcherServlet such as ApplicationContext
- Initiate a request to the DispatcherServlet (front-end controller)
- DispatcherServlet requests HandlerMapping (processor mapper) to find Handler
- HandlerMapping returns Handler to DispatcherServlet
- DispatcherServlet calls HandlerAdapter to execute Handler
- HandlerAdapter 执行 Handler
- Handler execution is complete, return ModelAndView to HandlerAdapter
- HandlerAdapter returns ModelAndView to DispatcherServlet
- DispatcherServlet requests ViewResolver for view resolution
- ViewResolver returns View to DispatcherServlet
- DispatcherServlet for view rendering
- DispatcherServlet returns result to user
Component Analysis
HandlerMapping
HandlerAdapter
HandlerExceptionResolver
ViewResolver (view resolver)
RequestToViewNameTranslator
LocalResolve
ThemeResolver
MultipartResolver
FlashMapManager
Reference blog
[1]http://blog.csdn.net/tzs_1041218129/article/category/6627771