Below is a JSP that I got to work in Tomcat but when I call the page a second time I get an error: java.lang.ArrayIndexOutOfBoundsException, I have narrowed down the problem to the generateCombinations() method. I ran this same program as a java class from the compiler and it works fine, but when I write it out as a JSP it will only compile and run fine the first time and any time after that it will throw an error.
<HTML><HEAD>
<TITLE>Permutation JSP</TITLE>
</HEAD><BODY>
<%!
String letters = "ABCDE";
String temp = "";
int ltrsLen = letters.length();
String[] combo;
int c = 0;
%>
<%!
public void generateCombinations (String s, String slist, int maxc)
{
if (slist.length() == 0 || s.length() == maxc)
{
combo[c] = s;
c++;
}
for (int i = 0; i < slist.length(); i++)
{
String ts = s + slist.substring(i,(i+1));
String tlist = "";
for (int j = 0; j < slist.length(); j++)
{
if (i != j)
{
tlist = tlist + slist.substring(j,(j+1));
}
}
generateCombinations(ts,tlist,maxc);
}
}
%>
<%
int tCount = 1;
for (int t = ltrsLen; t > 0; t--)
{
tCount = tCount * t;
}
combo = new String[tCount];
generateCombinations (temp,letters,ltrsLen);
%>
<B>All combinations of <%= letters %> </B><BR>
<%
for (int cm = 0; cm < combo.length; cm++)
{
out.println(combo[cm] + " - "
;
}
%>
<BR><BR><BR><B>All combinations of A before D </B><BR>
<%
for (int cm = 0; cm < combo.length; cm++)
{
if (combo[cm].indexOf('A') < combo[cm].indexOf('D'))
{
out.println(combo[cm] + " - "
;
}
}
%>
<BR><BR><BR><B>All combinations of A before C and C before E </B><BR>
<%
for (int cm = 0; cm < combo.length; cm++)
{
if (combo[cm].indexOf('A') < combo[cm].indexOf('C') &&
combo[cm].indexOf('C') < combo[cm].indexOf('E'))
{
out.println(combo[cm] + " - "
;
}
}
%>
</BODY>
</HTML>
<HTML><HEAD>
<TITLE>Permutation JSP</TITLE>
</HEAD><BODY>
<%!
String letters = "ABCDE";
String temp = "";
int ltrsLen = letters.length();
String[] combo;
int c = 0;
%>
<%!
public void generateCombinations (String s, String slist, int maxc)
{
if (slist.length() == 0 || s.length() == maxc)
{
combo[c] = s;
c++;
}
for (int i = 0; i < slist.length(); i++)
{
String ts = s + slist.substring(i,(i+1));
String tlist = "";
for (int j = 0; j < slist.length(); j++)
{
if (i != j)
{
tlist = tlist + slist.substring(j,(j+1));
}
}
generateCombinations(ts,tlist,maxc);
}
}
%>
<%
int tCount = 1;
for (int t = ltrsLen; t > 0; t--)
{
tCount = tCount * t;
}
combo = new String[tCount];
generateCombinations (temp,letters,ltrsLen);
%>
<B>All combinations of <%= letters %> </B><BR>
<%
for (int cm = 0; cm < combo.length; cm++)
{
out.println(combo[cm] + " - "
}
%>
<BR><BR><BR><B>All combinations of A before D </B><BR>
<%
for (int cm = 0; cm < combo.length; cm++)
{
if (combo[cm].indexOf('A') < combo[cm].indexOf('D'))
{
out.println(combo[cm] + " - "
}
}
%>
<BR><BR><BR><B>All combinations of A before C and C before E </B><BR>
<%
for (int cm = 0; cm < combo.length; cm++)
{
if (combo[cm].indexOf('A') < combo[cm].indexOf('C') &&
combo[cm].indexOf('C') < combo[cm].indexOf('E'))
{
out.println(combo[cm] + " - "
}
}
%>
</BODY>
</HTML>