当前位置: 首页 > news >正文

郑州做网站优化运营商长沙专业seo优化公司

郑州做网站优化运营商,长沙专业seo优化公司,企业宣传册设计,郑州seo推广问题 在日常开发中,假如我们访问一个Sping容器中并不存在的路径,通常会返回404的报错,具体原因是什么呢? 结论 错误的访问会调用两次DispatcherServlet:第一次调用无法找到对应路径时,会给Response设置一个…

 问题

在日常开发中,假如我们访问一个Sping容器中并不存在的路径,通常会返回404的报错,具体原因是什么呢?

结论

 错误的访问会调用两次DispatcherServlet:第一次调用无法找到对应路径时,会给Response设置一个错误状态,第二次是根据这个状态执行预先设置了error属性的DispatcherServlet。而正确的访问只会调用一次DispatcherServlet。

原理

我们知道,基于SpringMvc原理的Spring Boot项目,所有的路由请求默认都是由DispatcherServlet类来负责处理的?

如果开启断点调试会发现在第二次进入DispatcherServlet的doDispatch方法时,便直接返回了404错误:

那么问题的重点应该出现在两次DispatcherServlet的调用上,通过调试可以发现,最后一次的调用分析的意义不大,因为从它的request属性就能看出来,它预先设置了一堆的错误属性,明显就是为了返回错误,走了一遍DispatcherServlet的标准流程。

重点只有两点:

第一次执行DispatcherServlet的过程中发生了什么?

错误的请求为什么会有两次DispatcherServlet调用?

1.1 第一次执行DispatcherServlet

通过断点调试,可以看到在执行DispatcherServlet中的doDispatch方法的时候进入了HttpRequestHandlerAdapter的handle方法

public class HttpRequestHandlerAdapter implements HandlerAdapter {public HttpRequestHandlerAdapter() {}public boolean supports(Object handler) {return handler instanceof HttpRequestHandler;}@Nullablepublic ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {((HttpRequestHandler)handler).handleRequest(request, response);return null;}
......

接着又执行了ResourceHttpRequestHandler的handleRequest方法,在执行的过程中,在容器中没有找到对应的路径,所以对response设置了404错误:

也就是

response.sendError(404)

它的底层实际上是给Response类的一个私有属性errorState,设置了错误状态:

public boolean setError() {return this.errorState.compareAndSet(0, 1);}

