DispatcherServlet
μ Servlet
μΈν°νμ΄μ€λ₯Ό ꡬνν, HTTP νλ‘ν μ½λ‘ λ€μ΄μ€λ λͺ¨λ μμ²μ λ¨Όμ λ°μ μ²λ¦¬νλ Front Controller μ΄λ€.
Servlet μ΄λ?
Java μΈμ΄λ₯Ό κΈ°λ°μΌλ‘, ν΄λΌμ΄μΈνΈκ° 보λ΄λ HTTP μμ²μ μ²λ¦¬νκ³ μλ΅νλ μν μ λ΄λΉνλ μΈν°νμ΄μ€ μ΄λ€.
public interface Servlet {
void init(ServletConfig config) throws ServletException;
ServletConfig getServletConfig();
void service(ServletRequest req, ServletResponse res) throws ServletException, IOException;
String getServletInfo();
void destroy();
}
μμ κ°μ λ©μλλ€μ΄ μ μλμ΄ μλ€.
μ€μν λΆλΆμ service(ServletRequest req, ServletResponse res)
μΈλ°, ν΄λΌμ΄μΈνΈμ μμ²μ λ°μ μ²λ¦¬νκ³ μλ΅μ μμ±νλ ν΅μ¬ λ©μλμ΄λ€.
DispatcherServlet
μ μ΄ Servlet
μΈν°νμ΄μ€μ ꡬν체μ΄κ³ , κ²°κ³Όμ μΌλ‘ service()
λ©μλλ₯Ό ꡬνν νΉμ λ©μλλ₯Ό μ€νμμΌ ν΄λΌμ΄μΈνΈμ μμ²μ μ²λ¦¬νλ κ²μ΄λ€.
μμ κ°μ μμ ꡬ쑰λ₯Ό κ°κ³ μκ³ , DispatcherServlet
μ΄ μ΄λ»κ² μ¬μ©μμ μλ΅μ μ²λ¦¬νλμ§ μ΄ν΄λ³΄μ.
HttpServlet | service()
service()
λ ν΄λΌμ΄μΈνΈμ μμ²μ μ²λ¦¬νλ λ©μλλΌκ³ νλ€.
Servlet
ν΄λμ€λ₯Ό μμνλ ν΄λμ€ μ€, HttpServlet
μ΄ ν΄λΉ λ©μλλ₯Ό Override νλ€.
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
HttpServletRequest request;
HttpServletResponse response;
try {
request = (HttpServletRequest) req;
response = (HttpServletResponse) res;
} catch (ClassCastException e) {
throw new ServletException(lStrings.getString("http.non_http"));
}
service(request, response);
}
μν μ, ServletRequest
λ₯Ό HttpServletRequest
λ‘ λ³ννκ³ ServletResponse
λ₯Ό HttpServletResponse
λ‘ λ³νν λ€,
service(HttpServletRequest req, HttpServletResponse res)
λ‘μ service()
λ©μλλ₯Ό Overload(Overrideβ) ν λ©μλλ₯Ό νΈμΆνλ€.
μ΄ Overload ν λ©μλλ νμ ν΄λμ€μΈ FrameWorkServlet
ν΄λμ€μμ μ°Ύμ μ μλ€.
FrameWorkServlet | service()
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
if (HTTP_SERVLET_METHODS.contains(request.getMethod())) {
super.service(request, response);
}
else {
processRequest(request, response);
}
}
// private static final Set<String> HTTP_SERVLET_METHODS =
// Set.of("DELETE", "HEAD", "GET", "OPTIONS", "POST", "PUT", "TRACE");
μ°λ¦¬κ° μ¬μ©νλ λλΆλΆμ HTTP μμ²μ GET, POST, PUT, DELETE μμ²μ΄λ―λ‘
μ λ‘μ§μ λ°λΌ, super.service(request,response)
κ° νΈμΆλμ΄ FrameworkServlet
μ μμ ν΄λμ€μ service()
λ©μλκ° νΈμΆλ κ²μ΄λ€.
λ€μ HttpServlet
ν΄λμ€μ service(HttpServletRequest req, HttpServletResponse res)
λ©μλκ° Overload λμ΄ κ΅¬νλμ΄μμΌλ―λ‘ λμμλ€.
HttpServlet | service()
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) {
doGet(req, resp);
} else {
long ifModifiedSince;
try {
ifModifiedSince = req.getDateHeader(HEADER_IFMODSINCE);
} catch (IllegalArgumentException iae) {
ifModifiedSince = -1;
}
if (ifModifiedSince < (lastModified / 1000 * 1000)) {
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 {
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);
}
}
μμ κ°μ λ©μλλ‘ κ΅¬νλμ΄ μλ€.
κ° HTTP λ©μλμ λ§κ² doXX λ©μλλ₯Ό νΈμΆνκ²λ λ‘μ§μ΄ μ§νλλ€.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String msg = lStrings.getString("http.method_get_not_supported");
sendMethodNotAllowed(req, resp, msg);
}
μ λ©μλλ₯Ό λ°κ²¬ν μ μμλλ°, protected
ν€μλλ₯Ό 보λ νμ ν΄λμ€μμ μλ‘κ² μ μν λ©μλκ° μκ² κ΅¬λ μκ°μ΄ λ€μ΄μ νμ ν΄λμ€λ₯Ό νμν΄λ³΄μλ€.
HttpServlet
μ μμν FrameWorkServlet
ν΄λμ€μμ Override ν doXX λ©μλκ° μμμ νμΈνλ€.
FrameWorkServlet | doGet()
@Override
protected final void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
μμ κ°μ λ©μλλ‘ μ μλμ΄ μλλ°, processRequest(request, response)
λΌλ λ©μλλ₯Ό νΈμΆνκ²λ λμ΄μλ€.
FrameWorkServlet | processRequest()
protected final void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
long startTime = System.currentTimeMillis();
Throwable failureCause = null;
LocaleContext previousLocaleContext = LocaleContextHolder.getLocaleContext();
LocaleContext localeContext = buildLocaleContext(request);
RequestAttributes previousAttributes = RequestContextHolder.getRequestAttributes();
ServletRequestAttributes requestAttributes = buildRequestAttributes(request, response, previousAttributes);
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
asyncManager.registerCallableInterceptor(FrameworkServlet.class.getName(), new RequestBindingInterceptor());
initContextHolders(request, localeContext, requestAttributes);
try {
doService(request, response);
}
catch (ServletException | IOException ex) {
failureCause = ex;
throw ex;
}
catch (Throwable ex) {
failureCause = ex;
throw new ServletException("Request processing failed: " + ex, ex);
}
finally {
resetContextHolders(request, previousLocaleContext, previousAttributes);
if (requestAttributes != null) {
requestAttributes.requestCompleted();
}
logResult(request, response, failureCause, asyncManager);
publishRequestHandledEvent(request, response, startTime, failureCause);
}
}
μ΄λ¬ν λ©μλκ° μμλ€.
κ²°λ‘ μ doService(request, response)
λ₯Ό νΈμΆνλ€.
protected abstract void doService(HttpServletRequest request, HttpServletResponse response)
throws Exception;
κ·Όλ°, doService()
λ©μλλ μΆμ λ©μλμλ€.
μ¦, μΆμ λ©μλλ‘ μν μ μ μν΄λκ³ νμ ν΄λμ€μμ ꡬνν λ©μλλ₯Ό μ¬μ©νκ²λ νλ ν νλ¦Ώ λ©μλ ν¨ν΄μ΄ μ μ©λμ΄ μμλ€.
κ²°κ³Όμ μΌλ‘ νμ ν΄λμ€μΈ DispatcherServlet
μμ ꡬνν doService(HttpServletRequest request, HttpServletResponse response)
κ° νΈμΆλλ€.
DispatcherServlet | doService()
@Override
protected void doService(HttpServletRequest request, HttpServletResponse response) throws Exception {
.
.
.
try {
doDispatch(request, response);
}
finally {
if (!WebAsyncUtils.getAsyncManager(request).isConcurrentHandlingStarted()) {
// Restore the original attribute snapshot, in case of an include.
if (attributesSnapshot != null) {
restoreAttributesAfterInclude(request, attributesSnapshot);
}
}
if (this.parseRequestPath) {
ServletRequestPathUtils.setParsedRequestPath(previousRequestPath, request);
}
}
}
DispatcherServlet
μ doService()
λ©μλλ λ€μ, doDispatch()
λ©μλλ₯Ό νΈμΆνλ€.
DispatcherServlet | doDispatch()
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletRequest processedRequest = request;
HandlerExecutionChain mappedHandler = null;
boolean multipartRequestParsed = false;
WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);
try {
ModelAndView mv = null;
Exception dispatchException = null;
try {
processedRequest = checkMultipart(request);
multipartRequestParsed = (processedRequest != request);
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest);
if (mappedHandler == null) {
noHandlerFound(processedRequest, response);
return;
}
// Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
// Process last-modified header, if supported by the handler.
String method = request.getMethod();
boolean isGet = HttpMethod.GET.matches(method);
if (isGet || HttpMethod.HEAD.matches(method)) {
long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
return;
}
}
if (!mappedHandler.applyPreHandle(processedRequest, response)) {
return;
}
// Actually invoke the handler.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
catch (Exception ex) {
dispatchException = ex;
}
catch (Throwable err) {
// As of 4.3, we're processing Errors thrown from handler methods as well,
// making them available for @ExceptionHandler methods and other scenarios.
dispatchException = new ServletException("Handler dispatch failed: " + err, err);
}
processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
}
catch (Exception ex) {
triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
}
catch (Throwable err) {
triggerAfterCompletion(processedRequest, response, mappedHandler,
new ServletException("Handler processing failed: " + err, err));
}
finally {
if (asyncManager.isConcurrentHandlingStarted()) {
// Instead of postHandle and afterCompletion
if (mappedHandler != null) {
mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
}
}
else {
// Clean up any resources used by a multipart request.
if (multipartRequestParsed) {
cleanupMultipart(processedRequest);
}
}
}
}
doDispatch()
λ©μλλ μμ κ°λ€.
mappedHandler = getHandler(processedRequest)
λΆλΆμμ μμ² urlμ κΈ°λ°μΌλ‘ μ μ ν νΈλ€λ¬(Controller)λ₯Ό μ°Ύλλ€.
κ·Έλ¦¬κ³ HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler())
λΆλΆμμ μ°Ύμ νΈλ€λ¬λ₯Ό μ€ννκΈ° μν νΈλ€λ¬ Adapterλ₯Ό μ°Ύλλ€.
mv = ha.handle(processedRequest, response, mappedHandler.getHandler())
λΆλΆμμ νΈλ€λ¬ μ΄λν°λ₯Ό μ€ννμ¬ μμ²μ μ²λ¦¬ν μ μλ Controller λ‘μ§μ μ€ννκ² λλ€.
μ°Έκ³ ν λΈλ‘κ·Έ : https://zzang9ha.tistory.com/441