I’m displaying text and xml on a webpage through a servlet. The servlet just loads the HTML/XML to the page on load through calls in the getPost() method. Now that I have that working the way I want it to my next step is to replace the static display text with calls to the database so that it can display information dynamically. My question is:
What is the best way to do this. I’m using JPA and entities for persistence but I have a choice of making direct calls to the persisted items or going through an Data/Entity Access Object which I have created. I followed a tutorial that used EAO’s and Entities with EJBs as a way of teaching best practices. I don’t see the value in doing this yet, however. It seems overly complicated to go through 3 classes when I could just access the data directly.
So is this the preferred method? or should servlets access data directly or through EAO’s?
Thanks
If you don’t need it – don’t do it.
Don’t introduce additional abstraction layer without a reason. It’s just harder to maintain, and if you don’t really need it – you’ll end with a bunch of dead code (empty delegates).
So, the first step I would do if I were you, I would use the JPA as a persistence layer. It will let you get rid of your additional DAO classes. The EntityManager (you can think of it as an entry point to the JPA) is a Data Access Object itself.
Your servlets code should not depend on the database directly, so an EntityManager is a great separation of concerns in your case.
Then you can think of separating your presentation code (Servlet) from the Data-oriented operations. You could do that by using i.e. a CDI and implementing this logic in simple POJOs. This will let you (Servlet) not to depend on the code used for fetching or transformation of your data. Your servlet will typically just ‘get the data’ (it doesn’t have to know where from it come).
Use EJBs if you need services they introduce (transactionality, thread-safety, timers, asynchronous calls, additional entry-points like SOAP or REST Web Services, JMX access, pooling, …).
HTH.