Feedback Form

Introduction to Model View Controller (MVC) in JSP and Servlets

Introduction to Model View Controller (MVC) in JSP and Servlets

What is MVC Architecture?

Model View Controller (MVC) एक बहुत ही popular design pattern है जो Web Application development में widely use किया जाता है। इसका main purpose है — application को तीन parts में divide करना: Model, View और Controller। इस separation से code maintain करना आसान होता है और logic, data और presentation को अलग-अलग handle किया जा सकता है।

MVC architecture को समझना Java Web Development या JSP-Servlet based projects के लिए बहुत जरूरी है, क्योंकि यह structure maintainability और scalability दोनों बढ़ाता है।

Why use MVC Architecture?

  • Code को अलग-अलग भागों में divide करता है जिससे maintain करना आसान होता है।
  • Reusability बढ़ती है क्योंकि logic और presentation अलग रहते हैं।
  • Team work आसान होता है — developer, designer और tester अलग-अलग काम कर सकते हैं।
  • Application की performance और scalability improve होती है।

Main Components of MVC

अब हम MVC के तीनों parts — Model, View और Controller को detail में समझते हैं।

1. Model

Model component application के data और business logic को handle करता है। यह database से data fetch करता है, उसे process करता है और फिर Controller को वापस देता है। Model में Java classes या Beans होते हैं जो data represent करते हैं।

Example: Student.java नाम की एक class जिसमें student का data store होता है जैसे rollNo, name, marks आदि।

public class Student { private int rollNo; private String name; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

2. View

View user interface को represent करता है। JSP pages या HTML files View का हिस्सा होती हैं। इनका काम होता है user को data दिखाना जो Model से आता है। View खुद business logic handle नहीं करता।

Example: JSP page में data को show करने के लिए expression tag का use करना:

Student Details

Name: <%= student.getName() %>

Roll No: <%= student.getRollNo() %>

3. Controller

Controller user requests को handle करता है और Model तथा View के बीच communication establish करता है। यह Servlet होता है जो client से आने वाले request को process करता है और response को decide करता है।

Example: Servlet code जो Model और View को connect करता है:

@WebServlet("/StudentController") public class StudentController extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Student student = new Student(); student.setName("Rahul"); student.setRollNo(101); request.setAttribute("student", student); RequestDispatcher rd = request.getRequestDispatcher("show.jsp"); rd.forward(request, response); } }

Flow of MVC in JSP and Servlets

MVC architecture का flow बहुत simple है — जब user browser से request भेजता है, तो Controller (Servlet) उसे receive करता है। Controller Model से data fetch करता है और फिर View (JSP) को send करता है display के लिए।

Step Component Description
1 View (JSP/HTML) User form या request submit करता है।
2 Controller (Servlet) Request को receive करता है और Model को call करता है।
3 Model (Java Class) Business logic process करता है और data Controller को लौटाता है।
4 Controller (Servlet) Data को View (JSP) को send करता है।
5 View (JSP) Data को user के सामने display करता है।

Advantages of Using MVC

  • Separation of Concerns: Logic, Data और UI अलग-अलग modules में divided रहते हैं।
  • Easy Maintenance: किसी एक layer में change करने से बाकी पर असर नहीं पड़ता।
  • Reusability: Model और Controller को multiple projects में reuse किया जा सकता है।
  • Team Collaboration: Different developers एक साथ अलग-अलग part पर काम कर सकते हैं।
  • Faster Development: Parallel work होने से development speed बढ़ जाती है।

MVC vs Traditional Architecture

Traditional architecture में presentation और logic दोनों एक ही file में लिखे जाते हैं, जिससे code complex हो जाता है। लेकिन MVC में clear separation होता है।

Factor Traditional Approach MVC Approach
Code Structure Mixed (HTML + Java Code) Separated into Model, View, Controller
Maintainability Difficult Easy and Modular
Testing Hard to test Each layer can be tested independently
Scalability Limited High

Real-Life Example of MVC in JSP

मान लीजिए कि हमें एक simple Login System बनाना है। इस case में:

  • Model: LoginBean.java — username और password validate करने के लिए।
  • View: login.jsp — user input लेने और result दिखाने के लिए।
  • Controller: LoginServlet.java — request handle करने और validation process चलाने के लिए।

Example Code:

// LoginBean.java public class LoginBean { private String username, password; public boolean validate() { return username.equals("admin") && password.equals("12345"); } } // LoginServlet.java @WebServlet("/login") public class LoginServlet extends HttpServlet { protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String uname = req.getParameter("username"); String pass = req.getParameter("password"); LoginBean bean = new LoginBean(); bean.setUsername(uname); bean.setPassword(pass); if(bean.validate()){ RequestDispatcher rd = req.getRequestDispatcher("success.jsp"); rd.forward(req, res); } else { RequestDispatcher rd = req.getRequestDispatcher("error.jsp"); rd.forward(req, res); } } }

Why MVC is Important for College Exams

Exam के लिए MVC architecture बहुत जरूरी topic है क्योंकि ये Java Web Application की backbone है। अधिकतर universities में इस पर short notes, explain working of MVC, या diagram-based questions आते हैं।

  • Definition और Components पर 2–5 marks के short questions।
  • Working Diagram या flow explanation पर 10 marks का long question।
  • Real-life examples जैसे Login System या Registration System पर practical question।

Key Points to Remember

  • MVC का full form है Model View Controller।
  • Model handles data और logic, View handle करता है presentation, Controller handle करता है control flow।
  • JSP pages View का part हैं जबकि Servlets Controller का part होते हैं।
  • Data handling के लिए Java Beans या POJO classes Model का part होती हैं।
  • Code separation से debugging और modification बहुत आसान हो जाता है।

Short Notes for Revision

  • Model: Application logic और data को represent करता है।
  • View: Data को user के सामने display करता है।
  • Controller: Request handle करता है और Model तथा View को connect करता है।
  • Flow: Request → Controller → Model → Controller → View → User।
  • Advantage: Clean code, better performance और आसान maintenance।

Summary (Exam Useful)

MVC architecture Web Application development में एक organized structure provide करता है। इसमें Model data handle करता है, View presentation, और Controller दोनों के बीच communication manage करता है। JSP और Servlets में MVC architecture का use करने से application efficient, maintainable और scalable बनती है — जो college exams में एक बहुत ही scoring topic है।