1

I have following architecture in my application.

Client (GWT) <--calls--> Servlet <--calls--> Service <--calls--> Dao

I want to make this architecture easier for changes. For example: when I want return the inserted id of an object from the Dao layer, because I need it in the client, I have to update the service and the servlet layer as well. So for this little change I have to update all 3 layers (3 classes and 3 interfaces) makes a change on 6 places. I see why I need the Dao Layer. I also get why I need the service layer. What I don't get is why the service layer can't also be a servlet. At the moment all my servlets do is forwarding the request to the service layer.

The Wikipedia Article about Java Servlets says:

Servlets are most often used to
1 process or store data that was submitted from an HTML form
2 provide dynamic content such as the results of a database query
3 manage state information that does not exist in the stateless HTTP protocol

1 and 2 are just database calls, which I make in my dao layer and the service layer makes extra business logic already possible. 3 I am currently not working with sessions. I don't have a login so far and I am just playing around with my architecture, but I think the service layer could handle this as well.

I want to know the cos and pros for this architecture change:

pros:

  • One layer less to update if a change happens.

cons

  • Service Layer gets complexer.

Thx for your answers.

1 Answer 1

2

You need the servlet layer because that is what allows access to the session.

Also, you don't want your service layer to need to know anything about HTTP since you want to be able to re-use the service and DAO layers in other applications (e.g. if you write a desktop application re-using those layers) and needing to include the servlet API would not make sense there.

If needed you could call the DAO layer directly from a servlet for simple cases in order to not duplicate methods in the service and DAO layers.

The servlet API has filters which are a good place to implement security in your web application.

You can use Spring Security if you are already using the Spring framework for your web application.

4
  • I could also use Spring Security on the service layer because it is a servlet layer then or am I misstaken?
    – SCBoy
    Mar 27, 2012 at 10:17
  • 1
    Spring security's main entry point implements a filter from the servlet spec, so it build on the servlet layer. It isn't a servlet layer itself. Mar 27, 2012 at 10:22
  • You can also annotate methods in your service layer with Spring Security: static.springsource.org/spring-security/site/docs/3.0.x/…. You could do this all yourself with AOP or using regular code and passing in a user object in your service layer. Mar 27, 2012 at 10:32
  • 1
    Either way, you still need to use the servlet API in a web application Mar 27, 2012 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.