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

Chapter 1 JSP

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

Chapter 1 JSP

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

Welcome to JSP

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Faculty of Engineering & Computer Science

Department: Information Technology

Course: Java Server Page(JSP)

Semester: Seven

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Chapter 1: Introduction to

Java Server Page(JSP)

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Content of this chapter
1.1 Introduction

1.2 The Lifecycle of a JSP Page

1.3 Creating a simple JSP Page

1.4 JSP API

1.5 Environment setup for JSP

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.1 Introduction

• JSP (Jakarta Server Pages, formerly Java Server Pages) is a


technology that allows a web developer to mix textual content
with Java code that is executed on the
web server to render the final web page. In this way it is
similar to PHP except that it uses the Java programming
language. With JSP the developer can create dynamic pages
based on HTML, XML, SOAP(Simple Object Access Protocol) or
other document types.

• JSP was released in 1999 by Sun Microsystems.

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
1.1 Introduction

What is the difference between JSP and servlet?

•JSP (Java Server Pages) is a technology that allows embedding


Java code into HTML pages, while servlets are Java classes that
handle requests and responses on the server-side.

• Servlets are pure Java, whereas JSP combines Java code and
HTML for dynamic web page generation.

•A Servlet is a Java class that runs into a web container, and JSP
is a text document that contains both static and dynamic data.

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
1.1 Introduction

• JSP technology is used to create web application just like


Servlet technology. It can be thought of as an extension to
Servlet because it provides more functionality than servlet
such as expression language, JSTL, etc.

• A JSP page consists of HTML tags and JSP tags. The JSP pages
are easier to maintain than Servlet because we can separate
designing and development. It provides some additional
features such as Expression Language, Custom Tags, etc.

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
1.1 Introduction
Advantages of JSP over Servlet

There are many advantages of JSP over the Servlet.

They are as follows:

A. Extension to Servlet

JSP technology is the extension to Servlet technology. We


can use all the features of the Servlet in JSP.

In addition to, we can use implicit objects, predefined tags,


expression language and Custom tags in JSP, that makes
JSP development easy.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.1 Introduction
B. Easy to maintain JSP can be easily managed because we can easily
separate our business logic with presentation logic. In Servlet
technology, we mix our business logic with the presentation logic.

C. Fast Development: No need to recompile and redeploy

If JSP page is modified, we don't need to recompile and redeploy the


project. The Servlet code needs to be updated and recompiled if we
have to change the look and feel of the application.

D. Less code than Servlet: In JSP, we can use many tags such as action
tags, JSTL, custom tags, etc. that reduces the code. Moreover, we can
use EL, implicit objects, etc.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.2 The Lifecycle of a JSP Page
The JSP pages follow these phases:

•Translation of JSP Page

•Compilation of JSP Page

•Classloading (the classloader loads class file)

•Instantiation (Object of the Generated Servlet is created).

•Initialization ( the container invokes jspInit() method).

•Request processing ( the container invokes _jspService() method).

•Destroy ( the container invokes jspDestroy() method).

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.2 The Lifecycle of a JSP Page

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.2 The Lifecycle of a JSP Page
As depicted in the above diagram, JSP page is translated
into Servlet by the help of JSP translator.

The JSP translator is a part of the web server which is


responsible for translating the JSP page into Servlet.

After that, Servlet page is compiled by the compiler and


gets converted into the class file.

Moreover, all the processes that happen in Servlet are


performed on JSP later like initialization, committing
response to the browser and destroy.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.3 Creating a simple JSP Page
To create the first JSP page, write some HTML code as given below, and save it by .jsp
extension. We have saved this file as index.jsp. Put it in a folder and paste the folder
in the web-apps directory in apache tomcat to run the JSP page.

index.jsp Let's see the simple example of JSP where we are using the script let tag
to put Java code in the JSP page.
We will learn scriptlet tag later.
<html> <body>
<% out.print(2*5); %>
</body> </html>

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.3 Creating a simple JSP Page
It will print 10 on the browser.

How to run a simple JSP Page?

Follow the following steps to execute this JSP page:

•Start the server

•Put the JSP file in a folder and deploy on the server

•Visit the browser by the URL


http://localhost:portno/contextRoot/jspfile, for example,
http://localhost:8888/myapplication/index.jsp

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.3 Creating a simple JSP Page
Do I need to follow the directory structure to run a
simple JSP?

No, there is no need of directory structure if you don't


have class files or TLD (A tag library descriptor ) files.

For example, put JSP files in a folder directly and deploy


that folder. It will be running fine.

However, if you are using Bean class, Servlet or TLD file,


