Answer the question
In order to leave comments, you need to log in
Why is DTO needed in simple servlets?
Could you please explain whether it is necessary to use DTO in servlets?
I'm trying to understand logically why
I get the Parameters like this and immediately make an Entity.
String name = req.getParameter("name");
String description = req.getParameter("description");
String category = req.getParameter("category");
String price = req.getParameter("price");
Answer the question
In order to leave comments, you need to log in
See, an entity in a database can contain many more fields and objects than you want to pass up or receive as a form.
As an example, displaying user information. The entity will most likely contain fields about the password hash, ackles (ACLs), additional links with other objects. On the top, most likely, this information should not be transferred, this is the inner workings of your application. To do this, they create a DTO (data transfer object), in which there is exactly what needs to be transferred. And yes, you need to do merging or encapsulation between entity and dto every time back and forth. On the one hand, there are unnecessary actions and transformations from objects to objects, on the other hand, there is a guarantee of security and separation of the logic of storage and the logic of transferring entities.
Whether it is necessary specifically in your application - no one will tell except you. But, it's good practice to make a dto for every case.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question