Java Server Pages: 6.1. Introduction To JSP
Java Server Pages: 6.1. Introduction To JSP
com/Gctpak
CIT-303 Web Development with Java Instructor Name:Ms.saba
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.
1. Translation
2. Compilation
3. Loading
4. Instantiation
5. Initialization
6. RequestProcessing
7. Destruction
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.
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 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: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
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