这是一个检查用户名是否已存在的函数,如果不存在则添加它。它在DepartmentBean类中。
私人列表用户;
<div class="container">
<div class="row">
<div class="menu-page" data-page="1">
<div class="col-lg-6" style="background-color: red; height: 200px;">
</div>
<div class="col-lg-6" style="background-color: yellow; height: 200px;">
</div>
</div>
<div class="menu-page" data-page="2">
<div class="col-lg-6" style="background-color: red; height: 200px;">
</div>
<div class="col-lg-6" style="background-color: yellow; height: 200px;">
</div>
</div>
</div>
</div>
在Servlet中,我检查这样的标志:
public List<UserBean> getUsers() {
return users;
}
public int flag;
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
public void addIfAbsent(UserBean userBean) {
if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))) {
setFlag(1);// flag to indicate that there is user with the same name
} else {
setFlag(2);
users.add(userBean);// add the user
}
}
如果这是因为大括号正在工作并且记录是maken。但是,如果我使用它,它不会,并带我到白页而不是index.jsp。
这是Servlet中的写入和读取函数:
DepartmentBean dp = new DepartmentBean();
UserBean user = new UserBean();//UserBean is class which store name and password
DepartmentBean departmentBean = read();
String userName = request.getParameter("username");
String password = request.getParameter("password");
user.setPassowrd(password);
user.setUsername(userName);
departmentBean.addIfAbsent(user);
if(dp.flag == 2)//if flag is 2, their's no user with the same name, so write it
{
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
write(departmentBean);//write to xml
}
答案 0 :(得分:0)
我看到你正在使用两个DepartmentBean变量:一个名为“dp”,一个名为“departmentBean”。您使用后者检查是否已存在用户(因此“departmentBean.flag”被修改)和“dp”以检查标志的值(在代码之前尚未修改)。我建议这样做:
DepartmentBean departmentBean = new DepartmentBean();
UserBean user = new UserBean();
String userName = request.getParameter("username");
String password = request.getParameter("password");
user.setPassowrd(password);
user.setUsername(userName);
departmentBean.addIfAbsent(user);
if(departmentBean.flag == 2)
{
RequestDispatcher rd=request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
write(departmentBean);//write to xml
}