Bug 33223

Summary: pageContext.forward and <jsp:include> result in StringIndexOutOfBoundsException
Product: Tomcat 5 Reporter: David Biesack <David.Biesack>
Component: JasperAssignee: Tomcat Developers Mailing List <dev>
Severity: normal    
Priority: P2    
Version: 5.5.4   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description David Biesack 2005-01-24 18:39:11 UTC
We have a tag library which we use on JSP pages. The Page tag forwards to a
controller (via pageContext.forward() which then redirects to a template page
and the template page includes via <jsp-include> the site navigation etc. then
includes  via <jsp-include> the original request page. This works in Tomcat 4
but it throws an exception in Tomcat 5 - I tried with 5.5.4.

In debugging this, I found the problem is JspServlet:service which tries to call 

if (requestUri != null){
                String currentIncludedUri 
                    = requestUri.substring(requestUri.indexOf(includeUri));

with the values:
includeUri= "/test/index.jsp"
requestUri= "/tomcat-bug/test/"

Since includeUri does not appear in requestUri, indexOf returns -1 and this
causes a runtime exception.

This occurs because my webapp has index.jsp in the web.xml welcome-file-list,
but when the initial page is being executed, request.getRequestURI() returns the
 requested page, not the resolved welcome page. For example, 

Here is a simplified example showing how to reproduce. I deployed this in the
webapp context "tomcat-bug" on Tomcat 5.5.4 with no front end web server -
Tomcat is running as the web server. I removed the tag libraries and other
infrastructure and created simple JSP files that demonstrate the root problem,
so no classes or jars are required in WEB-INF/lib or WEB-INF/classes. 

URL:  http://localhost:8080/tomcat-bug/test/
Note: I don't get the exception with the URL
http://localhost:8080/tomcat-bug/test/index.jsp, only when the page is resolved
via the welcome-file

Web application files:


  Boolean included = (Boolean) pageContext.getAttribute("template.running",
  if (included == null)
    String uri = request.getRequestURI();
    String path = request.getContextPath();
    if (path != null && uri.startsWith(path))
       uri = uri.substring(path.length());
    pageContext.setAttribute("template.body", uri, PageContext.REQUEST_SCOPE);

<p>This is my JSP page, test/index.jsp. This is the body/content of the page.</p>

<!-- end of test/index.jsp -->


    pageContext.setAttribute("template.running", Boolean.TRUE,
    String top = "/templates/top.jsp";
    String body = (String) pageContext.getAttribute("template.body",

<head><title>Tomcat bug</title></head>

<jsp:include flush="true" page="<%=top%>"></jsp:include>
<jsp:include flush="true" page="<%=body%>"></jsp:include>
<!-- end of templates/template.jsp -->

templates/top.jsp :
<p>This is the top navigation bar for the page template (top.jsp)<p>
<!-- end of templates/top.jsp -->


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"