I'm creating a very simple quiz application using spring mvc. It is working fine. But right now if a user is on the third question and another request comes in from another browser (another user) it will render the fourth question to the new user. I don't want this to happen. Every new request should start the quiz from the first question. How do I achieve this without having a login form for each user and yet identify each new request from a different browser as a different user? I know this can be achieved using sessions.
Can someone explain how to do this?
package dmv2.spring.controller;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import dmv2.form.QuestionForm;
import dmv2.model.Exam;
import dmv2.model.Question;
@Controller
@SessionAttributes
@RequestMapping("/Exam")
public class ExamController
{
private List<Question> questions = (new Exam()).getQuestions();
private int index = 0;
private int score = 0;
@RequestMapping(method = RequestMethod.GET)
public ModelAndView showQuestionForm()
{
Question q = questions.get(index);
return new ModelAndView("exam", "questionForm", new QuestionForm()).addObject("q", q);
}
@RequestMapping(method = RequestMethod.POST)
public ModelAndView showQuestionForm2(@ModelAttribute("questionForm") QuestionForm questionForm, BindingResult result)
{
Question q = questions.get(index);
if(q.getAnswer().getRightChoiceIndex() == Integer.parseInt(questionForm.getChoice()))
score = score + 1;
index = index + 1;
if(index < questions.size())
{
q = questions.get(index);
}
else
return new ModelAndView("result").addObject("score", score);
return new ModelAndView("exam", "questionForm", new QuestionForm()).addObject("q", q);
}
}