I following this example to make a method for a single person to save email from input instead of fix value. Sadly it does not change anything at all. I wonder if I misunderstood the tutorial.
Here is the DAO class to hold the user object
public User update(User user) {
MongoCollection<User> userTbl = database.getCollection("User", User.class);
userTbl.updateOne(eq("_id", "id"),combine(set("email", user.getEmail())));
return user;
}
Here is the Service to interact with the browser,
public void updateUser() throws ServletException, IOException {
Object id = request.getParameter("userId");
String email = request.getParameter("email"); // attr is name = "email" from the input field
String fullName = request.getParameter("fullname");
String password = request.getParameter("password");
System.out.println("NEW ID = " + id + ", NEW EMAIL = " + email + ", NEW NAME = " + fullName
+ ", NEW PASSWORD = " + password);
User user = new User(email, fullName, password);
userDAO.update(user);
}
In this eq("_id", "id"), you should be using something like: eq("_id", user.getId()). Also, you don’t need to use the combine method as you are updating only one field, the email.
@Prasad_Saya Thanks for your reply. I’ve tried it, but still can’t update the email for a user in my return statement. Should I need to iterate the collection to update the document of single person, per said?
@Prasad_Saya Thanks for your reply. I did and I’m in doubt with using this Object Id that I cast it to the constructor like below, or else I can’t pass the _id as it said:
The method setId(ObjectId) in the type User is not applicable for the arguments (Object)
If I could convert string to objectId could solve the problem.
I using POJOs by the way.
public void updateUser() throws ServletException, IOException {
Object id = (String) request.getParameter("id");
String email = request.getParameter("email"); // attr is name = "email" from the input field
String fullName = request.getParameter("fullname");
String password = request.getParameter("password");
System.out.println("NEW ID = " + id + ", or NEW EMAIL = " + email + ", or NEW NAME = " + fullName
+ ", NEW PASSWORD = " + password);
User user = new User((ObjectId) id, email, fullName, password);
userDAO.update(user);
String updateMsg = "User update done!";
listUser(updateMsg);
}
The User entity
public class User {
private ObjectId id;
@BsonProperty(value = "user_id")
private String userId;
private String email;
private String fullName;
private String password;
public User() {
}
public User(ObjectId id, String email, String fullName, String password) {
super();
this.id = id;
this.email = email;
this.fullName = fullName;
this.password = password;
}
// getter and setter
}
String idStr = (String) request.getParameter("id");
ObjectId objId = new ObjectId(idStr);
// ...
User user = new User(objId, email, fullName, password);
@Prasad_Saya really thanks for your support. Somehow it has an exception with the ObjectId. I wonder if the ObjectId in the entity can use a simple type like string could retrieve the same result, which means that not using POJOs mapping at all? howvever, it will break everything of my other CRUD operation.
The server encountered an unexpected condition that prevented it from fulfilling the request.
It shows the correct value, but the _id of the user in the console, e.g. NEW ID = null, or NEW EMAIL = pepe@bbc.com, or NEW NAME = Pepe Ronaldo , NEW PASSWORD = pepe123
Here is the Servlets for update user I just call it from the Service class.
@WebServlet("/update_user")
public class UpdateUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
dBUtils.getMongoDB();
UserService userService = new UserService(request, response);
userService.updateUser();
}
}
My apology!!! I made a typo to the JSP page and this is why I can’t get the parameter. I left userId instead of id while learning POJO few days ago (my bad)!
it now working like charm! NEW ID = 5f8172544236e93fcf86c6e1, or NEW EMAIL = pepe@bbc.com, or NEW NAME = Pepe Ronaldo , NEW PASSWORD = pepe123