日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tomcat如何将请求分发到servlet

發(fā)布時(shí)間:2024/9/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tomcat如何将请求分发到servlet 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Tomcat對(duì)一個(gè)請(qǐng)求進(jìn)行解析時(shí),首先會(huì)進(jìn)行最首先的封裝,將請(qǐng)求封裝成Request對(duì)象。
之后在一個(gè)類StandardWrapperValve中執(zhí)行invoke方法。
@Overridepublic final void invoke(Request request, Response response)throws IOException, ServletException {// Initialize local variables we may needboolean unavailable = false;Throwable throwable = null;// This should be a Request attribute...long t1=System.currentTimeMillis();requestCount.incrementAndGet();StandardWrapper wrapper = (StandardWrapper) getContainer();Servlet servlet = null;Context context = (Context) wrapper.getParent();// Check for the application being marked unavailableif (!context.getState().isAvailable()) {response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,sm.getString("standardContext.isUnavailable"));unavailable = true;}// Check for the servlet being marked unavailableif (!unavailable && wrapper.isUnavailable()) {container.getLogger().info(sm.getString("standardWrapper.isUnavailable",wrapper.getName()));long available = wrapper.getAvailable();if ((available > 0L) && (available < Long.MAX_VALUE)) {response.setDateHeader("Retry-After", available);response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,sm.getString("standardWrapper.isUnavailable",wrapper.getName()));} else if (available == Long.MAX_VALUE) {response.sendError(HttpServletResponse.SC_NOT_FOUND,sm.getString("standardWrapper.notFound",wrapper.getName()));}unavailable = true;}// Allocate a servlet instance to process this requesttry {if (!unavailable) {servlet = wrapper.allocate(); //在可用情況下分配servlet,可用看看 unavailable變量的取值}} catch (UnavailableException e) {container.getLogger().error(sm.getString("standardWrapper.allocateException",wrapper.getName()), e);long available = wrapper.getAvailable();if ((available > 0L) && (available < Long.MAX_VALUE)) {response.setDateHeader("Retry-After", available);response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE,sm.getString("standardWrapper.isUnavailable",wrapper.getName()));} else if (available == Long.MAX_VALUE) {response.sendError(HttpServletResponse.SC_NOT_FOUND,sm.getString("standardWrapper.notFound",wrapper.getName()));}} catch (ServletException e) {container.getLogger().error(sm.getString("standardWrapper.allocateException",wrapper.getName()), StandardWrapper.getRootCause(e));throwable = e;exception(request, response, e);} catch (Throwable e) {ExceptionUtils.handleThrowable(e);container.getLogger().error(sm.getString("standardWrapper.allocateException",wrapper.getName()), e);throwable = e;exception(request, response, e);servlet = null;}MessageBytes requestPathMB = request.getRequestPathMB();DispatcherType dispatcherType = DispatcherType.REQUEST;if (request.getDispatcherType()==DispatcherType.ASYNC) dispatcherType = DispatcherType.ASYNC;request.setAttribute(Globals.DISPATCHER_TYPE_ATTR,dispatcherType);request.setAttribute(Globals.DISPATCHER_REQUEST_PATH_ATTR,requestPathMB);// Create the filter chain for this requestApplicationFilterChain filterChain =ApplicationFilterFactory.createFilterChain(request, wrapper, servlet); //重點(diǎn),創(chuàng)建filterChain,根據(jù)代碼實(shí)現(xiàn)的Filter// Call the filter chain for this request// NOTE: This also calls the servlet's service() methodtry {if ((servlet != null) && (filterChain != null)) {// Swallow output if neededif (context.getSwallowOutput()) { //swallowOutput 是Tomcat的Context的屬性,用于設(shè)置System.out是否輸出到日志,默認(rèn)falsetry {SystemLogHandler.startCapture();if (request.isAsyncDispatching()) {request.getAsyncContextInternal().doInternalDispatch();} else {filterChain.doFilter(request.getRequest(),response.getResponse()); //重點(diǎn),servlet和filter生效的地方}} finally {String log = SystemLogHandler.stopCapture();if (log != null && log.length() > 0) {context.getLogger().info(log);}}} else {if (request.isAsyncDispatching()) {request.getAsyncContextInternal().doInternalDispatch();} else {filterChain.doFilter(request.getRequest(), response.getResponse());//重點(diǎn),servlet和filter生效的地方}}}} catch (ClientAbortException e) { }........捕獲異常+后續(xù)內(nèi)存釋放等

line 43: ?//在可用情況下分配servlet,可用看看 unavailable變量的取值

line 82: ? //重點(diǎn),創(chuàng)建filterChain,根據(jù)代碼實(shí)現(xiàn)的Filter ?

line 89: ?swallowOutput 是Tomcat的Context的屬性,用于設(shè)置System.out是否輸出到日志,默認(rèn)false ?

line 96 97 110//重點(diǎn),servlet和filter生效的地方?

繼續(xù)跟進(jìn),看看filterChain.doFilter (request.getRequest(), response.getResponse());

@Overridepublic void doFilter(ServletRequest request, ServletResponse response)throws IOException, ServletException {if( Globals.IS_SECURITY_ENABLED ) {final ServletRequest req = request;final ServletResponse res = response;try {java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction<Void>() {@Overridepublic Void run()throws ServletException, IOException {internalDoFilter(req,res);//重點(diǎn)方法return null;}});} catch( PrivilegedActionException pe) {Exception e = pe.getException();if (e instanceof ServletException)throw (ServletException) e;else if (e instanceof IOException)throw (IOException) e;else if (e instanceof RuntimeException)throw (RuntimeException) e;elsethrow new ServletException(e.getMessage(), e);}} else {internalDoFilter(request,response);//重點(diǎn)方法}}private void internalDoFilter(ServletRequest request,ServletResponse response)throws IOException, ServletException {// Call the next filter if there is oneif (pos < n) {//在 沒有循環(huán)完filter時(shí)循環(huán)調(diào)用doFilter方法,一個(gè)責(zé)任鏈模式的經(jīng)典實(shí)現(xiàn) 循環(huán)完跳出這個(gè)if方法塊 ApplicationFilterConfig filterConfig = filters[pos++];//獲取filterList由上面的createFilterChain創(chuàng)建try {Filter filter = filterConfig.getFilter();if (request.isAsyncSupported() && "false".equalsIgnoreCase(filterConfig.getFilterDef().getAsyncSupported())) {request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR, Boolean.FALSE);}if( Globals.IS_SECURITY_ENABLED ) {final ServletRequest req = request;final ServletResponse res = response;Principal principal =((HttpServletRequest) req).getUserPrincipal();Object[] args = new Object[]{req, res, this};SecurityUtil.doAsPrivilege ("doFilter", filter, classType, args, principal);} else {filter.doFilter(request, response, this);}} catch (IOException | ServletException | RuntimeException e) {throw e;} catch (Throwable e) {e = ExceptionUtils.unwrapInvocationTargetException(e);ExceptionUtils.handleThrowable(e);throw new ServletException(sm.getString("filterChain.filter"), e);}return;}// We fell off the end of the chain -- call the servlet instancetry {if (ApplicationDispatcher.WRAP_SAME_OBJECT) {lastServicedRequest.set(request);lastServicedResponse.set(response);}if (request.isAsyncSupported() && !servletSupportsAsync) {request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,Boolean.FALSE);}// Use potentially wrapped request from this pointif ((request instanceof HttpServletRequest) &&(response instanceof HttpServletResponse) &&Globals.IS_SECURITY_ENABLED ) {final ServletRequest req = request;final ServletResponse res = response;Principal principal =((HttpServletRequest) req).getUserPrincipal();Object[] args = new Object[]{req, res};SecurityUtil.doAsPrivilege("service",servlet,classTypeUsedInService,args,principal);} else {servlet.service(request, response); //跳出循環(huán)后執(zhí)行此方法,進(jìn)入servlet的重點(diǎn)方法,對(duì)應(yīng)到spring就是DispatcherServlet }} catch (IOException | ServletException | RuntimeException e) {throw e;} catch (Throwable e) {e = ExceptionUtils.unwrapInvocationTargetException(e);ExceptionUtils.handleThrowable(e);throw new ServletException(sm.getString("filterChain.servlet"), e);} finally {if (ApplicationDispatcher.WRAP_SAME_OBJECT) {lastServicedRequest.set(null);lastServicedResponse.set(null);}}}l ine 14: 繼續(xù)調(diào)用
line 42,43: 責(zé)任鏈filter ?
line 98 :servlet的入口,對(duì)應(yīng)到spring就是DispatcherServlet?

總結(jié)

以上是生活随笔為你收集整理的tomcat如何将请求分发到servlet的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 亚洲综合少妇 | 久久久久久999| 日韩福利片在线观看 | 中文有码在线播放 | 青青在线视频观看 | 日本特黄视频 | 欧美一级在线视频 | 后宫秀女调教(高h,np) | 日韩电影观看 | 国产成人精品免高潮在线观看 | 久久久久国产精品区片区无码 | 欧美日韩视频在线观看免费 | 亚洲欧美日韩激情 | jizz日本免费 | 日韩精品播放 | 黄色视屏免费 | 欧美色图视频在线 | 成人性毛片 | 午夜色网 | 麻豆极品| 欧美精品在线免费 | 国产一级特黄a高潮片 | 久久99精品久久久久久噜噜 | 综合色88| 草莓视频一区二区三区 | 免费看欧美一级片 | 国产一级片网站 | 中文字幕在线看片 | 91在线精品播放 | 色干干 | 国产欧美一区二区三区白浆喷水 | 欧洲亚洲另类 | 午夜在线观看影院 | 午夜三级在线 | av久热 | www.射| 农村脱精光一级 | 无码人妻一区二区三区精品视频 | 精品国产一区二区三区av性色 | 久草成人在线 | 91在线视频免费看 | 久久福利在线 | 秋霞成人午夜伦在线观看 | 日本一二三不卡视频 | 国产福利在线视频观看 | 国产亚洲精品久久久久久青梅 | 国产精品一区二区三区免费在线观看 | 亚洲一片 | 亚洲天堂2015 | 一区二区 中文字幕 | 国产精品人人做人人爽人人添 | 最近中文字幕在线观看 | 自拍偷拍视频网站 | 日本国产一区二区 | 一区二区三区91 | 欧美性受xxxx黑人 | 久久精品视频播放 | 欧美在线视频免费观看 | 欧美在线观看一区二区三区 | 国产精品乱码久久久久久 | 日韩在线一级 | 麻豆亚洲av成人无码久久精品 | 香蕉911 | 河北彩花中文字幕 | 香蕉视频在线观看网站 | 4438x五月天| 成人精品区 | 日韩一区在线看 | 粉嫩av一区二区三区天美传媒 | 少妇精品偷拍高潮白浆 | 国产无限制自拍 | 国产理论片 | 中国白嫩丰满人妻videos | 亚洲第三区 | 丝袜视频在线观看 | 日韩a√| www国产免费 | 国产精品亚洲自拍 | 亚洲国产精品一区二区尤物区 | 黄色片日韩 | 不卡av免费| 饥渴放荡受np公车奶牛 | 国产精品丝袜视频无码一区69 | 成人在线影视 | 日韩少妇| 丰满放荡岳乱妇91ww | 免费在线看视频 | 黄色小说视频网站 | 国产成人在线一区 | 久99久视频 | 先锋影音av中文字幕 | 中国超碰| 国产日韩欧美激情 | 99久久久无码国产精品性色戒 | 婷婷视频网 | 好吊视频一区二区 | 99re在线视频播放 | 日本午夜电影 | 一级全黄裸体免费观看视频 |