一切在本地工作正常,但在部署时,当我从servlet使用ConcurrentModificationException
时,我会抛出session.setAttribute()
。在使用HTML表单添加实体后,我使用它来从数据存储区返回一个值。
为什么我该怎么办呢?
这是破损的servlet:
public class AdminServlet extends HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
Boolean logged = false;
HttpSession session = req.getSession(true);
Enumeration<String> names = session.getAttributeNames();
while(names.hasMoreElements()) {
String name = (String) names.nextElement();
if(name.equals("logged")) {
logged = (Boolean) session.getAttribute("logged");
}
}
if(logged) {
String p1 = req.getParameter("name");
String p2 = req.getParameter("value");
if(p1 != "" && p2 != "") {
Entity e= new Entity("MyEntity");
e.setProperty("name", p1);
e.setProperty("value", p2);
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
Transaction txn = datastore.beginTransaction();
Key k = datastore.put(e);
txn.commit();
if(txn.isActive())
txn.rollback();
session.setAttribute("returnedKey", KeyFactory.keyToString(k)); // CRASHES HERE
}
}
resp.sendRedirect("adminpage.jsp");
}
}
谢谢!
答案 0 :(得分:1)
您也在会话中打开了枚举。
枚举名称= session.getAttributeNames();
更改会话中的属性会影响名称,这就是您看到错误的原因。
尝试编写代码,以便名称不在使用中(无效或超出范围):
将它放在方法
中很容易public boolean logLogged(HttpSession session){
Enumeration<String> names = session.getAttributeNames();
boolean loggedTmp=false;
while(names.hasMoreElements()){
String name = (String) names.nextElement();
if(name.equals("logged")){
loggedTmp = (Boolean) session.getAttribute("logged");
}
}
return loggedTmp;
}