From 4c5035364f49fb580c858e3fff560e1cb6512e45 Mon Sep 17 00:00:00 2001 From: markt Date: Mon, 20 Jun 2011 22:34:16 +0000 Subject: [PATCH] Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=51396 Correctly handle jsp-file entries in web.xml when the JSP servlet has been configured via code when embedding Tomcat. git-svn-id: https://svn.apache.org/repos/asf/tomcat/trunk@1137806 13f79535-47bb-0310-9956-ffa450edef68 --- java/org/apache/catalina/startup/ContextConfig.java | 20 +++++++++++++++++--- .../apache/catalina/startup/TestContextConfig.java | 17 +++++++++++++++++ test/webapp-3.0-fragments/WEB-INF/web.xml | 6 ++++++ test/webapp-3.0-fragments/bug51396.jsp | 21 +++++++++++++++++++++ webapps/docs/changelog.xml | 4 ++++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 test/webapp-3.0-fragments/bug51396.jsp diff --git a/java/org/apache/catalina/startup/ContextConfig.java b/java/org/apache/catalina/startup/ContextConfig.java index 4e50dd02a..bf8394994 100644 --- a/java/org/apache/catalina/startup/ContextConfig.java +++ b/java/org/apache/catalina/startup/ContextConfig.java @@ -1381,15 +1381,29 @@ public class ContextConfig } private void convertJsps(WebXml webXml) { + Map jspInitParams; ServletDef jspServlet = webXml.getServlets().get("jsp"); + if (jspServlet == null) { + jspInitParams = new HashMap(); + Wrapper w = (Wrapper) context.findChild("jsp"); + if (w != null) { + String[] params = w.findInitParameters(); + for (String param : params) { + jspInitParams.put(param, w.findInitParameter(param)); + } + } + } else { + jspInitParams = jspServlet.getParameterMap(); + } for (ServletDef servletDef: webXml.getServlets().values()) { if (servletDef.getJspFile() != null) { - convertJsp(servletDef, jspServlet); + convertJsp(servletDef, jspInitParams); } } } - private void convertJsp(ServletDef servletDef, ServletDef jspServletDef) { + private void convertJsp(ServletDef servletDef, + Map jspInitParams) { servletDef.setServletClass(org.apache.catalina.core.Constants.JSP_SERVLET_CLASS); String jspFile = servletDef.getJspFile(); if ((jspFile != null) && !jspFile.startsWith("/")) { @@ -1405,7 +1419,7 @@ public class ContextConfig } servletDef.getParameterMap().put("jspFile", jspFile); servletDef.setJspFile(null); - for (Map.Entry initParam: jspServletDef.getParameterMap().entrySet()) { + for (Map.Entry initParam: jspInitParams.entrySet()) { servletDef.addInitParameter(initParam.getKey(), initParam.getValue()); } } diff --git a/test/org/apache/catalina/startup/TestContextConfig.java b/test/org/apache/catalina/startup/TestContextConfig.java index 3c78258ca..ca29e64ee 100644 --- a/test/org/apache/catalina/startup/TestContextConfig.java +++ b/test/org/apache/catalina/startup/TestContextConfig.java @@ -69,6 +69,23 @@ public class TestContextConfig extends TomcatBaseTest { assertEquals("OK - Custom default Servlet", res.toString()); } + public void testBug51396() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + File appDir = new File("test/webapp-3.0-fragments"); + // app dir is relative to server home + tomcat.addWebapp(null, "/test", appDir.getAbsolutePath()); + + tomcat.start(); + + ByteChunk bc = new ByteChunk(); + int rc = getUrl("http://localhost:" + getPort() + + "/test/bug51396.jsp", bc, null); + + assertEquals(HttpServletResponse.SC_OK, rc); + assertTrue(bc.toString().contains("

OK

")); + } + private static class CustomDefaultServletSCI implements ServletContainerInitializer { diff --git a/test/webapp-3.0-fragments/WEB-INF/web.xml b/test/webapp-3.0-fragments/WEB-INF/web.xml index 4dca98282..177185c13 100644 --- a/test/webapp-3.0-fragments/WEB-INF/web.xml +++ b/test/webapp-3.0-fragments/WEB-INF/web.xml @@ -43,4 +43,10 @@ + + + bug51396 + /bug51396.jsp + + \ No newline at end of file diff --git a/test/webapp-3.0-fragments/bug51396.jsp b/test/webapp-3.0-fragments/bug51396.jsp new file mode 100644 index 000000000..9863826bf --- /dev/null +++ b/test/webapp-3.0-fragments/bug51396.jsp @@ -0,0 +1,21 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--%> + + +

OK

+ + \ No newline at end of file diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 09e0207f4..e267c4ecb 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -91,6 +91,10 @@ includes a SAXParserFactory is the first web application to be loaded. (markt) + + 51396: Correctly handle jsp-file entries in web.xml when the + JSP servlet has been configured via code when embedding Tomcat. (markt) + -- 2.11.0