@Override
public void dispatch() {
check();//检查request是否为空,如果为空,抛出异常IllegalStateException
String path;
String pathInfo;
ServletRequest servletRequest = getRequest();
if (servletRequest instanceof HttpServletRequest) {
HttpServletRequest sr = (HttpServletRequest) servletRequest;
path = sr.getServletPath();
pathInfo = sr.getPathInfo();
} else {
path = request.getServletPath();
pathInfo = request.getPathInfo();
}
if (pathInfo != null) {
path += pathInfo;
}
dispatch(path);
}
@Override
public void dispatch(String path) {
check();
dispatch(request.getServletContext(),path);
}
@Override
public void dispatch(ServletContext context, String path) {
if (log.isDebugEnabled()) {
logDebug("dispatch ");
}
check();
if (dispatch != null) {//dispatch是一个runnable接口,初始为空,如果不为空抛出异常,dispatch已经started
throw new IllegalStateException(
sm.getString("asyncContextImpl.dispatchingStarted"));
}
if (request.getAttribute(ASYNC_REQUEST_URI)==null) {
request.setAttribute(ASYNC_REQUEST_URI, request.getRequestURI());
request.setAttribute(ASYNC_CONTEXT_PATH, request.getContextPath());
request.setAttribute(ASYNC_SERVLET_PATH, request.getServletPath());
request.setAttribute(ASYNC_PATH_INFO, request.getPathInfo());
request.setAttribute(ASYNC_QUERY_STRING, request.getQueryString());//设置request的属性
}
//<span style="font-family: Arial, Helvetica, sans-serif;"></span>/**
* Returns a RequestDispatcher object that acts as a wrapper for the
* resource located at the given path. A <code>RequestDispatcher</code>
* object can be used to forward a request to the resource or to include the
* resource in a response. The resource can be dynamic or static.
* <p>
* The pathname must begin with a "/" and is interpreted as relative to the
* current context root. Use <code>getContext</code> to obtain a
* <code>RequestDispatcher</code> for resources in foreign contexts. This
* method returns <code>null</code> if the <code>ServletContext</code>
/// * cannot return a <code>RequestDispatcher</code>.
final RequestDispatcher requestDispatcher = context.getRequestDispatcher(path);
if (!(requestDispatcher instanceof AsyncDispatcher)) {
throw new UnsupportedOperationException(
sm.getString("asyncContextImpl.noAsyncDispatcher"));
}
final AsyncDispatcher applicationDispatcher =
(AsyncDispatcher) requestDispatcher;
final ServletRequest servletRequest = getRequest();
final ServletResponse servletResponse = getResponse();
Runnable run = new Runnable() {
@Override
public void run() {
request.getCoyoteRequest().action(ActionCode.ASYNC_DISPATCHED, null);
try {
applicationDispatcher.dispatch(servletRequest, servletResponse);
}catch (Exception x) {
//log.error("Async.dispatch",x);
throw new RuntimeException(x);
}
}
};
this.dispatch = run;
this.request.getCoyoteRequest().action(ActionCode.ASYNC_DISPATCH, null);
clearServletRequestResponse();
}