MVC is an architectural design pattern.
- Model - business logic.
- View - User Interface
- Controller - handles user input
Why MVC ?
- View is free of any business logic so looks simple and readable.
- Business logic within the model can be reused for more than one views.
Simple example for MVC with JSP
- A web page for register the student details.
- Here Student name, Parent name, Grade, and Address are input
fields.
- User can enter these values by clicking "Insert"
- Here Student name and Grade are mandatory fields.
- System should validate this fields and show the proper error message.
- Create jsp pages for input interface, Error, Success response messages. - view
- Create a java class (Student.java) for a student model - model
- Create a servlet class for redirect a proper view - controller
Student Registration page.
When we click "Insert" button after enter all details.
If we don't enter the Mandatory fields. This error message will display
Mapping a servlet page
Servlets need to be configured ans it needs to be mapped to a URI (URL/URN).
So we have to include the following mapping to the web.xml file.
<servlet>
<servlet-name>ServletPage</servlet-name>
<servlet-class>org.ymini.lukshica.controller.ServletPage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletPage</servlet-name>
<url-pattern>/Test</url-pattern>
</servlet-mapping>
Mappings are used by the servlet container to translate a particular request URI to a particular servlet.