the problem i have is with session handling.
i have a code for online exam.the code works fine for single user.
i tried to make the code more efficient by using session object so that multiple clients could access it.
i have a question count and the score count in my code.
the problem is that i get different session id for different browser windows,but the both the counts are
not isolated from each other.
i.e if one user has a question count 3 and then second user opens other browser,
instesd of getting question count 1 , he is getting question count 4.
same is the case for score count.for the first correct answer he is getting the score of first user's score incremented
by one and not 1
i want to know how can the question count ans score be different for different users?
these counts shoul not have any sort of relation between them.
following is the code for my
servlet ******
Please let me know what changes shall i make.
its very urgent coz i gotto submit the code as my mini project in my college
==========================================
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
public class Check_ans extends HttpServlet {
static final
String co= "count";
static final String sc= "score";
static Connection conn;
String que,ans,ans1,a,b,c,d,x1;
Statement stmt;
ResultSet rs;
PrintWriter out;
int score=0;
int count=1;
int x;
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
HttpSession session = req.getSession(true);
resp.setContentType("text/html");
out=resp.getWriter();
//out=new PrintWriter(resp.getOutputStream(),true);
resp.setHeader("Expires","Tues,01 jan 1970 00:00:00 GMT");
//Gets the question and answer given by the uuser
que=req.getParameter("que");
ans=req.getParameter("a");
try{
//Database connections
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc

dbc:exam","exam","exam");
stmt=conn.createStatement();
//selects answer from database
String q="select ans from questionbank where que='"+que+"'";
rs=stmt.executeQuery(q);
while(rs.next()){
//stores the answer from database into ans1
ans1=rs.getString(1);
}
}
catch(Exception sq)
{
out.println("Error : "+sq);
}
//session.setAttribute("count", new Integer(count));
//session.setAttribute("score", new Integer(score));
Integer i = (Integer) session.getValue(co);
if (i != null) {
count = count + 1;
//score = i.intValue() + 1;
}
Integer j = (Integer) session.getValue(sc);
if (j != null) {
//compares user's answer with answer from DB
// score is incremented if both match
if(ans.equals(ans1)){
score=score+1;
//score = i.intValue() + 1;
}
}
session.putValue(co, new Integer(count));
session.putValue(sc, new Integer(score));
out.println("Score : "+score);
//Question count is incremented
//count++;
out.println("Count : "+count);
out.println("Your session ID is <b>" +
session.getId());
//Function for generating next question
nextque();
}
//Function for next question
public void nextque(){
//calling rand () for Random number for questions
x=rand();
//Exam set for 5 questions
if(count>=5){
//After completing 5 question completed function is called
completed();
}
else{
try{
//Same functionality like Exam.class
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc

dbc:exam","exam","exam");
stmt=conn.createStatement();
String s="select que,a,b,c,d from questionbank where qno='"+x+"'";
rs=stmt.executeQuery(s);
while(rs.next()){
que=rs.getString(1);
a=rs.getString(2);
b=rs.getString(3);
c=rs.getString(4);
d=rs.getString(5);
}
}
catch(Exception e){
out.println("Error : "+e);
}
out.println("<html>");
out.println("<body>");
//Calling the same servlet from itself
out.println("<FORM action=http://localhost:8080/servlet/Check_ans method=get>");
out.println("<br><input type=hidden value='"+que+"' name=que>Question : "+que);
out.println("<br>");
out.println("<br><INPUT type=radio value='"+a+"' name=a>"+a);
out.println("<br><INPUT type=radio value='"+b+"' name=a>"+b);
out.println("<br><INPUT type=radio value='"+c+"' name=a>"+c);
out.println("<br><INPUT type=radio value='"+d+"' name=a>"+d);
out.println("<br><input type=submit value=submit>");
out.println("</form></body></html>");
}
out.flush();
out.close();
}
//Function for random number
public int rand(){
Random r1=new Random();
x=r1.nextInt(50);
return(x);
}
public void init(ServletConfig cfg)
throws ServletException
{
super.init(cfg);
}
public void destroy()
{
super.destroy();
}
//Function executed atlast when 5 questions are over
public void completed(){
out.println("<html>");
out.println("<body>");
out.println("You have Completed the test");
out.println("Score : "+score);
out.println("</body></html>");
count=0;
score=0;
//super.destroy();
}
}
[This message has been edited by sachin dabhade (edited April 24, 2001).]