Spring MVC source code learning

Explain Servlet in detail

Servlet (interface)

write picture description here

  1. The init method is called by the container when the container starts, and the ServletConfig is passed in by the container
  2. service is used to specifically handle a request
  3. destroy is used for servlet destruction

GenericServlet (abstract class)

  1. Implement the ServletConfig interface, you can directly call the methods in ServletConfig
  2. Provides a parameterless init method
  3. Provides the log method

HttpServlet (abstract class)

Generally, when writing servlets, you can directly inherit the HttpServlet class. The
write picture description here
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

write picture description here
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
write picture description here

  1. Initiate a request to the DispatcherServlet (front-end controller)
  2. DispatcherServlet requests HandlerMapping (processor mapper) to find Handler
  3. HandlerMapping returns Handler to DispatcherServlet
  4. DispatcherServlet calls HandlerAdapter to execute Handler
  5. HandlerAdapter 执行 Handler
  6. Handler execution is complete, return ModelAndView to HandlerAdapter
  7. HandlerAdapter returns ModelAndView to DispatcherServlet
  8. DispatcherServlet requests ViewResolver for view resolution
  9. ViewResolver returns View to DispatcherServlet
  10. DispatcherServlet for view rendering
  11. 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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325428426&siteId=291194637