0% found this document useful (0 votes)
6 views33 pages

Week 5 - Java Server Pages

Java Server Pages (JSP) is a server-side technology used for creating dynamic web applications by embedding Java code within HTML. It supports features like easy integration with Java, component reuse, and separation of presentation from business logic. JSP allows developers to create interactive web content, manage user input, and access databases through a clear and familiar syntax.

Uploaded by

Betselot Kidane
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)
6 views33 pages

Week 5 - Java Server Pages

Java Server Pages (JSP) is a server-side technology used for creating dynamic web applications by embedding Java code within HTML. It supports features like easy integration with Java, component reuse, and separation of presentation from business logic. JSP allows developers to create interactive web content, manage user input, and access databases through a clear and familiar syntax.

Uploaded by

Betselot Kidane
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

Java Server

Pages
(JSP)
▪JSP technology
▪JSP files
▪Why JSP
contents
Java Server Pages
▪ It is used for creating web application.
▪ It’s server side technology
▪ Used to create dynamic web content
▪ a technology which is used to develop web pages by inserting Java code into
the HTML pages by making special JSP tags.
▪ The JSP tags which allow java code to be included into it are
<% —-java code—-%>.
Creating a simple JSP Page: [Link]
<%@ page language="java" contentType="text/html;
charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
<body>
<%[Link]("Hello World"); %>
</body>
</html>
What can you do with JSP?
▪ collect input from users through Webpage forms,
▪ present records from a database or another source,
▪ create Web pages dynamically.
▪ retrieving information from a database or registering user preferences,
▪ accessing JavaBeans components,
▪ passing control between pages, and
▪ sharing information between requests, pages, etc.
Features of JSP
• Easy integration with Java code: JSP allows you to embed Java
code directly into HTML using special tags, making it easy to create
dynamic content.
• Reuse of components: JSP supports the use of custom tags and
JavaBeans, allowing for modularity and reusability of code.
• Separation of presentation and logic: It encourages separation of
business logic from the presentation layer.
Why JSP?
▪ Separation of concerns
JSP allows the separation of presentation (HTML/CSS) from Java code.
This promotes a clearer distinction between the UI and the backend logic,
enhancing maintainability.
▪ Familiar syntax
JSP uses a syntax similar to HTML, making it easier for web designers and
front-end developers to collaborate with backend developers.
▪ Reusable components
JSP allows for the creation of reusable components and templates,
facilitating the development process and promoting consistency across the
application.
▪ Dynamic content
It enables dynamic content generation by embedding Java code within
HTML pages, allowing for the creation of dynamic and interactive web
applications.
Basic syntax
• <%@ directive %>: This is used to give instructions to the JSP
container. For example, directives like page, include, taglib, etc.
• <%! declaration %>: Declarations contain variables or methods that
are available throughout the JSP.
• <%= expression %>: Expressions are used to insert Java values
directly into the output.
• <% scriptlet %>: Scriptlets contain Java code that is executed when
the page is processed.
JSP page
▪ A text-based document capable of returning dynamic content to a client
browser
▪ Contains both static and dynamic content
▪ Static content: HTML, CSS, Image and JS
▪ Dynamic content: programming code, and JavaBeans, custom tags
▪ Enables separation of business logic from presentation.
Presentation is in the form of HTML
Business logic is implemented as JavaBeans or Action Tags
▪ Builds on Servlet technology
JSP Elements
▪ Static content (HTML, CSS, JS, AngularJS, ..etc)
▪ Scriptlets (Expression, Declaration and Scriptlet)
▪ Directives(page, include and import)
▪ JSP Actions (Stanndard, JSTL, and Custom)
Static content
▪ HTML
defines the structure and content of web pages, providing the foundation for
JSP to generate dynamic content within its structure.
▪ CSS
styles HTML elements, enhancing the presentation and layout of
JSP-generated content, allowing for visual enhancements and consistency
across pages.
▪ JavaScript (client-side scripting)
enables client-side interactivity and behavior within JSP-generated pages,
facilitating dynamic updates or interactions without reloading the entire
page.
▪ jQuery
a JavaScript library that simplifies DOM manipulation and event handling,
often used in JSP to streamline client-side scripting tasks and enhance
interactivity.
Scriptlets
▪ Scriptlets (Expression, Declaration and Scriptlet)

A scriptlet in JavaServer Pages (JSP) is a snippet of Java code


embedded within the JSP page.

