0% found this document useful (0 votes)
41 views29 pages

Unit IV (JSP)

JSP (JavaServer Pages) is a server-side technology developed by Sun Microsystems for creating dynamic web applications by embedding Java code in HTML. It addresses the limitations of servlets by separating business logic from presentation, allowing for easier collaboration between developers and web designers. JSP utilizes a lifecycle that includes translation, compilation, initialization, execution, and cleanup, and supports various features such as directives, scriptlets, expressions, and custom tags.

Uploaded by

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

Unit IV (JSP)

JSP (JavaServer Pages) is a server-side technology developed by Sun Microsystems for creating dynamic web applications by embedding Java code in HTML. It addresses the limitations of servlets by separating business logic from presentation, allowing for easier collaboration between developers and web designers. JSP utilizes a lifecycle that includes translation, compilation, initialization, execution, and cleanup, and supports various features such as directives, scriptlets, expressions, and custom tags.

Uploaded by

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

Unit IV (JSP)

Definition: JSP is a server-side technology used to


create dynamic, platform-independent web
applications.
It is developed by Sun Microsystems.
It allows embedding Java code in HTML pages.
JSP is Launched in 1999 as part of the Java EE
platform.
Developed to simplify the process of creating web
applications by separating business logic from
presentation.
Problems with Servlets
• Here are the disadvantages for using Servlet:
• Thorough Java programming knowledge is needed to develop
and maintain all aspects of the application, since the processing
code and the HTML elements are combined together.
• Changing the look and feel of the application, or adding
support for a new type of client (such as a WML client),
requires the servlet code to be updated and recompiled.
• It's hard to take advantage of web-page development tools
when designing the application interface. If such tools are used
to develop the web page layout, the generated HTML must
then be manually embedded into the servlet code, a process
which is time consuming, error prone, and extremely boring.
• Why to use JSP?
• Separating the request processing and business logic from presentation
makes it possible to divide the development tasks among people with
different skills.
• Java programmers implement the request processing and business logic
pieces,
• web page authors implement the user interface, and
• Both groups can use best development tools for the task at hand. The result
is a much more productive development process.
• It also makes it possible to change different aspects of the application
independently, such as changing the business rules without touching the
user interface.
• This model has clear benefits even for a web-page author without
programming skills, working alone.
• A page author can develop web applications with many dynamic features,
using the JSP standard actions and the JSTL libraries, as well as Java
components provided by open source projects and commercial companies.
MVC Architecture
• Model–view–controller (MVC) is a software design
pattern commonly used for developing user interfaces
that divides the related program logic into three
interconnected elements. These elements are:
• Model,
• View,
• Controller.
• Model: Handles data logic.
• View: It displays the information from the model to
the user.
• Controller: It controls the data flow into a model
object and updates the view whenever data changes.
• Why Use JSP?
• Platform-independent.
• Combines HTML and Java for dynamic content
generation.
• Simplifies the creation of interactive web
applications.
• Integrates well with other Java technologies
(e.g., Servlets).
JSP Architecture

• Client sends an HTTP request.


• Web server (e.g., Apache Tomcat) passes the
request to JSP.
• JSP processes the request using embedded
Java code.
• Generates dynamic content (HTML) as the
response.
• Sends the HTTP response back to the client.
JSP Lifecycle

Translation: JSP is converted into a servlet by the web


server.

Compilation: The servlet is compiled into bytecode.

Initialization: The servlet is initialized.

Execution: The servlet processes requests and generates


responses.

Cleanup: Resources are released


Basic Syntax
• JSP tags are embedded in HTML using
• <% %> for scriptlets,
• <%= %> for expressions, and
• <%! %> for declarations.

• Example:jsp
• <html>
• <body>
• <h1>Current Date and Time:</h1>
• <%= new java.util.Date() %>
• </body>
• </html>
The Anatomy of a JSP Page
• A JSP page consists of various elements that
combine to create a dynamic web page.
• These elements include
directives,
scriptlets,
expressions,
declarations,
comments,
standard actions, and more.
JSP Directives
• Directives provide global information that
affects the entire JSP page. They are usually
placed at the top of the JSP file.
Syntax:
<%@ directive attribute="value" %>
• Common directives are:
1. page
2. include
3. taglib
• Page Directive: Defines page-dependent attributes, such as scripting
language, content type, error pages, etc.
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

• Include Directive: Includes a file during the translation phase.


<%@ include file="header.jsp" %>

• Taglib Directive: Declares a tag library, enabling the use of custom tags.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

The <%@ taglib %> directive is a powerful feature in JSP that allows
developers to leverage custom tags to encapsulate functionality, promote
code reuse, and maintain cleaner and more readable JSP pages.
By using tag libraries like JSTL or creating custom tags, developers can
significantly enhance the functionality and maintainability of their web
applications.
Scriptlets

• Scriptlets allow embedding Java code in the HTML.


