Q4.
Write a short note on JSP
=>
a. Jakarta Server Pages (JSP) also known as Java Server Pages is a technology used for
developing dynamic web pages based on Java.
b. It allows embedding Java code directly into HTML pages.
c. JSP is part of the Java EE (Enterprise Edition) standard and provides an easy way to build web
applications that can interact with databases and generate dynamic content.
d. Key Features of JSP
1. Separation of Logic and Presentation:
a. JSP separates the business logic from the presentation layer by using Java code and
HTML tags in different sections.
b. This makes the code easier to maintain and update.
2. Embedded Java Code:
a. Java code can be embedded within HTML using special tags like <% code %>, <%=
expression %>, and <%! declaration %>.
b. This allows for dynamic content generation based on user input or other variables.
3. Automatic Compilation:
a. When a JSP page is requested for the first time, the web container compiles it into a
servlet, which is then executed.
b. This compilation is automatic, making it easy for developers to focus on coding
rather than configuration.
4. Tag Libraries:
a. JSP supports Tag Libraries (such as JSTL - JavaServer Pages Standard Tag Library)
that allow you to use pre-built tags for common operations like loops, conditionals,
and database access, without writing Java code directly.
5. Session Management:
a. JSP can manage sessions and cookies to track user interactions and maintain
session data across multiple requests.
6. Integration with Java Beans:
a. JSP works well with JavaBeans, which are reusable components in Java. JavaBeans
encapsulate data and business logic, making it easier to build scalable
applications.
e. JSP Life Cycle
1. Translation Phase: When a JSP page is requested for the first time, the container
translates it into a servlet.
2. Compilation Phase: The servlet is compiled into a Java class.
3. Initialization Phase: The servlet is initialized by calling the init() method.
4. Request Handling Phase: The servlet processes client requests using the service()
method, which in turn calls the appropriate JSP page.
5. Destruction Phase: The servlet is destroyed when the server is shut down or when the
page is removed from the container’s memory.
f. JSP Syntax
• Directives: Used to provide information about the JSP page (e.g., page language, imports).
<%@ page language="java" contentType="text/html" %>
• Scriptlets: Embed Java code inside HTML.
<% int a = 10; %>
• Expressions: Output values to the client.
<%= "Hello, World!" %>
• Declarations: Declare variables or methods.
<%! int count = 0; %>
• Actions: Used for working with components like JavaBeans or custom tags.
<jsp:useBean id="bean" class="[Link]" />