title: The Scope and the life time of Session attribute in Servlet in my web application get me confused when using more than 2 tabs of the same browser
body: <p>I was doing a simple web project (you can see the code below). As far as I know, session attributes are related to one session. When I opened two tabs of the same browser and run type the URL, only one session ID is created, but two different objects of the same session attribute are running (i.e I don't want to run two quizzes at the same time. But, when I changed the question in one of the tab, it doesn't affect the session attributes of the other tab). Can you explain me why it happened like that? How can I change my code to make the session variables shared so that when I changed one of the session attributes in one of the tabs, I wanted the other tab's session variables to be affected to?</p>
  
  <pre><code>/*
   * To change this template, choose Tools | Templates
   * and open the template in the editor.
   */
  package com.quizServlet;
  
  import QuizApp.Quiz;
  import java.io.IOException;
  import java.io.PrintWriter;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  
  /**
   *
   * @author Mati
   */
  @WebServlet(name = "QuizServlet", urlPatterns = {"/Quiz"})
  public class QuizServlet extends HttpServlet {
  
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter();
          try {
          } catch (Exception ex) {
              out.write("&lt;font style='color:red'&gt;&lt;b&gt;" + ex.getMessage() + "&lt;/b&gt;&lt;/font&gt;");
          } finally {
              out.close();
          }
      }
  
      @Override
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
  
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter();
          try {
              if (request.getSession().getAttribute("QuizzObject") == null) {
                  Quiz quiz = new Quiz();
                  quiz.addQuestion(new int[]{1, 2, 3, 4});
                  quiz.addQuestion(new int[]{1, 1, 2, 3, 5, 8});
                  quiz.addQuestion(new int[]{0, 5, 10, 15, 20, 25});
                  request.getSession().setAttribute("QuizzObject", quiz);
              }
              if (request.getSession().getAttribute("questionsLeft") == null) {
                  request.getSession().setAttribute("questionsLeft", true);
              }
                          Quiz qq = (Quiz) request.getSession().getAttribute("QuizzObject");
                          qq.reset();
  
              StringBuilder SB = new StringBuilder();
  
              SB.append("&lt;form name='myform' method='post'&gt;");
              SB.append("&lt;h3&gt;Have fun with NumberQuiz!&lt;/h3&gt;");
              SB.append("&lt;p&gt;&lt;input type='submit' name='btnNext' value='Start quiz' /&gt;&lt;/p&gt;");
              SB.append("&lt;/form&gt;");
              out.print(SB.toString());
  
          } catch (Exception ex) {
              out.write("&lt;font style='color:red'&gt;&lt;b&gt;" + ex.getMessage() + "&lt;/b&gt;&lt;/font&gt;");
          } finally {
              out.close();
          }
      }
  
      @Override
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          PrintWriter out = response.getWriter();
  
          try {
  
              StringBuilder SB = new StringBuilder();
              String msg="";
              SB.append("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;");
              Quiz qq = (Quiz) request.getSession().getAttribute("QuizzObject");
              SB.append(request.getSession().getId());
              boolean questionsLeft = (Boolean) request.getSession().getAttribute("questionsLeft");
              if (questionsLeft) {
                  qq.addAttempts();
                  if (request.getParameter("txtAnswer") != null) {
                      if (qq.isCorrect(Integer.parseInt(request.getParameter("txtAnswer")))) {
                          qq.scoreAnswer();
                      } else {
                          msg="&lt;p&gt;&lt;font style='color:red'&gt;Wrong Answer .. Try Again&lt;/font&gt;&lt;/p&gt;";
                      }
                  }
                  if (qq.getCurrentQuestion() == null) {
                      request.getSession().setAttribute("questionsLeft", false);
                      SB.append("Congratulations, you have completed the quiz!");
                      SB.append("&lt;br&gt;Your final score is:" + qq.getScore());
                      SB.append("&lt;br&gt;Total attempts:" + qq.getAttempt());
                      qq.reset();
                      request.getSession().setAttribute("questionsLeft",null);
                  } else {
                      SB.append("&lt;form name='myform' method='post'&gt;");
                      //SB.append("&lt;h3&gt;Have fun with NumberQuiz!&lt;/h3&gt;");
                      SB.append("&lt;p&gt;Your current score is " + qq.getScore() + ".&lt;/p&gt;");
                      SB.append("&lt;p&gt;Guess the next number in the sequence!&lt;/p&gt;");
                      SB.append("&lt;p&gt;" + qq.getCurrentQuestion().toString().replaceAll("\\?", "&lt;font style='color:red'&gt;&lt;b&gt;?&lt;/b&gt;&lt;/font&gt;") + "&lt;/p&gt;");
                      SB.append("&lt;p&gt;Your answer:&lt;input type='text' id='txtAnswer' name='txtAnswer' value='' /&gt;&lt;/p&gt;");
                      SB.append("&lt;p&gt;&lt;input type='submit' name='btnNext' value='Next' onclick='return validate()' /&gt;");
                      SB.append("&lt;input type='Reset' name='btnStart' value='Restart!' onclick=\"document.location.href='/QuizzWeb/Quiz';return false;\" /&gt;&lt;/p&gt;");
                      SB.append(msg);
                      SB.append("&lt;/form&gt;");
                      SB.append("&lt;script type='text/javascript'&gt;function validate(){if(document.getElementById('txtAnswer').value==''){alert('You should write an answer');return false;}return true;}&lt;/script&gt;");
                  }
                  SB.append("&lt;/body&gt;&lt;/html&gt;");
                  out.print(SB.toString());
              }
  
          } catch (Exception ex) {
              out.print("&lt;font style='color:red'&gt;&lt;b&gt;" + ex.getMessage() + "&lt;/b&gt;&lt;/font&gt;");
          } finally {
              out.close();
          }
  
      }
  
      @Override
      public String getServletInfo() {
          return "Short description";
      }
  }
  </code></pre>