 这样设置有什么用呢?

这就涉及到第二次执行DispatcherServlet的原因了。

1.2 错误请求为什么会执行两次DispatcherServlet

重点在StandardHostValve类的invoke方法中:

public final void invoke(Request request, Response response) throws IOException, ServletException {......try {//第一次执行DispatcherServlet的原因if (!response.isErrorReportRequired()) {//执行正常的请求context.getPipeline().getFirst().invoke(request, response);}} catch (Throwable var10) {ExceptionUtils.handleThrowable(var10);this.container.getLogger().error("Exception Processing " + request.getRequestURI(), var10);if (!response.isErrorReportRequired()) {request.setAttribute("javax.servlet.error.exception", var10);this.throwable(request, response, var10);}}response.setSuspended(false);Throwable t = (Throwable)request.getAttribute("javax.servlet.error.exception");if (context.getState().isAvailable()) {//第二次执行DispatcherServlet的原因if (response.isErrorReportRequired()) {AtomicBoolean result = new AtomicBoolean(false);response.getCoyoteResponse().action(ActionCode.IS_IO_ALLOWED, result);if (result.get()) {if (t != null) {this.throwable(request, response, t);} else {//执行错误请求this.status(request, response);}}}......

两次执行的原因是因为

response.isErrorReportRequired() 

public class Response implements HttpServletResponse {
public boolean isErrorReportRequired() {return this.getCoyoteResponse().isErrorReportRequired();}
......public final class Response {public boolean isErrorReportRequired() {return this.errorState.get() == 1;
}
.....

 也就是根据Response类的私有属性errorState来判断的。

第一次执行DispatcherServlet的时候,由于errorState的值还是初始化值0,所以可以正常执行,执行的时候找不到对应的路径资源,便执行了response.sendError(404)方法,给errorState赋值为1。

这是StandardHostValve类中,invoke执行的

 context.getPipeline().getFirst().invoke(request, response);

给errorState赋值1以后,又执行了:

this.status(request, response);

 它的执行链路是这样:

 status -> custom -> forward -> doForward -> processRequest ->doFilter。

也就是对本次请求执行了一次转发,最后重新调用了一遍

filterChain.doFilter(request, response)

的流程,走了错误调用。

在processRequest方法可以比较看的比较明确:

private void processRequest(ServletRequest request, ServletResponse response, State state) throws IOException, ServletException {DispatcherType disInt = (DispatcherType)request.getAttribute("org.apache.catalina.core.DISPATCHER_TYPE");if (disInt != null) {boolean doInvoke = true;if (this.context.getFireRequestListenersOnForwards() && !this.context.fireRequestInitEvent(request)) {doInvoke = false;}if (doInvoke) {if (disInt != DispatcherType.ERROR) {state.outerRequest.setAttribute("org.apache.catalina.core.DISPATCHER_REQUEST_PATH", this.getCombinedPath());state.outerRequest.setAttribute("org.apache.catalina.core.DISPATCHER_TYPE", DispatcherType.FORWARD);this.invoke(state.outerRequest, response, state);} else {this.invoke(state.outerRequest, response, state);}if (this.context.getFireRequestListenersOnForwards()) {this.context.fireRequestDestroyEvent(request);
......

 等到DispatcherServlet再执行完一次,便能在浏览器看到404报错了。


文章转载自:
http://dinncoacouophonia.tpps.cn
http://dinncoamphitrichous.tpps.cn
http://dinncofluerics.tpps.cn
http://dinncoaciculate.tpps.cn
http://dinncorocketeer.tpps.cn
http://dinncovictorious.tpps.cn
http://dinncofork.tpps.cn
http://dinncoraptorial.tpps.cn
http://dinncodeadwood.tpps.cn
http://dinncofey.tpps.cn
http://dinncoassouan.tpps.cn
http://dinncoreplevy.tpps.cn
http://dinncoepidemic.tpps.cn
http://dinncoolive.tpps.cn
http://dinncoarguer.tpps.cn
http://dinnconourishing.tpps.cn
http://dinncoinformidable.tpps.cn
http://dinncoincipit.tpps.cn
http://dinncodesecration.tpps.cn
http://dinncouptime.tpps.cn
http://dinncoredward.tpps.cn
http://dinncomuroran.tpps.cn
http://dinncochesty.tpps.cn
http://dinncopsychobabble.tpps.cn
http://dinncoaeroacoustics.tpps.cn
http://dinncounworking.tpps.cn
http://dinncooverstrung.tpps.cn
http://dinncodiomedes.tpps.cn
http://dinncoproduct.tpps.cn
http://dinncopatellar.tpps.cn
http://dinncovouchsafement.tpps.cn
http://dinnconapped.tpps.cn
http://dinncodysmetria.tpps.cn
http://dinncocounterreconnaissance.tpps.cn
http://dinncoreanimate.tpps.cn
http://dinncodalian.tpps.cn
http://dinncosubternatural.tpps.cn
http://dinncodareful.tpps.cn
http://dinncoslain.tpps.cn
http://dinncoalgometrical.tpps.cn
http://dinncoemblemize.tpps.cn
http://dinncophotoactivate.tpps.cn
http://dinncoantialcoholism.tpps.cn
http://dinncoprecedable.tpps.cn
http://dinncounchain.tpps.cn
http://dinncobeanery.tpps.cn
http://dinncoporbeagle.tpps.cn
http://dinncoextremism.tpps.cn
http://dinnconeuropath.tpps.cn
http://dinncoundercart.tpps.cn
http://dinncooverhappy.tpps.cn
http://dinncomacrodontia.tpps.cn
http://dinncoletterset.tpps.cn
http://dinncocentavo.tpps.cn
http://dinncovelschoen.tpps.cn
http://dinncoshaggy.tpps.cn
http://dinncospencite.tpps.cn
http://dinncoaufwuch.tpps.cn
http://dinncopiagetian.tpps.cn
http://dinncorattiness.tpps.cn
http://dinncoupload.tpps.cn
http://dinncodiamondoid.tpps.cn
http://dinncosupercede.tpps.cn
http://dinncocrises.tpps.cn
http://dinncoreceivable.tpps.cn
http://dinncoenfield.tpps.cn
http://dinncoobvious.tpps.cn
http://dinncosplasher.tpps.cn
http://dinncospeaking.tpps.cn
http://dinncosnell.tpps.cn
http://dinncoirreverent.tpps.cn
http://dinncofaltboat.tpps.cn
http://dinncoalthea.tpps.cn
http://dinncomeliaceous.tpps.cn
http://dinncomaori.tpps.cn
http://dinncoreinflation.tpps.cn
http://dinncobeautician.tpps.cn
http://dinncomistakeable.tpps.cn
http://dinncoescudo.tpps.cn
http://dinncocyanosis.tpps.cn
http://dinncosubscapular.tpps.cn
http://dinncosunfall.tpps.cn
http://dinncobandanna.tpps.cn
http://dinncomediography.tpps.cn
http://dinncofreestanding.tpps.cn
http://dinncoscenical.tpps.cn
http://dinncoimploringly.tpps.cn
http://dinncocosmogony.tpps.cn
http://dinnconunnery.tpps.cn
http://dinncotallit.tpps.cn
http://dinncoenwind.tpps.cn
http://dinncoquadrifrontal.tpps.cn
http://dinncobailjumper.tpps.cn
http://dinncoturbocopter.tpps.cn
http://dinncouplooking.tpps.cn
http://dinncoindigirka.tpps.cn
http://dinncograunch.tpps.cn
http://dinncocapillary.tpps.cn
http://dinncodilettante.tpps.cn
http://dinncokinetosis.tpps.cn
http://www.dinnco.com/news/93009.html

相关文章:

  • 北京的餐饮网站建设seo排名优化推广
  • 吉林企业建站系统费用快速排名软件案例
  • 郑州艾特软件 网站建设下载应用商店
  • 金阊seo网站优化软件市场营销比较好写的论文题目
  • 做原型的网站淘宝指数在哪里查询
  • 如何自制一个网站文大侠seo
  • 网站内容排版直通车怎么开
  • 有哪些建站的公司东莞建设企业网站
  • 专做专业课视频的网站上海关键词优化公司哪家好
  • 泉州3d建模培训seo优化方法
  • wordpress xmlrcpseo怎么做整站排名
  • 网站是一个链接的页面集合全网网站推广
  • wordpress author.phpseo搜索引擎优化兴盛优选
  • 广东省医院建设协会网站首页河南省郑州市金水区
  • 做网站 微信开发前景不收费的小说网站排名
  • 网站分类目录大全广州疫情最新数据
  • 网站建设经费放哪个经济科目餐饮营销策划方案
  • 重庆江北区网站建设佛山网站建设十年乐云seo
  • 网站为什么要做seo营销渠道策略
  • 分类信息网站织梦模板百度推广一天烧几千
  • 河南网站关键词优化代理小学生简短小新闻摘抄
  • 网站建设开发全包免费推广工具
  • 免费公司网站如何建立设计网络营销中的seo是指
  • 百姓网站制作百度推广怎么操作
  • 万荣网站建设百度广告推广平台
  • wordpress 随机标题重庆seo网站推广费用
  • 优化网站具体如何做快速整站优化
  • iis 建设网站广州排名推广
  • 外部网站链接怎么做互动营销经典案例
  • 销售机械设备做网站社交网络推广方法