Objective
Understand JavaServer Pages (JSP) as a technology for separating presentation (UI) from content generation logic in web applications. Learn the JSP lifecycle, MVC architecture, key JSP elements (directives, scriptlets, expressions, etc.), and how to integrate JSP with Servlets for dynamic web applications.
Lecture Topics
1. JSP: Separating UI from Content Generation Code
- Definition: JSP is a server-side technology that allows developers to create dynamic web pages by embedding Java code in HTML.
- Purpose:
- Simplifies presentation layer development compared to Servlets, which require manual response writing (e.g.,
response.getWriter().write()
).
- Enables separation of concerns by isolating UI (HTML/CSS) from business logic (handled by Servlets or Java classes).
- How it Works:
- JSP pages are compiled into Servlets by the web container at runtime.
- The compiled Servlet processes requests and generates dynamic HTML output.
- Advantages:
- Easier to write and maintain UI compared to Servlets.
- Supports reusable components (e.g., custom tags, JSTL).
- Integrates seamlessly with Java code and frameworks like Spring.
2. MVC Architecture
- Definition: Model-View-Controller (MVC) is a design pattern that separates application logic into three components:
- Model: Represents data and business logic (e.g., POJOs, DAOs).
- View: Handles presentation (e.g., JSP pages).
- Controller: Manages requests and coordinates between Model and View (e.g., Servlets).
- Role of JSP in MVC:
- JSP acts as the View, rendering data provided by the Controller.
- Servlets (Controller) process requests, interact with the Model (e.g., via DAO), and forward data to JSP for display.
- Benefits:
- Separation of concerns: UI designers work on JSP, developers focus on Servlets/DAOs.
- Maintainability and scalability.
- Example:
- Servlet retrieves a list of books from a DAO, sets it as a request attribute, and forwards to a JSP.
- JSP displays the book list in an HTML table.
3. Design Pattern: MVC Pattern
-
Components in J2EE Context:
- Model: JavaBeans (POJOs) or DAOs for data access (e.g., using JDBC or Hibernate).
- View: JSP pages with HTML, CSS, and JSTL for rendering.
- Controller: Servlets handling HTTP requests and responses.
-
Flow:
- Client sends a request to a Servlet.
- Servlet processes the request, interacts with the Model (e.g., retrieves data from a database).
- Servlet sets data in request/session attributes and forwards to a JSP.
- JSP renders the data as HTML for the client.
-
Example (pseudo-code):
// Servlet (Controller)
public class BookServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
BookDAO bookDAO = new BookDAOImpl();
List<Book> books = bookDAO.getAll();
req.setAttribute("books", books);
req.getRequestDispatcher("/books.jsp").forward(req, resp);
}
}
4. Life Cycle of a JSP Page
- Stages:
- Translation: JSP is converted into a Servlet (Java source code) by the container.
- Example:
index.jsp
becomes index_jsp.java
.
- Compilation: The generated Servlet is compiled into a
.class
file.
- Loading: The Servlet class is loaded by the container.
- Instantiation: The container creates an instance of the Servlet.
- Initialization: The
jspInit()
method is called to initialize resources.
- Request Processing: The
_jspService(HttpServletRequest, HttpServletResponse)
method handles client requests.
- Destruction: The
jspDestroy()
method is called when the JSP is removed.