why business logic should be moved out of JSP?
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
- Understanding MVC
- What is MVC ?
- Separation Of Concern
Adding business logic in JSP
Developing web-applications in Java has evolved quite a lot over the past 10 or so years. PHP-style JSP pages may have been ok-ish in 1999 but not today. At very minimum, you should isolate your business-logic in a servlet and only have your rendering/view in the JSP. That is just the bare-minimum to pass some form of basic level of hygiene. If you are doing any sort of new development or even refactoring, you should look into a more refined MVC/MVP framework. Spring is a good candidate. Maybe not the "coolest" but definitely established and well-known. The tutorial Andrea mentioned is a good place to start. And don't get too nervous about things like "how does it make sure the right request and session is used" - that is what spring-mvc does, it's the core functionality. Go through the tutorials, try it out and then dig deeper to understand what is happening under the hood and how you can customize the behavior to fit your needs.
Business Logic Layerin Servlet and JSP
Yes, creating a separate Java project would be the way to go. Just a simple Java Project suffices. To use it in the dynamic web project, you should not only add it as a project in the Build Path. You also need to refer it as Java EE module in the dynamic web project. Eclipse will then take care about buiding and placing the JAR the right way.
If you're on Eclipse 3.5 or older, then check the Java EE Module Dependencies section in the properties of the dynamic web project. You should add the Java project there.
If you're on Eclipse 3.6 or newer, then check the Deployment Assembly section which does essentially the same (it has just been renamed/grouped with other functions as per 3.6).
Can 'moving business logic to application layer' increase performance?
If your business logic is still in SQL statements, the database will be doing as much work as before, and you will not get better performance. (may be more work if it is not able to cache query plans as effectivily as when stored procedures were used)
To get better performance you need to move some work to the application layer, can you for example cache data on the application server, and do a lookup or a validation check without hitting the database?
Where to put business logic in spring mvc framework?
@Controller
classes serve as C from MVC. Note that the real controller in Spring MVC is DispatcherServlet
that will use the specific @Controller
class to handle the URL request.
@Service
classes should serve for your service layer. Here you should put your business logic.
@Repository
classes should serve for your data access layer. Here you should put CRUD logic: insert, update, delete, select.
@Service
, @Repository
and your entity classes will be M from MVC. JSP and other view technologies(e.g. JSP, Thymeleaf etc.) will conform V from MVC.
@Controller
classes should only have access to @Service
classes through interfaces. Similar, @Service
classes should only have access to other @Service
classes and for a specific set of @Repository
classes through interfaces.
What is the best way to migrate an existing messy webapp to elegant MVC?
Your best bet is probably to refactor it slowly as you go along. Few us of have the resources that would be required to completely start from scratch with something that has so many business rules buried in it. Management really hates it when you spend months on developing an app that has more bugs than the one you replaced.
If you have the opportunity to build any separate apps from scratch, use all of the best practices there and use it to demonstrate how effective they are. When you can, incorporate those ideas gradually into the old application.
Where to place business logic in Spring Hibernate app?
It should be in the service layer. The Controller layer is for handling and translating GUI stuff. But the creation of an user and configure it correct is not GUI stuff it is a business (or technical) use case, therefore one should place it in a service.
`personService.createPersonWithRandomPassword();`
Why we call a simple java class a bean class when it is used to hold the business logic in jsp file
Mostly the java class and the Bean class are similar one.
In Bean class we have getter and setter methods.
Its just a naming convention.
we call it as a bean class because Java Beans follow the Bean conventions like
1.Serializable
2.Default, no-arg constructor,
- getX()/setX() or isX()/setX() naming convention for read/write access to private data member X.
4.Can use java.bean.PropertyChangeEvent to notify interested parties when values change
5.Can use java.bean.PropertyChangeListener to register for notification when a particular property changes
Related Topics
How to Detect Submit Button Clicked in Multiple Submit Buttons Scenario in Single Action Class
Sorting a List of Points with Java
Why Shouldn't I Call Setvisible(True) Before Adding Components
Jtable Disable Checkbox in Cell
Layering Multiple Glasspane's in a Root Container
Jprogressbar Isn't Progressing
Maven Parent Pom VS Modules Pom
Why Does This Go into an Infinite Loop
Is the Buildsessionfactory() Configuration Method Deprecated in Hibernate
Converting a Java Keystore into Pem Format
Should I Use Java's String.Format() If Performance Is Important
Retrieve Version from Maven Pom.Xml in Code