It's enclosed within <% %> tags and allows developers to dynamically
generate content or perform logic that gets executed when the JSP
page is processed on the server before being sent to the client's
browser.
▪ Scriptlets in JSP allow you to embed Java code directly within an HTML-like
structure. They are enclosed within <% %> tags and are used to execute Java
code snippets directly in the JSP file.
<%
String message = "Hello World";
[Link](message);
%>

•<% %> denotes the scriptlet.


•String message = "Hello World!"; is a Java statement defining a string
variable.
•[Link](message); uses the out object (implicit in JSP) to output the
content of the message variable to the HTML response.
Examples: Basic Arithmetic
<body>
<%
int num1 = 10;
int num2 = 5;
int sum = num1 + num2;
int product = num1 * num2;
%>
<p> Sum: <%= sum %> </p>
<p> Product: <%= product %> </p>
</body>
Example: Retrieving Request Parameters
………………………………………..HTML
<form action="add">
1st Number: <input type="text" name = "num1">
2nd Number: <input type="text" name = "num2">
<input type="Submit">
</form>

……………………………………………..JSP

<%
int n1 = [Link]([Link]("num1"));
int n2 = [Link]([Link]("num2"));

int k = n1 + n2;
[Link]("Output is: " + k);
%>
Forms of Scripting Elements
Expressions: <%= Expressions %>
Scriptlets: <% java code %>
Declarations: <%! Declarations %>
Expressions
▪ During execution phase
- Expression is evaluated and converted into a String
- The String is then inserted into the servlet’s output stream directly
- Results in something like [Link](expression)
- Can use predefined variable (implicit objects) within expression
▪ Format
<%= Expression %>
- Semi-colons are not allowed for expressions
▪ In JSP, expressions (<%= %>) provide a way to embed Java expressions
directly into the HTML content.

▪ Unlike scriptlets (<% %>), expressions are used to display the result of an
expression directly within the HTML code.
Examples
▪ Display current time using Date class
Current Time: <%= new Date()%>
▪ Display random number using Math class
Random Number:<%= [Link]()%>
▪ Use implicit objects
- Your hostname:<%= [Link]()%>
- Your parameter:<%= [Link](“username”)%>
- Your Server:<%= [Link]()%>
- Your Session ID:<%= [Link]()%>
Examples: Displaying variables
Displaying the value of a variable within
HTML content.
<body>
<%
String greeting = "Hello, World!";
%>

<p><%= greeting %></p>


</body>
Example: Arithmetic Operations
<body>
<%
int num1 = 10;
int num2 = 5;
%>
<p>Sum: <%= num1 + num2 %> </p>

<p>Product: <%= num1 * num2 %></p>


</body>
Example: Accessing Object properties
<body>
<%
class Person {
String name;
int age;
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
String getName() {
return name;
}
}
Person p1 = new Person("Alice", 30);
%>
<p>Name: <%= [Link]() %></p>
</body>
Examples: Calling methods and displaying the
returned values.
<body>
<%
String getMessage() {
return "Hello from a method!";
}
%>
<p><%= getMessage() %></p>
</body>
Scriptles
▪ Used to insert arbitrary Java code into servlet’s jspService() method
▪ Can do things expressions alone cannot do
- setting response headers and status code
- updating database
- executing code that contains loops, conditionals
▪ Can use predefined variables (implicit objects)

format

<% java code %>


<jsp:scriptlet> java code </jsp:scriptlet>
Examples
▪ Display query string
<% String queryData = [Link]();
[Link](queryData); %>
▪ Setting response type
<% [Link](“text/plain”); %>
Declarations
▪ Used to define variables or methods that get inserted into the main body of
servlet class
- outside of _jspService() method
- implicit objects are not accessible to declarations
▪ Usually used with Expressions or Scriptlets
▪ For initialization and cleanup in JSP pages, use declarations to override
jspInit() and jspDestroy() methods

format

<%! Method or variable declaration code %>


<jsp:declaration><%! Method or variable declaration code
%></jsp:declaration>
Example
<body>
<h1>Declaration example</h1>
<%!
private String randomHeading(){
return ("<h2>" + [Link]() + "</h2>");
}
%>
<%= randomHeading() %>
</body>
Lifecycle
▪ JSPs follow a lifecycle similar to servlets, including initialization, service, and
destruction.
Tags and expression language
• JSPs come with a set of predefined tags for various purposes such as
displaying data, controlling the flow, etc.
• Expression Language (EL) is used to easily access data stored in
JavaBeans components or other objects.
JSTL (JSP Standard Tag Library)
▪ JSTL provides a set of tags that encapsulate core functionality common to
many applications. These tags can be used to perform iteration, conditional
logic, and more.
References
▪[Link] [Link]#72

You might also like