我有一个html表单,用于选择应存储在xml文件中的用户名和密码。这个存储用户名详细信息的类:
@XmlRootElement(name = "Emp")
@XmlType(propOrder = {"username", "passowrd"})
public class UserBean {
String username;
String passowrd;
@XmlElement(name = "username")
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@XmlElement(name = "password")
public String getPassowrd() {
return passowrd;
}
public void setPassowrd(String passowrd) {
this.passowrd = passowrd;
}
@Override
public String toString() {
return "UserBean [username=" + username + ", passowrd=" + passowrd + "]";
}
}
这是我获取用户名详细信息的servlet,并尝试将它们存储在xml文件中:
@WebServlet("/Data")
public class Data extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final String FILE_NAME = "jaxb-emp.xml";
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserBean emp = new UserBean ();
String userName = request.getParameter("username");
String password = request.getParameter("password");
emp.setUsername(userName);
emp.setPassowrd(password);
jaxbObjectToXML(emp);
UserBean empFromFile = jaxbXMLToObject();
System.out.println(empFromFile.toString());
RequestDispatcher rd=request.getRequestDispatcher("index.jps");
rd.forward(request, response);
}
private static UserBean jaxbXMLToObject() {
try {
JAXBContext context = JAXBContext.newInstance(UserBean.class);
Unmarshaller un = context.createUnmarshaller();
UserBean emp = (UserBean) un.unmarshal(new File(FILE_NAME));
return emp;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
private static void jaxbObjectToXML(UserBean emp) {
try {
JAXBContext context = JAXBContext.newInstance(UserBean.class);
Marshaller m = context.createMarshaller();
//for pretty-print XML in JAXB
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
// Write to System.out for debugging
// m.marshal(emp, System.out);
// Write to File
m.marshal(emp, new File(FILE_NAME));
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
每当我启动tomcat并输入用户数据时,它都会覆盖该文件。问题是,我的目的是拥有许多用户名并避免重复。
答案 0 :(得分:0)
您可以为商店UserBeans使用容器类。
算法:
注意:将密码存储为plait文本是不安全的。你应该 考虑安全问题。
容器:
@XmlRootElement(name = "Department")
@XmlAccessorType(value = XmlAccessType.FIELD)
public class DepartmentBean {
@XmlElement(name = "Emp", type = UserBean.class)
@XmlElementWrapper(name = "Emps")
private List<UserBean> users;
public List<UserBean> getUsers() {
return users;
}
public void addIfAbsent(UserBean userBean) {
if (users.stream().anyMatch(x -> x.getUsername().equals(userBean.getUsername()))) {
System.err.println("User " + userBean + " is already exist, do nothing");
} else {
users.add(userBean);
}
}
}
xml示例(Department和Emps标记应始终位于文件中):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Department>
<Emps>
<Emp>
<username>Name</username>
<password>pass</password>
</Emp>
</Emps>
</Department>
使用示例:
public static void main(String... args) throws Exception {
DepartmentBean departmentBean = read();
UserBean user = new UserBean();
user.setPassword("pass");
user.setUsername("user2");
departmentBean.addIfAbsent(user);
write(departmentBean);
}
public static DepartmentBean read() throws JAXBException {
JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
return (DepartmentBean) unmarshaller.unmarshal(new StreamSource(new File("1.xml")));
}
public static void write(DepartmentBean department) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(DepartmentBean.class, UserBean.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(department, new File("1.xml"));
}