Ch 5 link is at the bottom of the page
Click here for more IMP AND EXAMPLES 👇
UNIT - 4 JAVA SERVER PAGE
The architecture of JSP is based on the Model-View-Controller (MVC) design pattern and integrates seamlessly with Java Servlets. A JSP page is essentially a servlet that simplifies content generation by allowing embedded HTML with Java code.
Key Components of JSP Architecture
- Client (Browser): - Sends requests to the server (via HTTP) and receives responses.
 
- JSP Container: - Part of the web server or application server (e.g., Apache Tomcat).
- Converts JSP pages into servlets and handles their execution.
 
- Translation Phase: - The JSP engine translates the JSP file into a Java servlet.
 
- Compilation Phase: - The generated servlet is compiled into bytecode (a .classfile).
 
- The generated servlet is compiled into bytecode (a 
- Servlet Execution: - The servlet processes requests and generates dynamic responses.
 
- Backend (Database/Services): 
- JSP interacts with databases, APIs, or other services to fetch or store data.
2. JSP Lifecycle
The JSP lifecycle describes how a JSP page is executed by the JSP container.
Phases in the JSP Lifecycle:
- Translation Phase: - The JSP page is translated into a servlet.
 
- Compilation Phase: - The servlet is compiled into a .classfile.
 
- The servlet is compiled into a 
- Initialization ( - jspInit()):- Called once when the servlet is initialized.
 
- Request Handling ( - _jspService()):- Handles each client request.
- The container invokes this method automatically.
 
- Destruction ( - jspDestroy()):- Called once when the JSP page is removed from memory.
 
Lifecycle Methods
- jspInit():- One-time setup tasks (e.g., resource initialization).
- Called only once during the JSP's lifecycle.
 
- _jspService(HttpServletRequest, HttpServletResponse):- Handles client requests.
- Called every time the JSP page is accessed.
 
- jspDestroy():
- Cleanup tasks before the JSP page is unloaded from memory.
3. JSP Elements
JSP elements enable the embedding of Java code in HTML pages. These include:
1. Directives:
- Provide global information about the JSP page.
- Syntax: <%@ directive attribute="value" %>
- Types:- Page Directive: Defines attributes like imports, error pages, etc.jsp<%@ page language="java" contentType="text/html" %>
- Include Directive: Includes a static resource.jsp<%@ include file="header.jsp" %>
 
- Page Directive: Defines attributes like imports, error pages, etc.
2. Scriptlets:
- Embed Java code within JSP.
- Syntax: <% Java Code %>
- Example:jsp<% int x = 10; out.println("Value of x: " + x); %>
3. Expressions:
- Output dynamic content to the client.
- Syntax: <%= expression %>
- Example:jsp<p>Current Time: <%= new java.util.Date() %></p>
4. Declarations:
- Define methods or variables that can be used in the JSP page.
- Syntax: <%! Java Code %>
- Example:jsp<%! int square(int x) { return x * x; } %>
5. Standard Actions:
- Perform specific tasks like including resources or forwarding requests.
- Syntax: <jsp:action />
- Example:jsp<jsp:include page="header.jsp" />
4. Comparison of JSP and Servlets
| Aspect | JSP | Servlets | 
|---|---|---|
| Definition | A technology for generating dynamic web pages with embedded Java code. | A Java class that processes requests and generates responses. | 
| Ease of Use | Easier to write; combines HTML and Java code. | More verbose; requires embedding HTML in Java code. | 
| Separation of Concerns | Better separation of presentation and logic. | Logic and presentation are tightly coupled. | 
| Compilation | Automatically compiled into a servlet. | Needs to be manually compiled. | 
| Preferred Use | Suitable for view (presentation layer). | Suitable for controller (business logic). | 
| Performance | Slightly slower due to additional translation and compilation steps. | Faster as it's directly a Java class. | 
5. Form and Input Elements
Forms and input elements in JSP are used for user data collection and interaction. JSP can process form data submitted via HTML.
HTML Form Elements:
- <form>:- Defines the form.
- Attributes:- method="POST"or- method="GET"
- action="target.jsp"
 
 - Example: html- <form method="POST" action="processForm.jsp"> <label for="name">Name:</label> <input type="text" id="name" name="username"> <input type="submit" value="Submit"> </form>
- <input>:- Types:- text: Single-line text input.
- password: Obscured text input.
- radio: Radio buttons.
- checkbox: Checkboxes.
- submit: Submit button.
 
 
- Types:
- <textarea>:- Multi-line text input.
 - Example: html- <textarea name="message" rows="4" cols="50"></textarea>
- <select>:- Dropdown menu.
 - Example: html- <select name="gender"> <option value="male">Male</option> <option value="female">Female</option> </select>
- <button>:
- Customizable button.
 
إرسال تعليق