Hi,
I am running a
JSP page, which contains two HTML forms. I have given the part of the code which I am having trouble with below. The forms basically submit a field from a drop down list (call it "dropdown list 1" for explaination purposes), which according to what is selected, populates another drop down list ("dropdown list 2") further down the page. The information is extracted from an LDAP server. The problem is, the first few times I submit a field from "dropdown list 1", it populates "dropdown list 2" fine. Then, for some reason, it decides to go bang - giving me an exception:
java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(StringTokenizer.java
I am not sure what is going on with my
String Tokenizer instance I create in my code below called 'stbox'. I have commented the bit where things go wrong (look at the comment written in capitals). Is there something about the StringTokenizer class that I need to know about??
<%@ page contentType="text/html" %>
<%--//Page Directives --%>
<%@ page language="java" %>
<%@ page import="javax.xml.parsers.*" %>
<%@ page import="org.w3c.dom.Document" %>
<%@ page import="org.w3c.dom.Element" %>
<%@ page import="org.w3c.dom.DOMException" %>
<%@ page import="java.net.URL" %>
<%@ page import="java.io.InputStream" %>
<%@ page import="java.io.IOException" %>
<%@ page import="logonBean.*" %>
<%@ page import="java.security.*" %>
<%@ page import="java.util.Hashtable" %>
<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.StringTokenizer" %>
<%@ page import="java.util.*" %>
<%@ page import="javax.naming.*" %>
<%@ page import="javax.naming.directory.*" %>
<%@ page import="javax.servlet.*" %>
<%! public String routeURL="logon.jsp";%>
<%! static String[] appnames = new String[40];%>
<%! static String[] boxnames = new String[40];%>
<%! int n=0; int flag=0; String name1 = ""; String name2 = ""; String parameterValue = ""; String parameterName="";%>
<%! int flag1=0; int m=0; String parameterValue1 = "";%>
<%-- //HTML Header --%>
<html>
<head>
<title>Main Page</title>
</head>
<h1>LDAP Server Connection</h1>
<body bgcolor="white">
<script language='javascript'>
function post(){
document.forms[0].submit();
}
</script>
<FORM name=form2 id=form2 ACTION=main.jsp METHOD=POST>
<% Hashtable env1 = new Hashtable(5, 0.75f);
env1.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env1.put(Context.PROVIDER_URL, "ldap://boxname/dc=db,dc=com");
try {
DirContext ctx1 = new InitialDirContext(env1);
SearchControls constraints1 = new SearchControls();
constraints1.setSearchScope(SearchControls.SUBTREE_SCOPE);
//instead of ou=WebSSO, take user input to determine value into variable
NamingEnumeration box_results = ctx1.search(ou + ", ou=Application", "(boxname=*)", constraints1);
m=0;
while(box_results != null && box_results.hasMore()) {
SearchResult search = (SearchResult)box_results.next();
String box = search.getName();
StringTokenizer stbox = new StringTokenizer(box, "=");
while(stbox.hasMoreElements()){
//GOING WRONG DOWN HERE!!! WORKS FOR A WHILE THEN CAUSES AN ERROR!!!
String box1 = stbox.nextToken();
String box2 = stbox.nextToken();
boxnames[m] = box2;
System.out.println(boxnames[m]+" , ");
m++;
}
}
//Exception Handling
}//end of try
catch(NamingException e)
{
e.printStackTrace();
}
parameterValue1 = request.getParameter("App");
%>
<br><br><br>
<TR> <TD> Choose a Unix Box for the Application selected</TD>
<TD> <SELECT onSelect='javascript
ost()' NAME=Box>
<% if(parameterValue1==null){%>
<OPTION selected>-------Select Value-------</OPTION>
<%}else {%>
<OPTION>-------Select Value-------</OPTION>
<%}
int l=0;
while(boxnames[l]!=null){
if(boxnames[l].equals(parameterValue1)){
%>
<OPTION selected><%=boxnames[l]%></OPTION>
<%}else{%>
<OPTION><%=boxnames[l]%></OPTION>
<%}
l++;
}
%>
</SELECT></TD>
</TR>
<INPUT TYPE=SUBMIT VALUE="Submit BOXNAME">
</FORM>
<HR>
<B>Box Selected</B><BR>
<TABLE>
<%
Enumeration parameter1 = request.getParameterNames();
parameterValue1 = request.getParameter("Box");
%>
<TR>
<TD><%=parameterValue1%></TD>
</TR>
<%
%>
</TABLE>
</body>
</html>
Thankyou for your help guys.
Kamal.