From 34191f948269dc91cdb14ace0ce1f58829e85669 Mon Sep 17 00:00:00 2001 From: chenweiqing <740311548@qq.com> Date: Sat, 21 Nov 2020 01:29:58 +0800 Subject: [PATCH] =?UTF-8?q?ServletMatchHandler.java:50=20-->=20=E5=85=BC?= =?UTF-8?q?=E5=AE=B9=20=E8=AF=B7=E6=B1=82=20uri=20=E4=B8=BA=20servletPath?= =?UTF-8?q?=E7=BB=93=E5=B0=BE=E4=B8=8D=E5=B8=A6=20'/'=20=E7=9A=84=E6=83=85?= =?UTF-8?q?=E5=86=B5=20HttpServletRequestImpl.java:72=20-->=20=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E6=97=B6=20=E8=AE=BE=E7=BD=AE=E5=88=9D=E5=A7=8Buri=20?= =?UTF-8?q?ServletContextImpl.java:107/361=20-->=20=E6=9A=82=E6=97=B6=20?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=20null=20=E4=BD=BForg.apache.jasper.servlet.?= =?UTF-8?q?JasperInitializer#onStartup=20=E5=8F=AF=E4=BB=A5=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=20ServletContextImpl.java:124=20-->=20=E8=B5=84?= =?UTF-8?q?=E6=BA=90=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=97=B6,=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E7=A9=BA,=E5=8F=82=E8=80=83=E5=AE=9E=E7=8E=B0org.apac?= =?UTF-8?q?he.jasper.servlet.JspCServletContext#getResource=20ServletHttpH?= =?UTF-8?q?andle.java:118=20-->=20=E5=85=BC=E5=AE=B9=20=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=20uri=20=E4=B8=BA=20servletPath=E7=BB=93=E5=B0=BE=E4=B8=8D?= =?UTF-8?q?=E5=B8=A6=20'/'=20=E7=9A=84=E6=83=85=E5=86=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/smartboot/servlet/ContainerRuntime.java | 3 +++ .../org/smartboot/servlet/ServletHttpHandle.java | 4 +++- .../servlet/handler/ServletMatchHandler.java | 5 ++++- .../servlet/impl/HttpServletRequestImpl.java | 2 ++ .../servlet/impl/ServletContextImpl.java | 16 +++++++++++++--- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/servlet-core/src/main/java/org/smartboot/servlet/ContainerRuntime.java b/servlet-core/src/main/java/org/smartboot/servlet/ContainerRuntime.java index eb602bf..25dadac 100644 --- a/servlet-core/src/main/java/org/smartboot/servlet/ContainerRuntime.java +++ b/servlet-core/src/main/java/org/smartboot/servlet/ContainerRuntime.java @@ -129,6 +129,9 @@ public class ContainerRuntime { ServletContextEvent event = new ServletContextEvent(servletContext); servletContextListener.contextDestroyed(event); }); + servletContext.getDeploymentInfo().getServlets().values().forEach(servletInfo -> { + servletInfo.getServlet().destroy(); + }); } public boolean isStarted() { diff --git a/servlet-core/src/main/java/org/smartboot/servlet/ServletHttpHandle.java b/servlet-core/src/main/java/org/smartboot/servlet/ServletHttpHandle.java index f067d03..bfbd4a1 100644 --- a/servlet-core/src/main/java/org/smartboot/servlet/ServletHttpHandle.java +++ b/servlet-core/src/main/java/org/smartboot/servlet/ServletHttpHandle.java @@ -115,7 +115,9 @@ public class ServletHttpHandle extends HttpHandle { return runtime; } for (ContainerRuntime matchRuntime : runtimes) { - if (StringUtils.startsWith(servletPath, matchRuntime.getServletContext().getDeploymentInfo().getContextPath())) { + //todo 兼容 请求 uri 为 servletPath结尾不带 '/' 的情况 + String contextPath = matchRuntime.getServletContext().getDeploymentInfo().getContextPath(); + if (StringUtils.startsWith(servletPath, contextPath) || servletPath.equals(contextPath.substring(0, contextPath.length() - 1))) { runtime = matchRuntime; contextCache.put(servletPath, runtime); break; diff --git a/servlet-core/src/main/java/org/smartboot/servlet/handler/ServletMatchHandler.java b/servlet-core/src/main/java/org/smartboot/servlet/handler/ServletMatchHandler.java index d1d5581..23b7de0 100644 --- a/servlet-core/src/main/java/org/smartboot/servlet/handler/ServletMatchHandler.java +++ b/servlet-core/src/main/java/org/smartboot/servlet/handler/ServletMatchHandler.java @@ -47,8 +47,11 @@ public class ServletMatchHandler extends Handler { request.setRequestUri(request.getRequestURI()); } else { int i = request.getRequestURI().length() - servletContext.getContextPath().length(); - if (i == 0) { + //todo 兼容 请求 uri 为 servletPath结尾不带 '/' 的情况 + if(i == -1){ request.setRequestUri(request.getRequestURI() + deploymentInfo.getWelcomeFile()); + }else if (i == 0) { + request.setRequestUri(request.getRequestURI() + deploymentInfo.getWelcomeFile().substring(1)); } else if (i == 1 && request.getRequestURI().charAt(request.getRequestURI().length() - 1) == '/') { request.setRequestUri(request.getRequestURI().substring(0, request.getRequestURI().length() - 1) + servletContext.getDeploymentInfo().getWelcomeFile()); } else { diff --git a/servlet-core/src/main/java/org/smartboot/servlet/impl/HttpServletRequestImpl.java b/servlet-core/src/main/java/org/smartboot/servlet/impl/HttpServletRequestImpl.java index 64c47a2..88a2985 100644 --- a/servlet-core/src/main/java/org/smartboot/servlet/impl/HttpServletRequestImpl.java +++ b/servlet-core/src/main/java/org/smartboot/servlet/impl/HttpServletRequestImpl.java @@ -69,6 +69,8 @@ public class HttpServletRequestImpl implements HttpServletRequest { this.dispatcherType = dispatcherType; this.servletContext = runtime.getServletContext(); this.sessionManager = runtime.getSessionManager(); + //todo 设置初始uri + this.requestUri = request.getRequestURI(); } @Override diff --git a/servlet-core/src/main/java/org/smartboot/servlet/impl/ServletContextImpl.java b/servlet-core/src/main/java/org/smartboot/servlet/impl/ServletContextImpl.java index ff5f755..d7ba12c 100644 --- a/servlet-core/src/main/java/org/smartboot/servlet/impl/ServletContextImpl.java +++ b/servlet-core/src/main/java/org/smartboot/servlet/impl/ServletContextImpl.java @@ -104,7 +104,8 @@ public class ServletContextImpl implements ServletContext { @Override public Set getResourcePaths(String path) { - throw new UnsupportedOperationException(); + //todo 暂时 返回 null 使org.apache.jasper.servlet.JasperInitializer#onStartup 可以执行 + return null; } @Override @@ -120,6 +121,12 @@ public class ServletContextImpl implements ServletContext { URL url = new URL(deploymentInfo.getContextUrl(), path.substring(1)); RunLogger.getLogger().log(Level.SEVERE, "path:" + url + " ,url:" + deploymentInfo.getContextUrl()); + //todo 资源不存在时,返回空,参考实现org.apache.jasper.servlet.JspCServletContext#getResource + try (InputStream is = url.openStream()) { + } catch (Throwable t) { + RunLogger.getLogger().log(Level.SEVERE, "path:" + url + " ,Err:" + t.getMessage()); + url = null; + } return url; } @@ -179,7 +186,9 @@ public class ServletContextImpl implements ServletContext { public String getRealPath(String path) { try { URL url = getResource(path); - return new File(url.toURI()).getAbsolutePath(); + if (url != null) { + return new File(url.toURI()).getAbsolutePath(); + } } catch (MalformedURLException | URISyntaxException e) { e.printStackTrace(); } @@ -358,7 +367,8 @@ public class ServletContextImpl implements ServletContext { @Override public JspConfigDescriptor getJspConfigDescriptor() { - throw new UnsupportedOperationException(); + //todo 暂时 返回 null 使org.apache.jasper.servlet.JasperInitializer#onStartup 可以执行 + return null; } @Override -- Gitee