我的任务是制作一个多用户安全的游戏,但是当我在同一浏览器的不同选项卡中运行此应用程序的两个实例时,它们会同步并成为相同的游戏状态。有没有办法解决?另外,我怎么能阻止两个用户加载与存储在服务器上相同的保存文件。我正在使用Jsp和Servlets来创建这个应用程序。
编辑:这是我的servlet代码,这样做所有的操作和我所有的jsp页面都只获取对象和显示数据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 && gameType == null && closeCase == null && proceed == null && offer == null)
{
response.sendRedirect(request.getContextPath() + "/");
return;
}
//Parameters coming from homepage
if(username != null && 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") && game.getRoundN() != 5)
{
Briefcase[] temp = game.getBriefcase();
double tempPrize = 0;
int caseCount = 0;
double max = 0;
for(int i = 0; i < temp.length; i++)
{
if(temp[i].isOpen() == false)
{
tempPrize += temp[i].getAmount();
caseCount++;
if(temp[i].getAmount() > 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 < 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;
}
}
}
}
}
这是我的所有jsp页面用来获取对象的内容
<%
HttpSession sesh = request.getSession();
Game game = (Game) sesh.getAttribute("Game");
if(game == null)
{
response.sendRedirect(request.getContextPath() + "/");
return;
}
%>