0% found this document useful (0 votes)
4 views

Advance Java Unit-IV Notes

Uploaded by

ayushgupta15012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Advance Java Unit-IV Notes

Uploaded by

ayushgupta15012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit-IV

Java Server Page(JSP)

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 follows a three-layer architecture:


• Client Layer: The browser sends a request to the server.
• Web Server Layer: The server processes the request using a JSP engine.
• Database/Backend Layer: Interacts with the database and returns the response
to the client.

Steps to Create a JSP

1. Take any HTML file you have previously created.


2. Change the file extension from .html to .jsp.
3. Load the new .jsp file in a browser.

When we load a JSP file for the first time:


• JSP is converted into a Java file.
• Java file is compiled into a servlet.
• Compiled servlet is loaded and executed.

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

By: Mr. Amrit Jaiswal 1


This tag is used to output any data on the generated page. These data are automatically
converted to a string and printed on the output stream.
Syntax:
<%= “Anything” %>

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
%>

Variables available to the JSP Scriptlets are:


• Request
• Response
• Session
• Out

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>

Example of a JSP Web Page


Example:
<!DOCTYPE html>
<html>
<head>
<title>A Web Page</title>
</head>
<body>
<% out.println("Hello there!"); %>
</body>
</html>

Running a Simple JSP Page

Steps to Run JSP


• Save JSP file using the .jsp extension (e.g., hello.jsp).
• Start server (e.g., Apache Tomcat).
• Place your application inside the appropriate folder (e.g., webapps for Tomcat).
• Open the browser and enter the JSP page URL:
http://localhost:portnumber/YourApplicationContextRoot/jspfile

Why Use JSP?

JSP is powerful because it allows us to:


• Embed Java logic directly into HTML.
• To create dynamic pages that respond to user actions.
• To customize content for each user or session.

JSP - Action Tags

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.

List of the Commonly used JSP Action Tags in JSP

By: Mr. Amrit Jaiswal 3


Tag Name Syntax Description Example
Include <jsp:include It can be used to <jsp:include
Tag page="filename.jsp" include the content of page="header.jsp" />
/> the other resources like
the JSP, HTML, or
servlet in the current
JSP page.
Forward <jsp:forward This tag can be used to <jsp:forward
Tag page="destination.js forward the current page="success.jsp"
p" /> request to another />
resource like the JSP,
HTML, or servlet
without the client's
knowledge
setPropert <jsp:setProperty This tag can be used to < jsp:setProperty
y Tag name="beanName" set the properties of the name="user"
property="propertyN JavaBean component of property="username"
ame" the JSP pages. value="John" />
value="propertyValu
e" />
getPropert <jsp:getProperty This tag can be used to <jsp:getProperty
y name="beanName" retrieve the properties name="user"
property="propertyN of the JavaBean property="username"
ame" /> component of the JSP />
pages.
useBean < jsp:useBean This tag can be used to <jsp:useBean
Tag id="beanId" instantiate the id="user"
class="packageNam JavaBean component class="com.example.
e.className" /> or retrieve the existing User" />
instance of the JSP
pages.
plugin Tag <jsp:plugin It can be used to <jsp:plugin
type="pluginType" generates the HTML type="applet"
code="pluginCode" code for the browser code="MyApplet.clas
/> specific plugin. s" />
attribute <jsp:attribute This tag can be used to <jsp:attribute
tag name="attributeNam defines the attributes name="color"
e" values for the custom value="blue" />
value="attributeValu actions of the JSP
e" /> pages.
Body Tag <jsp:body> <!-- Body This tag can be used to <jsp:body> <p>This is
Content --> defines the body the body
</jsp:body> content for the custom content.</p>
actions of the JSP </jsp:body>
pages.

By: Mr. Amrit Jaiswal 4


Expression Language in JSP

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-

• use request.getParameter() says we have an object session

• If we want to retrieve data from the session, then we will use


the session.getAttribute() but this would require extra code.

Necessity of Expression Language :


1. Expression Language makes the data access work much easier.
2. The data stored in the objects can be directly retrieved with the help of the
Expression Language
3. The data is stored in the JavaBeans Component.
4. It allows to access a bean by following a short syntax, ${ expression}. It can also
include literals.

Syntax

It represents a placeholder which represents that the value of the


specified expression should be dynamically inserted at runtime

${expression}

1. Variable Access:

Used to access the value of a variable

By: Mr. Amrit Jaiswal 5


${variableName}
${xxxScope.variableName}

2. Property Access:

Used to access the value of a property

${object.property}

3. Arithmetic and Logical Operators:

Performs arithmetic or logical operations on numbers

${num1>num2}
${num1+num2}

4. Conditional Expressions:

Evaluate a condition and return one of two values.

${condition ? value1 : value2 }

5. Collection Iterator :

Iterates over a collection and prints each item

<c:forEach var="item" items = "${collection}" >


${item}
</c:forEach >

6. Accessing Request Parameters:

Accesses the value of a request parameter

${param. parametersName}

By: Mr. Amrit Jaiswal 6

You might also like