diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/DefaultServlet.java b/servlet-core/src/main/java/tech/smartboot/servlet/DefaultServlet.java index 2341cc2..d634de9 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/DefaultServlet.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/DefaultServlet.java @@ -12,6 +12,7 @@ import jakarta.servlet.DispatcherType; +import jakarta.servlet.RequestDispatcher; import jakarta.servlet.ServletConfig; import jakarta.servlet.ServletContext; import jakarta.servlet.ServletException; @@ -113,7 +114,7 @@ private void loadDefaultFavicon() { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { - String fileName = request.getRequestURI(); + String fileName = request.getDispatcherType() == DispatcherType.INCLUDE ? (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) : request.getRequestURI(); String method = request.getMethod(); URL url = request.getServletContext().getResource(fileName.substring(request.getContextPath().length())); File file = null; @@ -195,7 +196,6 @@ private void forwardWelcome(HttpServletRequest request, HttpServletResponse resp // 而且请求的资源不存在,那么默认的 servlet 必须抛出 FileNotFoundException 异常。 // 如果这个异常没有被捕获和处理,以及响应还未􏰀交,则响应状态 码必须被设置为 500。 if (request.getDispatcherType() == DispatcherType.INCLUDE) { - response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase()); throw new FileNotFoundException(); } @@ -213,7 +213,7 @@ private void forwardWelcome(HttpServletRequest request, HttpServletResponse resp } private String matchForwardWelcome(HttpServletRequest request) throws MalformedURLException { - String requestUri = request.getRequestURI(); + String requestUri = request.getDispatcherType() == DispatcherType.INCLUDE ? (String) request.getAttribute(RequestDispatcher.INCLUDE_REQUEST_URI) : request.getRequestURI(); ServletContext servletContext = request.getServletContext(); if (requestUri.endsWith("/")) { for (String file : deploymentInfo.getWelcomeFiles()) { diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/WebXmlParseEngine.java b/servlet-core/src/main/java/tech/smartboot/servlet/WebXmlParseEngine.java index cba0191..a74929b 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/WebXmlParseEngine.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/WebXmlParseEngine.java @@ -219,19 +219,23 @@ private void parseErrorPage(WebAppInfo webAppInfo, Element parentElement) { private void parseFilterMapping(WebAppInfo webAppInfo, Element parentElement) { List childNodeList = getChildNodes(parentElement, "filter-mapping"); for (Node node : childNodeList) { - Map nodeData = getNodeValue(node, Arrays.asList("filter-name", "url-pattern", "servlet-name")); + Map nodeData = getNodeValue(node, Arrays.asList("filter-name")); String filterName = nodeData.get("filter-name"); - String urlPattern = nodeData.get("url-pattern"); - String servletName = nodeData.get("servlet-name"); - List dispatcher = getChildNodes(node, "dispatcher"); + List dispatchers = getNodeValues(node, "dispatcher"); Set dispatcherTypes = new HashSet<>(); - if (CollectionUtils.isEmpty(dispatcher)) { + if (CollectionUtils.isEmpty(dispatchers)) { dispatcherTypes.add(DispatcherType.REQUEST); } else { - dispatcher.forEach(dispatcherElement -> dispatcherTypes.add(DispatcherType.valueOf(StringUtils.trim(dispatcherElement.getFirstChild().getNodeValue())))); + dispatchers.forEach(dispatcher -> dispatcherTypes.add(DispatcherType.valueOf(dispatcher))); } - FilterMappingInfo filterInfo = new FilterMappingInfo(filterName, StringUtils.isBlank(urlPattern) ? FilterMappingType.SERVLET : FilterMappingType.URL, servletName, urlPattern, dispatcherTypes); - webAppInfo.getFilterMappingInfos().add(filterInfo); + getNodeValues(node, "url-pattern").forEach(urlPattern -> { + FilterMappingInfo filterInfo = new FilterMappingInfo(filterName, FilterMappingType.URL, null, urlPattern, dispatcherTypes); + webAppInfo.getFilterMappingInfos().add(filterInfo); + }); + getNodeValues(node, "servlet-name").forEach(servletName -> { + FilterMappingInfo filterInfo = new FilterMappingInfo(filterName, FilterMappingType.SERVLET, servletName, null, dispatcherTypes); + webAppInfo.getFilterMappingInfos().add(filterInfo); + }); } } diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletResponseImpl.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletResponseImpl.java index 62f7412..f6cccf8 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletResponseImpl.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/HttpServletResponseImpl.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.Locale; @@ -96,10 +97,6 @@ public String encodeRedirectURL(String url) { @Override public void sendError(int sc, String msg) throws IOException { - //If the response has already been committed, this method throws an IllegalStateException. - if (isCommitted()) { - throw new IllegalStateException(); - } //After using this method, the response should be considered to be committed and should not be written to. if (servletOutputStream != null) { servletOutputStream.resetBuffer(); @@ -351,13 +348,16 @@ public void flushServletBuffer() throws IOException { @Override public void resetBuffer() { - if (servletOutputStream == null) { - return; + if (servletOutputStream != null) { + servletOutputStream.resetBuffer(); } - if (servletOutputStream.isCommitted()) { - throw new IllegalStateException(); + if (writer != null) { + try { + writer = new PrintWriter(new ServletPrintWriter(servletOutputStream, getCharacterEncoding())); + } catch (UnsupportedEncodingException e) { + throw new RuntimeException(e); + } } - servletOutputStream.resetBuffer(); } @Override @@ -367,8 +367,8 @@ public boolean isCommitted() { @Override public void reset() { - if (isCommitted()) { - throw new IllegalStateException(); + if (servletOutputStream != null) { + servletOutputStream.resetBuffer(); } response.getHeaderNames().forEach(headerName -> response.setHeader(headerName, null)); setContentLength(-1); @@ -377,9 +377,6 @@ public void reset() { response.setHttpStatus(HttpStatus.OK); writer = null; responseType = RESPONSE_TYPE_NONE; - if (servletOutputStream != null) { - servletOutputStream.resetBuffer(); - } } @Override diff --git a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletOutputStreamImpl.java b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletOutputStreamImpl.java index 654f1a5..31cf1ad 100644 --- a/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletOutputStreamImpl.java +++ b/servlet-core/src/main/java/tech/smartboot/servlet/impl/ServletOutputStreamImpl.java @@ -106,6 +106,9 @@ public boolean isCommitted() { } public void resetBuffer() { + if (committed) { + throw new IllegalStateException(); + } written = 0; }