我有一个问题,当不止一个人在申请表上签名时,它会为第二个人提取第一个人的帐户详细信息。
当某人登录时,我会将其详细信息写入服务器端:
客户端:
//Store the LoginView data for use in subsequent Views.
AsyncCallback<ViewData> callback2 = new ViewDataStoreHandler<ViewData>();
rpc.setViewData(account.getAccountId(), account.getLevel(), null, null, null, scoutGroup, null, 0, scoutGroupSection, callback2);
服务器端:
//Store and retrieve data used by Views within the application
//This allows us to securely pass parameters between Views.
private ViewData viewData = null;
public ViewData setViewData(String accountId, String accountLevel,
String ymId, String awId, String adAwardGroup, String adScoutGroup,
String caId, Integer numberOfGroupsStarted, String groupSection) {
viewData = new ViewData();
viewData.setaccountId(accountId);
viewData.setaccountLevel(accountLevel);
viewData.setymId(ymId);
viewData.setawId(awId);
viewData.setadAwardGroup(adAwardGroup);
viewData.setadScoutGroup(adScoutGroup);
viewData.setcaId(caId);
viewData.setnoGroupsStarted(numberOfGroupsStarted);
viewData.setsection(groupSection);
return viewData;
}
然后显示第一个视图“SelectPersonView”,在其中使用以下命令获取存储的信息:
客户端:
//On load of page get the Account Level and ID of the account holder.
AsyncCallback<ViewData> callback = new ViewDataHandler<ViewData>(SelectPersonView.this);
rpc.getViewData(callback);
服务器端:
public ViewData getViewData() {
return viewData;
}
当只有一个人登录时,这可以正常工作。当我有两个人登录时,第一个人的帐户详细信息将在第二个人登录时显示。非常感谢你的帮助。
嗨,科林,这是我所实施的(我会相信你一旦我确信它是有效的):
/Store and retrieve data used by Views within the application
//This allows us to securely pass parameters between Views.
public ViewData setViewData(String accountId, String accountLevel,
String ymId, String awId, String adAwardGroup, String adScoutGroup,
String caId, Integer numberOfGroupsStarted, String groupSection) {
ViewData viewData = new ViewData();
getThreadLocalRequest().getSession(true).setAttribute("viewData", viewData);
viewData.setaccountId(accountId);
viewData.setaccountLevel(accountLevel);
viewData.setymId(ymId);
viewData.setawId(awId);
viewData.setadAwardGroup(adAwardGroup);
viewData.setadScoutGroup(adScoutGroup);
viewData.setcaId(caId);
viewData.setnoGroupsStarted(numberOfGroupsStarted);
viewData.setsection(groupSection);
return viewData;
}
public ViewData getViewData() {
return (ViewData) getThreadLocalRequest().getSession().getAttribute("viewData");
}
最佳答案
public ViewData getViewData() {
return viewData;
}
假设这是在您的
RemoteServiceServlet
中,这就是您的问题。在一个普通的JavaEE服务器中,每个servlet都是一个单独的实例——只有一个实例。这意味着webapp上的所有用户将共享同一个实例,因此服务器需要找到比在同一个servlet实例的同一字段中更好的跟踪数据的方法。一种可能的选择是使用HttpSession类存储当前活动的每个浏览器会话的特定详细信息。这些是作为HttpServletRequest对象的一部分跟踪的,并且由于服务器端服务也是一个servlet,所以可以使用它。方法
getThreadLocalRequest()
将向您发出请求,您可以将数据读写到会话中:public void setViewData(viewDataFromClient) {
getThreadLocalRequest().getSession(true).setAttribute("key-goes-here", new ViewData());
//...
public ViewData getViewData() {
return (ViewData) getThreadLocalRequest().getSession().getAttribute("key-goes-here");
}
你可能想再清理一下,但这是一个起点。注意
getSession(true)
方法,如果当前客户机不存在会话,则创建会话,但getSession()
不会尝试创建它,而是坚持它必须已经存在以完成它的工作。还要确保属性键是唯一的,因为此会话将与此webapp中的所有其他servlet共享。开发人员通常会在密钥前面加上servlet类名。当然,这个数据只存在于会话的持续时间,这取决于配置的时间有多长——它可能与浏览器退出或服务器重新启动一样短。为了长期保存数据,请考虑将数据存储在某种类型的数据库中(远远超出单个StackOverflow答案的范围…)。
对编辑的响应:
添加的代码并不能真正解决这个问题—您仍然有一个共享字段,所有客户机都将尝试读取和写入该字段,因此它们将互相删除更改
//Store and retrieve data used by Views within the application
//This allows us to securely pass parameters between Views.
private ViewData viewData = null;
public ViewData setViewData(String accountId, String accountLevel,
String ymId, String awId, String adAwardGroup, String adScoutGroup,
String caId, Integer numberOfGroupsStarted, String groupSection) {
getThreadLocalRequest().getSession(true).setAttribute
(accountId, new ViewData());
viewData.setaccountId(accountId);
viewData.setaccountLevel(accountLevel);
viewData.setymId(ymId);
viewData.setawId(awId);
viewData.setadAwardGroup(adAwardGroup);
viewData.setadScoutGroup(adScoutGroup);
viewData.setcaId(caId);
viewData.setnoGroupsStarted(numberOfGroupsStarted);
viewData.setsection(groupSection);
return viewData;
}
不要使用字段
viewData
,除非您希望所有用户都看到相同的对象。相反,请考虑以下代码,其中ViewData是一个局部变量:
public ViewData setViewData(String accountId, String accountLevel,
String ymId, String awId, String adAwardGroup, String adScoutGroup,
String caId, Integer numberOfGroupsStarted, String groupSection) {
//create a view data object with the specific details
ViewData viewData = new ViewData();
viewData.setaccountId(accountId);
viewData.setaccountLevel(accountLevel);
viewData.setymId(ymId);
viewData.setawId(awId);
viewData.setadAwardGroup(adAwardGroup);
viewData.setadScoutGroup(adScoutGroup);
viewData.setcaId(caId);
viewData.setnoGroupsStarted(numberOfGroupsStarted);
viewData.setsection(groupSection);
//store the data in the session so we remember it when the user comes back
getThreadLocalRequest().getSession(true).setAttribute(accountId, viewData);
//return the viewdata to the user
return viewData;
}
关于java - 当两个人登录时,显示第二人称的第一人称账户详细信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35421813/