0% found this document useful (0 votes)
46 views7 pages

Java Server Pages: 6.1. Introduction To JSP

This document discusses Java Server Pages (JSP), which is a server-side technology used to create dynamic web content. JSP allows Java code to be embedded in HTML pages using JSP tags. It works by first converting JSP pages into servlets, which are then executed to process requests. The document covers the JSP lifecycle, elements like scriptlets and expressions, standard actions, and advantages/disadvantages of using JSP.

Uploaded by

Prince Jutt
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)
46 views7 pages

Java Server Pages: 6.1. Introduction To JSP

This document discusses Java Server Pages (JSP), which is a server-side technology used to create dynamic web content. JSP allows Java code to be embedded in HTML pages using JSP tags. It works by first converting JSP pages into servlets, which are then executed to process requests. The document covers the JSP lifecycle, elements like scriptlets and expressions, standard actions, and advantages/disadvantages of using JSP.

Uploaded by

Prince Jutt
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/ 7

www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.

com/Gctpak
CIT-303 Web Development with Java Instructor Name:Ms.saba

Chp.06 Java Server Pages


6.1. Introduction to JSP
 It stands for Java Server Pages.
 It is a server side technology.
 It is used for creating web application.
 It is used to create dynamic web content.
 In this JSP tags are used to insert JAVA code into HTML pages.
 It is an advanced version of Servlet Technology.
 It is a Web based technology helps us to create dynamic and platform
independent web pages.
 In this, Java code can be inserted in HTML/ XML pages or both.
 JSP is first converted into servlet by JSP container before processing the
client’s request.
Features of JSP:
 Coding in JSP is easy
 Reduction in the length of Code
 Connection to Database is easier
 Make Interactive websites .
 Portable, Powerful, flexible and easy to maintain
 No Redeployment and No Re-Compilation
 Extension to Servlet
Advantages of using JSP:
 It does not require advanced knowledge of JAVA
 It is capable of handling exceptions
 Easy to use and learn
 It can tags which are easy to use and understand
 It is suitable for both JAVA and non JAVA programmer
Disadvantages of using JSP:
 Difficult to debug for errors.
 First time access leads to wastage of time
 It’s output is HTML which lacks features.

www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
6.2. Life cycle of JSP
A Java Server Page life cycle is defined as the process started with its creation
which later translated to a servlet and afterward servlet lifecycle comes into
play. This is how the process goes on until its destruction.

Following steps are involved in JSP life cycle:

1. Translation
2. Compilation
3. Loading
4. Instantiation
5. Initialization
6. RequestProcessing
7. Destruction

1-Translation of JSP page to Servlet :

This is the first step of JSP life cycle. This translation phase deals with Syntactic
correctness of JSP. Here test.jsp file is transllated to test.java.

www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
2-Compilation of JSP page :

Here the generated java servlet file (test.java) is compiled to a class file
(test.class).

3-Classloading :

Servlet class which has been loaded from JSP source is now loaded into
container.

4-Instantiation :

Here instance of the class is generated. The container manages one or more
instance by providing response to requests.

5-Initialization :

jspInit() method is called only once during the life cycle immediately after the
generation of Servlet instance from JSP.

6-Request processing :

_jspService() method is used to serve the raised requests by JSP.It takes request
and response object as parameters.This method cannot be overridden.

7-JSP Cleanup :

In order to remove the JSP from use by the container or to destroy method for
servlets jspDestroy()method is used. This method is called once, if you need to
perform any cleanup task like closing open files, releasing database connections
jspDestroy() can be overridden.

6.3. Elements of JSP


The elements of JSP have been described below −
1-The Scriptlet
A scriptlet can contain any number of JAVA language statements, variable or
method declarations, or expressions that are valid in the page scripting language.
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
Following is the syntax of Scriptlet −
<% code fragment %>

You can write the XML equivalent of the above syntax as follows −
<jsp:scriptlet>
code fragment
</jsp:scriptlet>
2-JSP Declarations
A declaration declares one or more variables or methods that you can use in Java
code later in the JSP file. You must declare the variable or method before you
use it in the JSP file.
Following is the syntax for JSP Declarations −
<%! declaration; [ declaration; ]+ ... %>
Following is an example for JSP Declarations −
<%! int i = 0; %>
<%! int a, b, c; %>
<%! Circle a = new Circle(2.0); %>
3-JSP Expression
A JSP expression element contains a scripting language expression that is
evaluated, converted to a String, and inserted where the expression appears in
the JSP file
Following is the syntax of JSP Expression −
<%= expression %>
Following example shows a JSP Expression −

<html>
<head><title>A Comment Test</title></head>

<body>
<p>Today's date: <%= (new java.util.Date()).toLocaleString()%></p>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
</body>
</html>
4- JSP Comments
JSP comment marks text or statements that the JSP container should ignore. A
JSP comment is useful when you want to hide or "comment out", a part of your
JSP page.
Following is the syntax of the JSP comments −
<%-- This is JSP comment --%>
Following example shows the JSP Comments −

<html>
<head><title>A Comment Test</title></head>

<body>
<h2>A Test of Comments</h2>
<%-- This comment will not be visible in the page source --%>
</body>
</html>
5- JSP Directives
A JSP directive affects the overall structure of the servlet class. It usually has the
following form −
<%@ directive attribute="value" %>
6- JSP Actions
JSP actions use constructs in XML syntax to control the behavior of the servlet
engine. You can dynamically insert a file, reuse JavaBeans components, forward
the user to another page, or generate HTML for the Java plugin.
There is only one syntax for the Action element, as it conforms to the XML
standard −
<jsp:action_name attribute="value" />
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
6.4. JSP Standard Actions
JSP actions are special XML tags which control the behavior of servlet engine.
JSP actions allow you to insert a file dynamically
1 JSP:include action

JSP include action allows you to include a file at runtime. The syntax of JSP
include action is as follows:

<jsp:include page="Relative URL" flush="true" />


2 JSP:useBean action

JSP useBean action lets you load a JavaBean component into the page and use it
later. JSP useBean action allows you to reuse other Java classes. The syntax of
JSP useBean action is as follows:

<jsp:useBean id="objectName" class="package.class" />


3 JSP:forward Action

jsp:forward action allows you to forward a request to the other page. The
syntax of the jsp:forward action is listed as below. There is one attribute called
page which value is a page you want to forward the request to. You can specify
the page statically or dynamically by using the expression

<jsp:forward page="error.jsp" />


<jsp:forward page="<%= java-expression %>" />

4 JSP:plugin Action

jsp:plugin action allows you to embedded Java Applet into a page. Suppose you
have an applet which demonstrates the JSP page life
cycle called com.jsp.jspapplet. Here is the way we use jsp:plugin action
to embedded that applet into a page:

<html>
<head>
<title>jsp:plugin Demo</title>
</head>
<body>
www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab
www.SalmanAdeeb.wixsite.com/DAE-CIT-books www.facebook.com/Gctpak
<jsp:plugin type="applet"
code="com.jsp.jspapplet"
codebase="."
width="500"
height="400">
<jsp:fallback>
<p>Unable to use Java Plugin</p>
</jsp:fallback>
</jsp:plugin>

</body>
</html>

www.youtube.com/AdeebTechnologyLab www.facebook.com/Adeeb.Technology.Lab

You might also like