the directory structure is required.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.3 Creating a simple JSP Page
The Directory structure of JSP
The directory structure of JSP page is same as Servlet.
We contain the JSP page outside the WEB-INF folder or in
any directory.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.4 JSP API(application programming interface)

The JSP API consists of two packages:

•javax.servlet.jsp

•javax.servlet.jsp.tagext

javax.servlet.jsp package

The javax.servlet.jsp package has two interfaces and


classes.

The two interfaces are as follows:

•JspPage

•HttpJspPage
Instructor: Ibrahim Yusuf Mohamed(Bulbul)
Copyright © 2018 Pearson Education,
Inc.
1.4 JSP API(application programming interface)

The classes are as follows:

•JspWriter

•PageContext

•JspFactory

•JspEngineInfo

•JspException

•JspError

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
1.4 JSP API(application programming interface)

The JspPage interface

According to the JSP specification, all the generated


servlet classes must implement the JspPage interface.

It extends the Servlet interface.

It provides two life cycle methods.

Copyright © 2018 Pearson Education,

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Inc.
1.4 JSP API(application programming interface)

Methods of JspPage interface

•public void jspInit(): It is invoked only once during the life


cycle of the JSP when JSP page is requested firstly.

It is used to perform initialization. It is same as the init()


method of Servlet interface.

•public void jspDestroy(): It is invoked only once during the


life cycle of the JSP before the JSP page is destroyed.

It can be used to perform some clean up operation.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.4 JSP API(application programming interface)

The HttpJspPage interface

The HttpJspPage interface provides the one life cycle


method of JSP. It extends the JspPage interface.

Method of HttpJspPage interface:

public void _jspService(): It is invoked each time when


request for the JSP page comes to the container. It is used
to process the request. The underscore _ signifies that you
cannot override this method.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.4 JSP API(application programming interface)

The environment setup for JSP mainly consists of 3 steps:

A. Setting up the JDK.

B. Setting up the NetBeans

C. Setting up the webserver (Tomcat).

A. Setting up Java Development Kit

•This step involves downloading an implementation of the Java


Software Development Kit (SDK) and setting up the PATH environment
variable appropriately. You can download SDK from Oracle's Java site
− Java SE Downloads.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.5 Environment setup for JSP
B. Setting up the NetBeans
Step 0: Install JDK

To use NetBeans for Java programming, you need to first install Java
Development Kit (JDK). See "JDK - How to Install".

Step 1: Download

Download "NetBeans IDE" installer from


http://netbeans.org/downloads/index.html. There are many "bundles" available.
For beginners, choose the 1st entry "Java SE" (e.g., "netbeans-8.2-javase-
windows.exe" 95MB).

Step 2: Run the Installer

Run the downloaded installer.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.5 Environment setup for JSP
C. Setting up the webserver (Tomcat).

Apache Tomcat is an open source software implementation of the Java


Server Pages and Servlet technologies and can act as a standalone
server for testing JSP and Servlets, and can be integrated with the
Apache Web Server.

Here are the steps to set up Tomcat on your machine −

•Download the latest version of Tomcat from https://tomcat.apache.org/.

•Once you downloaded the installation, unpack the binary distribution


into a convenient location.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.5 Environment setup for JSP
After a successful startup, the default web applications
included with Tomcat will be available by
visiting http://localhost:8080/.
Upon execution, you will receive the following output −

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
1.5 Environment setup for JSP
Tomcat can be started by executing the following commands on the
Windows machine −

%CATALINA_HOME%\bin\startup.bat

or

C:\apache-tomcat-5.5.29\bin\startup.bat

Tomcat can be started by executing the following commands on the


Unix (Solaris, Linux, etc.) machine −
$CATALINA_HOME/bin/startup.sh
or
/usr/local/apache-tomcat-5.5.29/bin/startup.sh

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Answer the following question
1. When and who was released jsp?

2. What is the difference between JSP and servlet?

3. List Advantages of JSP over Servlet.

4. List The Lifecycle of a JSP Page

5. Do we need to follow the directory structure to run a simple JSP?

6. Define the Directory structure of JSP?

7. The JSP API consists of two packages List of them.

8. The javax.servlet.jsp package has two part, what are they?

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
Answer the following question
9. What are the two interfaces in javax.servlet.jsp?

10. List the classes of javax.servlet.jsp?

11. Tell Methods of JspPage interface.

12. Differentiate public void jspInit() and public void jspDestroyed()?

13. Define HttpJspPage interface?

14. List Method of HttpJspPage interface.

15. The environment setup for JSP mainly consists of 3 steps, list of
them.

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.
That is End

Any Questions
Thanks For your Listening

Instructor: Ibrahim Yusuf Mohamed(Bulbul)


Copyright © 2018 Pearson Education,
Inc.

You might also like