aspose file tools*
The moose likes Servlets and the fly likes session problem  --======urgent plz======-- Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "session problem  --======urgent plz======--" Watch "session problem  --======urgent plz======--" New topic
Author

session problem --======urgent plz======--

sachin dabhade
Ranch Hand

Joined: Mar 10, 2001
Posts: 73
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).]
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by sachin dabhade:

int score=0;
int count=1;

int x;

public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{

Make the score and count variables local to doGet method.
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException{
int score = 0;
int count = 1;
}
sachin dabhade
Ranch Hand

Joined: Mar 10, 2001
Posts: 73
but i am calling the same servlet from itself.
i.e i am generating the dynamic page and it is calling the same servlet again and again.
so if i initialize score=0,count=0 inside doGet
the score is incremented but again set to zero in next iteration.
if i take count,score as static and public the scores are incrementing but the mentioned problem arise
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124
Originally posted by sachin dabhade:
but i am calling the same servlet from itself.
i.e i am generating the dynamic page and it is calling the same servlet again and again.
so if i initialize score=0,count=0 inside doGet
the score is incremented but again set to zero in next iteration.

Well that's what you want, isn't it??? You want to keep the score and count in the session instead of keeping it in the class.. like below?

[This message has been edited by Bhupinder Dhillon (edited April 24, 2001).]
[This message has been edited by Bhupinder Dhillon (edited April 24, 2001).]
Andrew Shafer
Ranch Hand

Joined: Jan 19, 2001
Posts: 338

Sachin,
When you declare a variable as static, that means there is only one value for that variable.
No matter how many instances of a class exist, they all reference the same value of a static variable. If one instance changes the value, that value changes for all instances. That is what the declaration of static does.
However, this has nothing to do with your problem.
The only thing you have declared as static that I can see is two strings and a connection. The strings you are using to call session variables (which doesn't make a ton of sense to me) and the connection is just that. (why do you need to declare static final strings to reference your session variables? Anyway. . .)
Ok, so your servlet is running, and someone want's to take the test. They start trucking along and then someone else wants to take it too.
Now we have a score and a count variable declared as integers in the servlet. These are the variables you are using to print out the score and the count. The way you have this coded, the only thing that can ever happen to score or count is they go up one, no matter what session is making the request.
You seem to try to make some sort of check of the session value with the whole i,j stuff but you are only checking to see if these are null or not.
So when anyone answers correctly, score gets incremented and then printed out.
You appear to be assigning the correct value of score to the session attribute, but then you just print of score, which never gets reassigned for the session request.
Redesign your code so that you get and reassign the value for score and count from the session each time. It looks like you thought about sorta trying to do this, with the score = i.intValue() stuff that got commented out. (Why do you want i and j as Integers. . . What is the intValue() for? Seems like extra baggage, like the static final Strings)
I suggest stepping through all your logic and you should solve your problem.

[This message has been edited by Andrew Shafer (edited April 24, 2001).]


!_I_Know_Kung_Fu_!
Paul Gibby
Greenhorn

Joined: Apr 25, 2001
Posts: 2
If you open a new browser window from the same browser, you
will still be in the same session. You would have to use
somebody elses machine or (?) a different (new) browser program
on the same machine (not sure about this last).
--Paul
Bhupinder Dhillon
Ranch Hand

Joined: Oct 12, 2000
Posts: 124

Now consider this...


[This message has been edited by Bhupinder Dhillon (edited April 25, 2001).]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: session problem --======urgent plz======--