What are the advantages of keeping the business logic outside JSP, since JSP's are meant mainly for presentation? We still see business logic written inside the JSP, so I needed to know what benefit we get by moving business logic out of JSP.
-
1Related: http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files – BalusC Apr 28 '11 at 16:44
7 Answers
The main benefit of MVC is you can have multiple view and clean and separated architecture & Simplicity
Re usability
Suppose tomorrow you need same app running on a desktop app. then you can just change the view.
Testability
You can unit test your service methods, but you can't simply unit test logic from view.
Maintainability
It is easy to understand the code from Service methods, also we can change it /release service api and maintain it easily
Version ability
You can give version to your API and maintain standard docs related to issues/updates if you use service API instead view for logic
See Also
It is a typical application of the Separation of Concerns design principle.
By separating concerns, i.e. by creating separate logical units (mainly classes) for each of them you reduce the number of reasons to change any particular unit.
Another benefit of SoC is reducing the average size and complexity of these units. This in turn makes your software easier to understand and to change.
Furthermore having small logical units makes them much easier to unit test, easier to mock in integration tests and easier to fix the tests after changes in the implementation.

- 60,521
- 48
- 179
- 224
-
-
and in theory you could swap out your presentation layer for something else. Or expose the functionality over a webservice. – Joeri Hendrickx Apr 28 '11 at 12:17
-
please do, thats why I commented, rather than post an answer. +1 for a good answer. – Codemwnci Apr 28 '11 at 12:18
I'll add one more reason to all the very good ones that are posted here.
Client technology is changing all the time. Users don't want to come through a desktop, browser, or mobile app; they want to use all of them, all the time. So if you embed business logic in one type of user interface technology, you'll probably have to duplicate it in all the others. That's bad for maintenance, reusability, and adding new business logic.
You don't want to have to re-write your app just because you decide to change UI technology.
It's also better for security. If business logic goes down to the browser, there's a chance that users could see the code and figure out what you're doing.
So you're better off keeping business logic on the server side.

- 305,152
- 44
- 369
- 561
- It becomes reusable (both for other applications, and for different views (e.g. a JSON API))
- It takes it away from the designers (so it doesn't get in their way, and they don't accidentally break it)

- 914,110
- 126
- 1,211
- 1,335
I'm not sure but this can be the reason:
Its for reusability purpose.
Jsp should be only used for presentation purpose and our html designer,who later design the page is not aware of java coding will not be comfortable. and writing all buiseness logic in servlet lets the code reusable.and for writing buiseness logic in jsp page there are some other way like using scriplets.so why do the job with less profit and extra work.
Now if we are using jsp page for business logic then scriptlet will be more inside the JSP page which leads to heavy maintaince cost.The seperate declaration of servlet for business unit will avoid all above.

- 58,650
- 30
- 162
- 207
It is better to reuse and maintain the web application if business logic is separated from the presentation logic.
Suppose I have 3 JSP pages, each require some common business logic to be performed. If I put the business logic inside the JSP pages, there will be duplicacy of the code.

- 255
- 1
- 4
- 12
Just to add to the good reasons posted by other peers and particularly regarding "business logic should be moved out of JSP".
In a nutshell at work we had lots of JSPs where business logic was all over and it was pretty messy looking at it. There was logic to obtain objects from the session/request and perform certain checks. One simple example is contruction of different page titles depending on certain conditions all in the JSP.
How this logic was moved at our end was to introduce a Page Builder/Composer object which takes in all the necessary details to contruct a particular page and checks and sets all the correct fields in the page bean object. This page bean object is then set on request for e.g. This means that all the previous logic you would have on the JSP is now moved to the page builder/composer object and then most importantly you can write unit tests to tests! if the right values are set in the page bean.
final SimplePageBuilder pageBuilder = new SimplePageBuilder(object1);
request.setAttribute("TestBean", pageBuilder.buildPage());
The buildPage method will return the page bean object and in the jsp a simple example getTitle would simply return the title (easy to read as the logic is abstracted).

- 1,197
- 1
- 12
- 23