Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6741147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:41:25+00:00 2026-05-26T11:41:25+00:00

I’m creating a very simple quiz application using spring mvc. It is working fine.

  • 0

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);
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T11:41:25+00:00Added an answer on May 26, 2026 at 11:41 am

    Never put state in a Controller, like in a Servlet, it will be global to everyone, and access to it has to be synchronised. In general is a good rule to just not put anything mutable as Controller’s field. You shouldn’t even put business logic in a Controller, you should call Objects from the service layer doing all the work with powerful services.

    To come to your problem, you can define an interface called QuestSession that will act as proxy to the user conversational state. It’s better if you implement boundary checks. (I guess index and score can’t be negative, for example).

    public interface QuestSession {
         public int getIndex();
         public void setIndex(int index);
         public int getScore();      
         public void setScore(int score);
    }
    

    Next you simply pass this interface where you need it, such as:

    public ModelAndView showQuestionForm2(
        @ModelAttribute("questionForm") QuestionForm questionForm, 
        BindingResult result, QuestSession session) {
    

    To make it work, you’ve to create a QuestSessionImpl and add in your XML configuration.

    <bean id="questSessionImpl" class="package.QuestSessionImpl" scope="session">
        <aop:scoped-proxy />
    </bean>
    

    The aop:scoped-proxy, is the aspect oriented programming bit that does the magic of proxying your class so each session will talk to a different object. You can even use @Autowired on a Controller field and each session will still receive a different Object.

    I don’t know how to express it with annotations, if anyone is reading this knows it, please advice.

    A word of warning. Even session access is not thread safe. Of course it’s less dangerous than a global shared access to a single field, but it’s still possible that a user opens two browser tabs or windows creating race conditions. You start to feel all the pain of it with AJAX, since many asynchronous request can come together. Even if you’re not using AJAX, you may want to add proper synchronisation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I need to clean up various Word 'smart' characters in user input, including but
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.