Advance Java Unit-IV Notes
Advance Java Unit-IV Notes
JavaServer Pages (JSP) is a server-side technology that creates dynamic web applications.
It allows developers to embed Java code directly into HTML or XML pages, and it makes
web development more efficient.
JSP is an advanced version of Servlets. It provides enhanced capabilities for building
scalable and platform-independent web pages.
Features of JSP
• It is platform-independent; we can write once, run anywhere.
• It simplifies database interactions for dynamic content.
• It contains predefined objects like request, response, session, and application,
reducing development time.
• It has built-in mechanisms for exception and error management.
• It supports custom tags and tag libraries.
JSP Architecture
JSP Elements
We will learn several elements available in JSP with suitable examples. In JSP elements can
be divided into 4 different types.
• Expression
• Scriplets
• Directives
• Declarations
1. Expression
2. Scriplets
This allows inserting any amount of valid Java code. These codes are placed in the
_jspService() method by the JSP engine.
Syntax:
<%
// Java codes
%>
3. Directives
A JSP directive starts with <%@ characters. In the directives, we can import packages,
define error-handling pages, or configure session information for the JSP page.
Syntax:
<%@ directive attribute=”value” %>
Types of Directives:
• page: It defines page settings.
• include: It includes other files.
• taglib: It declares a custom tag library.
4. Declarations
This is used for defining functions and variables to be used in the JSP.
Syntax:
<%!
//java codes
%>
Example:
<%@ page import="java.util.*" %>
<html>
<body>
<%!
Date theDate = new Date();
Date getDate() {
System.out.println("In getDate() method");
return theDate;
}
By: Mr. Amrit Jaiswal 2
%>
Hello! The time is now <%= getDate() %>
</body>
</html>
In Java, JavaServer Pages can provide action tags that can enable the developers to
perform specific tasks including the other files, forwarding the requests, and
manipulating the session data within the JSP documents. Action tags are denoted by the
<jsp: ..> syntax and it is used to perform the dynamic actions on the server side of the
JSP applications.
Expression Language was introduced in JSP 2.0, it is a simplified language that is used to
access and manipulate data within JSP pages also it provides a convenient way to access the
data stored in objects like requests, sessions, applica]ons, JavaBeans, etc.
We know there are n number of objects that store data, and if we want to retrieve that data
then we'll have to write a few lines of code. If we have an object request we want to
retrieve data from this object, so for this-
Syntax
${expression}
1. Variable Access:
2. Property Access:
${object.property}
${num1>num2}
${num1+num2}
4. Conditional Expressions:
5. Collection Iterator :
${param. parametersName}