title: Multi user safe java web application
body: <p>I have an assignment that says to make a game that is multi user safe, though when I run two instances of this application in different tabs of the same browser, they synchronize and become the same game state. Is there any way around this? also, how could i prevent two users loading the same save file as they are stored on the server. I am using Jsp and Servlets to create this application. </p>
  
  <p>EDIT: here my code for my servlet, this does all the operations and all my jsp pages just get the object and display data</p>
  
  <pre><code>package can;
  import javax.servlet.RequestDispatcher;
  import javax.servlet.*;
  import java.io.FileNotFoundException;
  import javax.servlet.http.*;
  import java.io.IOException;
  import java.io.FileOutputStream;
  import java.io.ObjectInputStream;
  import java.io.InputStream;
  import java.io.ObjectOutputStream;
  
  public class SaveLoader extends HttpServlet
  {
      public SaveLoader() {}
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
  {
  
      HttpSession sesh = request.getSession();
      String username = request.getParameter("username");
      String gameType = request.getParameter("gameType");
      String closeCase = request.getParameter("closeCase");
      String proceed = request.getParameter("proceed");
      String offer = request.getParameter("offer");
      Game game;
  
      if(username == null &amp;&amp; gameType == null &amp;&amp; closeCase == null &amp;&amp; proceed == null &amp;&amp; offer == null)
      {
          response.sendRedirect(request.getContextPath() + "/");
          return;
      }
  
      //Parameters coming from homepage
      if(username != null &amp;&amp; gameType != null)
      {
          if(gameType.equals("NG"))
          {
              game = new  Game();
              game.setUsername(username);
              sesh.setAttribute("Game", game);
              response.sendRedirect("game");
              return;
          }
          else if(gameType.equals("LG"))
          {
              response.sendRedirect(request.getContextPath() + "/");
              return;
          }
      }
  
      if(sesh.getAttribute("Game") == null)
      {
          response.sendRedirect(request.getContextPath() + "/");
          return;
      }
      else
      {
  
          //user clicks on case
          if(closeCase != null)
          {
              game = (Game) sesh.getAttribute("Game");
              if(game.getCaseLeft() != 0)
              {
                  int closeCaseINT = Integer.parseInt(closeCase);
                  closeCaseINT = closeCaseINT - 1;
  
                  Briefcase[] temp = game.getBriefcase();
                  temp[closeCaseINT].setOpen(true);
                  game.setBriefcase(temp);
  
                  int cases = game.getCaseLeft();
                  cases = cases - 1;
  
                  game.setCaseLeft(cases);
  
                  response.sendRedirect("game");
                  return;
              }
              else
              {
                  response.sendRedirect("game");
                  return;
              }
          }
  
          //user clicks on NEXT after opening number of cases
          if(proceed != null)
          {
              game = (Game) sesh.getAttribute("Game");
              if(proceed.equals("nxt") &amp;&amp; game.getRoundN() != 5)
              {
                  Briefcase[] temp = game.getBriefcase();
                  double tempPrize = 0;
                  int caseCount = 0;
                  double max = 0;
  
                  for(int i = 0; i &lt; temp.length; i++)
                  {
                      if(temp[i].isOpen() == false)
                      {
                          tempPrize += temp[i].getAmount();
                          caseCount++;
  
                          if(temp[i].getAmount() &gt; max)
                          {
                              max = temp[i].getAmount();
                          }
                      }
                  }
                  tempPrize = tempPrize / caseCount;
                  game.setMaxInPlay(max);
                  game.setPrize(tempPrize);
  
                  response.sendRedirect("bank");
                  return;
              }
              else
              {
                  Briefcase[] temp = game.getBriefcase();
                  double tempPrize = 0;
  
                  for(int i = 0; i &lt; temp.length; i++)
                  {
                      if(temp[i].isOpen() == false)
                      {
                          tempPrize += temp[i].getAmount();
                      }
                  }
                  game.setPrize(tempPrize);
  
                  response.sendRedirect("end");
                  return;
              }
          }
  
          //bank offer Deal or No deal
          if(offer != null)
          {
              game = (Game) sesh.getAttribute("Game");
              int temp = game.getRoundN();
              temp++;
              game.setRoundN(temp);
  
              if(offer.equals("NoDeal"))
              {
                  game.setPrize(0);
                  game.setMaxInPlay(0.0);
                  response.sendRedirect("game");
                  return;
              }
              else if(offer.equals("Deal"))
              {
                  response.sendRedirect("end");
                  return;
              }
          }
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>here is what all my jsp pages use to get the object</p>
  
  <pre><code>&lt;%
    HttpSession sesh = request.getSession();
    Game game = (Game) sesh.getAttribute("Game");
  
    if(game == null)
       {
          response.sendRedirect(request.getContextPath() + "/");
          return;
      }
    %&gt;
  </code></pre>