0% found this document useful (0 votes)
1 views6 pages

JSP

Java Server Pages (JSP) is a server-side technology that enables the creation of dynamic web applications by embedding Java code into HTML or XML. It simplifies web development compared to Servlets by separating UI and business logic, reducing code length, and providing easy database interactions. JSP features a three-layer architecture and supports various elements such as expressions, scriptlets, directives, and declarations for efficient application development.

Uploaded by

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

JSP

Java Server Pages (JSP) is a server-side technology that enables the creation of dynamic web applications by embedding Java code into HTML or XML. It simplifies web development compared to Servlets by separating UI and business logic, reducing code length, and providing easy database interactions. JSP features a three-layer architecture and supports various elements such as expressions, scriptlets, directives, and declarations for efficient application development.

Uploaded by

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

JAVA SERVER PAGES

Java Server 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. It makes web development more efficient, scalable
and platform-independent.

JSP versus Servlets

JSP simplifies web development by combining the strengths of Java with the
flexibility of HTML. Some advantages of JSP over Servlets are listed below:

 JSP code is easier to manage than Servlets as it separates UI and


business logic.
 JSP minimizes the amount of code required for web applications.
 Generate content dynamically in response to user interactions.
 It provides access to the complete range of Java APIs for robust
application development.
 JSP is suitable for applications with growing user bases.
Key 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.

Differences Between JSP and Servlets

Features JSP Servlet

Jsp usually requires Servlets usually requires


Code Length
less code more code

Servlet is more complex to


Ease of Use Jsp is simple to use
use

Dynamic Easily embedded in Requires HTML generation


Content HTML in code

Page
Maintenance
Easier to maintain More challenging

.
Steps to Create a JSP Application

JSP allows us to embed Java code within HTML pages and making it easy to
create dynamic content. Let us start with a simple exercise to convert an
existing HTML file into a JSP file.

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.

Adding Dynamic Content with JSP


Here is an example to demonstrate how JSP can generate dynamic content:
hello.jsp

<!DOCTYPE html>
<html>
<body>
Hello! The time is now <%= new java.util.Date() %>
</body>
</html>

Explanation:
 The <%= %> tags enclose a Java expression.
 new java.util.Date() expression retrieves the current date and time.
 When the JSP page is loaded in the browser, the Java expression is
evaluated at runtime, and output is embedded into the HTML.
 Each time you reload the page, it displays the current time, demonstrating
how JSP dynamically generates HTML content based on Java logic.
JSP Elements

JSP has several elements which can be divided into 4 different types.

 Expression
 Scriplets
 Directives
 Declarations
1. Expression

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

JSP Expressions start with Syntax of JSP Scriptles are with <%= and ends
with %>. Between these, you can put anything that will convert to the String
and that will be displayed.
Example:
<%=”HelloWorld!” %>

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
%>
Note: JSP Scriptlets begins with <% and ends %> . We can embed any
amount of Java code in the JSP Scriptlets. JSP Engine places these codes in
the _jspService() method.

Example:

<%
String name = “Java”;
out.println(“Hello, ” + name);
%>

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

JSP Declaratives begins with <%! and ends %> with We can embed any
amount of java code in the JSP Declaratives. Variables and functions defined
in the declaratives are class-level and can be used anywhere on the JSP
page.

Example:

<%@ page import="java.util.*" %>


<html>
<body>
<%!
Date theDate = new Date();
Date getDate() {
System.out.println("In getDate() method");
return theDate;
}
%>
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

1.Save JSP file using the .jsp extension (e.g., hello.jsp).


2.Start server (e.g., Apache Tomcat).
3.Place your application inside the appropriate folder (e.g., webapps for
Tomcat).
4.Open the browser and enter the JSP page URL:
http://localhost:portnumber/YourApplicationContextRoot/jspfile

The JSP file is compiled and executed.

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.

You might also like