public final void invoke(Request request, Response response)
throws IOException, ServletException {
// Select the Host to be used for this Request
Host host = request.getHost();
if (host == null) {
response.sendError
(HttpServletResponse.SC_BAD_REQUEST,
sm.getString("standardEngine.noHost",
request.getServerName()));
return;
}
// Ask this Host to process this request
//这个是StandardHost通过管道要到下一个容器去
//Host要经过两个阀门由这里的ErrorReportValve与下一个的StandarHostValve
host.getPipeline().getFirst().invoke(request, response);
}
以下到了ErrorReportValve#invoke
/**
* Invoke the next Valve in the sequence. When the invoke returns, check
* the response state, and output an error report is necessary.
*
* @param request The servlet request to be processed
* @param response The servlet response to be created
*
* @exception IOException if an input/output error occurs
* @exception ServletException if a servlet error occurs
*/
public void invoke(Request request, Response response)
throws IOException, ServletException {
// Perform the request
//StandHostValve阀门的执行
getNext().invoke(request, response);
Throwable throwable =
(Throwable) request.getAttribute(Globals.EXCEPTION_ATTR);
if (response.isCommitted()) {
return;
}
if (throwable != null) {
// The response is an error
response.setError();
// Reset the response (if possible)
try {
response.reset();
} catch (IllegalStateException e) {
;
}
response.sendError
(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
response.setSuspended(false);
try {
report(request, response, throwable);
} catch (Throwable tt) {
;
}
}
再执行StandardHostValve#invoke
public final void invoke(Request request, Response response)
throws IOException, ServletException {
// Select the Context to be used for this Request
//容器StandardContext
Context context = request.getContext();
if (context == null) {
response.sendError
(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
sm.getString("standardHost.noContext"));
return;
}
// Bind the context CL to the current thread
if( context.getLoader() != null ) {
// Not started - it should check for availability first
// This should eventually move to Engine, it's generic.
//把WebappClassLoader设置进线程
Thread.currentThread().setContextClassLoader
(context.getLoader().getClassLoader());
}
// Ask this Context to process this request
//下一个管道StandardContextValve
context.getPipeline().getFirst().invoke(request, response);
// Access a session (if present) to update last accessed time, based on a
// strict interpretation of the specification
if (Globals.STRICT_SERVLET_COMPLIANCE) {
request.getSession(false);
}
// Error page processing
//页面错误进行暂停
response.setSuspended(false);
Throwable t = (Throwable) request.getAttribute(Globals.EXCEPTION_ATTR);
if (t != null) {
throwable(request, response, t);
} else {
//
status(request, response);
}
// Restore the context classloader
Thread.currentThread().setContextClassLoader
(StandardHostValve.class.getClassLoader());
}
执行到了StandContextValve#invoke
/**
* Select the appropriate child Wrapper to process this request,
* based on the specified request URI. If no matching Wrapper can
* be found, return an appropriate HTTP error.
*
* @param request Request to be processed
* @param response Response to be produced
* @param valveContext Valve context used to forward to the next Valve
*
* @exception IOException if an input/output error occurred
* @exception ServletException if a servlet error occurred
*/
public final void invoke(Request request, Response response)
throws IOException, ServletException {
// Disallow any direct access to resources under WEB-INF or META-INF
//不允许访问以下两个目录的资源文件
MessageBytes requestPathMB = request.getRequestPathMB();
if ((requestPathMB.startsWithIgnoreCase("/META-INF/", 0))
|| (requestPathMB.equalsIgnoreCase("/META-INF"))
|| (requestPathMB.startsWithIgnoreCase("/WEB-INF/", 0))
|| (requestPathMB.equalsIgnoreCase("/WEB-INF"))) {
notFound(response);
return;
}
// Wait if we are reloading
boolean reloaded = false;
while (context.getPaused()) {
reloaded = true;
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
;
}
}
// Reloading will have stopped the old webappclassloader and
// created a new one
if (reloaded &&
context.getLoader() != null &&
context.getLoader().getClassLoader() != null) {
Thread.currentThread().setContextClassLoader(
context.getLoader().getClassLoader());
}
// Select the Wrapper to be used for this Request
//获取StandardWrapper,这个不允许有子容器
Wrapper wrapper = request.getWrapper();
if (wrapper == null) {
notFound(response);
return;
} else if (wrapper.isUnavailable()) {
// May be as a result of a reload, try and find the new wrapper
wrapper = (Wrapper) container.findChild(wrapper.getName());
if (wrapper == null) {
notFound(response);
return;
}
}
// Normal request processing
Object instances[] = context.getApplicationEventListeners();
ServletRequestEvent event = null;
if ((instances != null)
&& (instances.length > 0)) {
event = new ServletRequestEvent
(((StandardContext) container).getServletContext(),
request.getRequest());
// create pre-service event
for (int i = 0; i < instances.length; i++) {
if (instances == null)
continue;
if (!(instances instanceof ServletRequestListener))
continue;
ServletRequestListener listener =
(ServletRequestListener) instances;
try {
listener.requestInitialized(event);
} catch (Throwable t) {
container.getLogger().error(sm.getString("standardContext.requestListener.requestInit",
instances.getClass().getName()), t);
ServletRequest sreq = request.getRequest();
sreq.setAttribute(Globals.EXCEPTION_ATTR,t);
return;
}
}
}
//StandardWrapperValve的执行,这下对servlet,filter,listener进行了处理
wrapper.getPipeline().getFirst().invoke(request, response);
if ((instances !=null ) &&
(instances.length > 0)) {
// create post-service event
for (int i = 0; i < instances.length; i++) {
if (instances == null)
continue;
if (!(instances instanceof ServletRequestListener))
continue;
ServletRequestListener listener =
(ServletRequestListener) instances;
try {
listener.requestDestroyed(event);
} catch (Throwable t) {
container.getLogger().error(sm.getString("standardContext.requestListener.requestDestroy",
instances.getClass().getName()), t);
ServletRequest sreq = request.getRequest();
sreq.setAttribute(Globals.EXCEPTION_ATTR,t);
}
}
}
}