The code inside scriptlets is inserted into the
service method of the generated servlet.
• <%
• String name = request.getParameter("name");
• if (name != null) {
• out.println("Hello, " + name + "!");
• }
• %>
Expressions

• Expressions are used to output values directly


to the client's browser.
• They are evaluated at runtime and the result
is converted to a string and placed in the
response.
• <%= new java.util.Date() %>
Declarations

• Declarations define methods or variables that get


inserted into the servlet class but outside of the
service method, making them available throughout the
JSP.
• <%!
• private int counter = 0;
• public int getCounter() {
• return counter++;
• }
• %>
Comments

• Comments in JSP can be hidden from the


client or sent to the client. There are two
types:
• JSP Comments: Not sent to the client.
• <%-- This is a JSP comment --%>
• HTML Comments: Sent to the client.
• <!-- This is an HTML comment -->
Standard Actions

• Standard actions provide an abstraction for common tasks,


like including content or forwarding requests.

• Include Action: Includes content at request time.


<jsp:include page="footer.jsp" />
• Forward Action: Forwards the request to another resource.
<jsp:forward page="login.jsp" />
• Use Bean Action: Instantiates or locates a bean.
<jsp:useBean id="user" class="com.example.User"
scope="session" />
• Expression Language (EL)
• Expression Language simplifies access to data
stored in JavaBeans components, various
objects in the request, session, and
application scopes.
${user.name}
• Custom Tags
Custom tags are defined in tag libraries (taglibs)
and allow you to encapsulate complex
behavior in a simple, reusable tag.
<mytag:customTag attribute="value" />
Example of a Complete JSP Page
<%@ page language="java" contentType="text/html;
charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%!
String message = "Welcome to my website!";
%>
<html>
<head>
<title>My JSP Page</title>
</head>
<body>
<h1><%= message %></h1>
<p>Current date and time: <%= new java.util.Date() %></p>
<%
String userName = request.getParameter("userName");
if (userName != null) {
out.println("Hello, " + userName + "!");
}
%>
<!-- This comment is visible in the client source -->
<%-- This comment is not visible in the client source --%>
<jsp:include page="footer.jsp" />
</body>
</html>
page directive attributes
• The <%@ page %> directive in JSP is used to
define various attributes that affect the
behavior and characteristics of the JSP page.
Here are some commonly used attributes of
the page directive:
• language: Specifies the scripting language
used in the page. The default is "java".
• <%@ page language="java" %>
• extends: Specifies the superclass of the
generated servlet. Use with caution, as it can
impact the default behavior of the servlet.
• <%@ page
extends="com.example.MySuperClass" %>
• import: Specifies the packages or classes to be
imported for use in the JSP page. Multiple
imports can be separated by commas.
• <%@ page import="java.util.*,
java.text.SimpleDateFormat" %>
• import: Specifies the packages or classes to be
imported for use in the JSP page. Multiple
imports can be separated by commas.
<%@ page import="java.util.*, java.text.SimpleDateFormat" %>

• session: Specifies whether the JSP page


participates in an HTTP session. The default is
true.
<%@ page session="true" %>
• buffer: Specifies the buffer size for the
JspWriter object. The default is 8kb.
<%@ page buffer="16kb" %>
• autoFlush: Specifies whether the buffer
should be automatically flushed when it is full.
The default is true.
<%@ page autoFlush="true" %>
• isThreadSafe: Specifies whether the JSP page
is thread-safe. The default is true, meaning the
container can send multiple requests to the
page at the same time. Setting it to false
means that the container will send requests
one at a time.
<%@ page isThreadSafe="false" %>
• errorPage: Specifies the URL of another JSP
page to handle errors thrown by the current
page.
<%@ page errorPage="error.jsp" %>
isErrorPage: Specifies whether the current page
is an error page that can handle exceptions.
The default is false.
<%@ page isErrorPage="true" %>
• contentType: Specifies the MIME type and
character encoding for the response. The
default is "text/html; charset=ISO-8859-1".
<%@ page contentType="text/html; charset=UTF-8" %>
• pageEncoding: Specifies the character
encoding for the JSP page itself. This affects
the way the JSP file is read and processed.
<%@ page pageEncoding="UTF-8" %>
• info: Provides a description of the JSP page.
This information can be retrieved using the
servlet’s getServletInfo method.
<%@ page info="This is a sample JSP page" %>
• language: Specifies the scripting language
used in the page. The default is "java".
<%@ page language="java" %>
• isELIgnored: Specifies whether the Expression
Language (EL) should be ignored. The default
is false.
<%@ page isELIgnored="true" %>
Example that includes several of these attributes:
• <%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*, java.text.SimpleDateFormat"
session="true" buffer="16kb" autoFlush="true" isThreadSafe="true"
errorPage="error.jsp" isErrorPage="false" info="Sample JSP Page" %>
• <html>
• <head>
• <title>Example JSP Page</title>
• </head>
• <body>
• <h1>Welcome to the Example JSP Page</h1>
• <p>Current date and time: <%= new java.util.Date() %></p>
• </body>
• </html>

You might also like