wood burning stoves 2.0*
The moose likes JSP and the fly likes StringTokenizer error on JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "StringTokenizer error on JSP" Watch "StringTokenizer error on JSP" New topic
Author

StringTokenizer error on JSP

Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
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.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Kamal,
Your code will go worng when variable box has odd pairs in it
say if box="one=two=three=four"
your while loop will work fine and
String box1 = stbox.nextToken(); will fetch values one &three
String box2 = stbox.nextToken(); will fetch vales two & four
but say box="one=tow=three=four=five"
then
String box1 will fetch values one
String box2 will fetch value tow
String box1 will fetch values three
String box2 will fetch value four
String box1 will fetch values five
and now
String box2=stbox.nextToken() throws that exception
as there is no more element in string
hope this helps
Cheers
Praful


All desirable things in life are either illegal, banned, expensive or married to someone else !!!
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
.nextToken returns null once it has run out of tokens. so you have run out of tokens in your string. suggest using String s = st.nextToken(); if(s != null) doSomething(); every time you pull a token.


Kim Jong II (North Korea's Dear Leader) said:Nuclear weapons don't kill people, people kill people.
Praful Thakare
Ranch Hand

Joined: Feb 10, 2001
Posts: 614
Tim,
I guess when once the StringTokenizer object runs out of tokens,
st.nextToken will throw NoSuchElement Exception,so if if String s = st.nextToken(); throws NoSuchElement Exception,it will never execute
if(s != null) doSomething();
I think the one way to avoid exception is
if(st.hasNextToken())
String s=st.nextElement();
Kindly let me know if I m going wrong
Cheers
Praful
[ November 12, 2003: Message edited by: Praful Thakare ]
Tim Baker
Ranch Hand

Joined: Oct 04, 2003
Posts: 541
nope you're right i wasn't remembering right
Kamal Patel
Greenhorn

Joined: Nov 05, 2003
Posts: 18
Thanks Praful and Tim - it worked!
 
wood burning stoves
 
subject: StringTokenizer error on JSP
 
Similar Threads
Loading a file content in dropdown list
Getting Parameters from form
dependent select list options
Passing more than one object between jsp and servlet
Values not poulating in the dropdown box.