Bug 33223 - pageContext.forward and <jsp:include> result in StringIndexOutOfBoundsException
pageContext.forward and <jsp:include> result in StringIndexOutOfBoundsException
Status: RESOLVED FIXED
Product: Tomcat 5
Classification: Unclassified
Component: Jasper
5.5.4
PC Windows XP
: P2 normal (vote)
: ---
Assigned To: Tomcat Developers Mailing List
:
Depends on:
Blocks:
  Show dependency tree
 
Reported: 2005-01-24 18:39 UTC by David Biesack
Modified: 2005-01-25 10:53 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
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:

test/index.jsp:

<%
  Boolean included = (Boolean) pageContext.getAttribute("template.running",
PageContext.REQUEST_SCOPE);
  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);
    pageContext.forward("/templates/template.jsp");
  }
  else
  {
%>

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

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

templates/template.jsp:

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

<html>
<head><title>Tomcat bug</title></head>
<body>

<jsp:include flush="true" page="<%=top%>"></jsp:include>
<br/>
Body:
<jsp:include flush="true" page="<%=body%>"></jsp:include>
:body
</body>
</html>
<!-- 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 -->

WEB-INF/web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
	http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
  
</web-app>