I have a JSP with all the fields and i in the servlet i am creating the DTO instance in the servlet and setting all the entered data in the setters of the User Object , Now i want to pass this data to the DAO Layer to perform a Insert operation .
public void setName(request.getParameter("Name"))
I am new to development .
Please tell me correct approach if i am wrong
inside the servlet which gets all the data from the JSP page or inside the DAO layer ??
It depends. If you are creating a three-tier web application, then you would not have this in the servlet code. The three tiers are Presentation, Business and Integration. The user data is passed down from the Presentation tier to the Business tier. The User object would be created in whatever business logic code exists on the Business tier. The DAO code is part of the Business tier.
If you are not creating a three-tier application, then you basically can create the User object wherever you want to.
Understand that the DAO layer is part of the Business tier.
If you use the User object as both a business object and a DTO, then you will create the User object in the Presentation tier and pass this to the Business tier code. Once the User object is in the Business tier, it is no longer a DTO, it is a business object. If there is any business logic related to processing the User object, then this occurs. To move the data from the Business tier to the Integration tier, you pass the object to the DAO code, and the DAO code stores the record on some data storage device on the Itegration tier.