This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Servlets and the fly likes can't i use java.util.TreeMap in servlets?? 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 » Servlets
Bookmark "can Watch "can New topic
Author

can't i use java.util.TreeMap in servlets??

Mahajan Bhupendra
Ranch Hand

Joined: Dec 01, 2000
Posts: 118
hi,
i m trying to do a simple program..
u can call it a beginging of online exam program..
i m having database in which i have stored
questions,their four answers and a correct answer..
now i wanted to do a simple thing
that collect 10 random questions from a database
and putting their nos as keys in a TreeMap objet
and their respective answers as the values..
so i have first written a simple application ..
which runs perfectly..
import java.sql.*;
import java.util.*;
public class t2{

public static void main(String args[]){
Connection con;
Statement st;
ResultSet rs;
TreeMap t=new TreeMap();
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection("jdbc dbc odar");
st=con.createStatement();

while(t.size()<10){
long no=Math.round(Math.floor(Math.random()*68));
if(!t.containsKey(new Long(no))){
rs=st.executeQuery("select * from assign where id="+no);
rs.next();
System.out.println(rs.getString("ans"));
t.put(new Long(no),rs.getString("ans"));
}
}
} catch(SQLException e){System.out.println(e);}
catch(Exception e){System.out.println(e);}
Set set=t.entrySet();
Iterator i1=set.iterator();

while(i1.hasNext()){
Map.Entry me=(Map.Entry)i1.next();
System.out.print(me.getKey()+": "+Long.parseLong(me.getKey()+"")+" ");
System.out.println(me.getValue());
}
}
}
then i converted the same program in servlet..
the code is ...
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class examfinal extends HttpServlet
{
Connection con;
Statement st;
ResultSet rs;
TreeMap t;
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
PrintWriter o=res.getWriter();
t=new TreeMap();
res.setContentType("text/html");
o.println("Hihgjhg");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con=DriverManager.getConnection
("jdbc dbc odar");
st=con.createStatement();
while(t.size()<10)
{
long no=
Math.round(Math.floor(Math.random()*68));
if(!t.containsKey(new Long(no)))
{
rs=st.executeQuery("select * from assign where id="+no);
rs.next();
o.println(rs.getString("ans"));
t.put(new Long(no),rs.getString("ans"));
}
}
}

catch(SQLException e) {o.println(e);}
catch(Exception e) {o.println(e);}
}
}
and then problem starts...
the code compiles very finely..
but when i run it i get the error..
500 Internal Server Error
The servlet named examfinal at the requested URL
http://ntserver:8080/servlet/examfinal
reported this exception: Unable to find class java.util.TreeMap. Please report this to the administrator of the web server.
java.lang.ClassNotFoundException: Unable to find class java.util.TreeMap at java.lang.Throwable.(Compiled Code) at java.lang.Exception.(Compiled Code) at java.lang.ClassNotFoundException.(Compiled Code) at com.sun.server.loader.DirectoryClassLoader.getClassFromLocalDir(Compiled Code) at com.sun.server.loader.DirectoryClassLoader.loadRequestedClass(DirectoryClassLoader.java:200) at com.sun.server.loader.URLClassLoader.loadRequestedClass(URLClassLoader.java:144) at com.sun.server.loader.JarClassLoader.loadRequestedClass(JarClassLoader.java:674) at com.sun.server.loader.ServletClassLoader.loadClass(Compiled Code) at com.sun.server.loader.ServletClassLoader.loadClass(ServletClassLoader.java:312) at com.sun.server.loader.JarClassLoader.loadClass(JarClassLoader.java:603) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:304) at examfinal.doGet(Compiled Code) at javax.servlet.http.HttpServlet.service(HttpServlet.java:715) at javax.servlet.http.HttpServlet.service(HttpServlet.java:840) at com.sun.server.ServletState.callService(ServletState.java:226) at com.sun.server.ServletManager.callServletService(ServletManager.java:936) at com.sun.server.http.servlet.InvokerServlet.service(InvokerServlet.java:137) at javax.servlet.http.HttpServlet.service(HttpServlet.java:840) at com.sun.server.ServletState.callService(ServletState.java:226) at com.sun.server.ServletManager.callServletService(ServletManager.java:936) at com.sun.server.ProcessingState.invokeTargetServlet(ProcessingState.java:423) at com.sun.server.http.HttpProcessingState.execute(HttpProcessingState.java:79) at com.sun.server.http.stages.Runner.process(Runner.java:79) at com.sun.server.ProcessingSupport.process(Compiled Code) at com.sun.server.Service.process(Service.java:204) at com.sun.server.http.HttpServiceHandler.handleRequest(HttpServiceHandler.java:374) at com.sun.server.http.HttpServiceHandler.handleRequest(Compiled Code) at com.sun.server.HandlerThread.run(Compiled Code)

i have got a lot of frustartion..
since i m trying to solove this from a week
please help me..
thanks in advance..
Bhupendra

<B>Bhupendra Mahajan</B>
Frank Lin
Ranch Hand

Joined: Jan 26, 2001
Posts: 76
It looks to me that your servlet engine is using JDK1.1.X while you were compiling the code with JDK1.2 or higher. Check the classpath setting in your servlet engine.
Mahajan Bhupendra
Ranch Hand

Joined: Dec 01, 2000
Posts: 118
Originally posted by Frank Lin:
It looks to me that your servlet engine is using JDK1.1.X while you were compiling the code with JDK1.2 or higher. Check the classpath setting in your servlet engine.

how will i come to know that what exactly servlet engine is using?
my classpath is ..
my autoexec contents are..
path=%path%;c:\jdk1.2\bin;c:\jsdk2.0\lib\jsdk.jar;C:\jsdk2.0\bin;c:\javawebserver2.0\bin;c:\javawebserver2.0\servlets;
set classpath=c:\jsdk2.0\lib\jsdk.jar;c:\jdk1.2\src.jar;
what exactly should i do??
plz tell i m wating..
Frank Lin
Ranch Hand

Joined: Jan 26, 2001
Posts: 76
I don't use java web server so do not know exactly how you can configure it to use Java 2 instead of its bundled Java 1.1.7. Look into the documentation. It should be in a startup or a configuration file.
Good luck.
Mahajan Bhupendra
Ranch Hand

Joined: Dec 01, 2000
Posts: 118
Originally posted by Frank Lin:
I don't use java web server so do not know exactly how you can configure it to use Java 2 instead of its bundled Java 1.1.7. Look into the documentation. It should be in a startup or a configuration file.
Good luck.

can changing web serever will solve my problem??
i m very new to servlets..
which server should i use then??
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
which server should i use then is probably the most commanly asked question here. Tomcat and Resin are popular open source servers wihich track the latest Servlet API standards very closely and work with any version of Java. Plenty of others: Orion, JRun, Jetty etc. are also available.
Personally I use Resin (from http://www.caucho.com/ which is fast and efficient and many people find easier to install and configure than Tomcat.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Vladimir Kositsky
Ranch Hand

Joined: Nov 03, 2000
Posts: 116
Above code has expression: while(t.size()<10).
If the code was inserted into the Web page verbatim, the <10 would be interpreted as the beginning of an HTML tag, and all of the code up to the next > would likely be interpreted as malformed pieces of that tag.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: can't i use java.util.TreeMap in servlets??
 
Similar Threads
Map Doubt
Could Anyone correct my Error with JDBC
html and servlet with jdbc
please help in simple jsp application
Datasource Problem in Websphere Studio