0

I'm currently using TomEE 8.0.8 WebProfile. I can't use Spring nor anything else besides what's included in TomEE.

I'm trying to implement a 3 tier application. The application has already been implemented in a 2 tier way (Business logic is written in HTTPServlet classes, which call DAOs and then dispatch to .jsp files). I'm having some difficulties trying not to tie up business logic with Servlet implementation details.

All my service classes are Stateless EJB with methods which may take DTOs as parameters and return other DTOs as result.

These service classes should return more than one error if needed (for example: bad length for attr 1, bad format for attr 2, empty attr 3): my idea was to create a custom Exception with a List<> of errors to throw from service layer. However, I don't know if this is approach is correct nor if there is some clean way to handle these exception on the controllers, without "try/catch" everywhere. (Maybe an interceptor? Is it possible with vanilla servlets?)

cidra
  • 374
  • 1
  • 4
  • 16
  • 1
    This is not a duplicate. This question is about decoupling business layer (EJB) and front layer (Servlets). The referenced duplicate is about show messages in presentation layer (jsp). – gmanjon Dec 22 '21 at 10:14
  • @gmanjon all questions on SO are seen as either duplicates or XY problems by many people – cidra Dec 25 '21 at 11:00

1 Answers1

1

Your question relates more to design approach rather than implementation, therefore it can have more than one answer. However, in your context of 3-tier application one you are following is:

JSP (client browser) --> Servlet --> EJB --> Database

Which is traditionally used and a sound approach, from that perspective (and this is strictly my view), you can do the data validations in the servlet layer, preferably with a framework like Java Validation Framework. The interceptor approach would also be sound, servlet filters can be leveraged here to provide the validation.

Ironluca
  • 3,402
  • 4
  • 25
  • 32