Anda di halaman 1dari 19

JSP Course

Mohammed Habeeb Vulla


Outline

Introducing Java Server Pages (JSP TM)


JSP scripting elements
Expressions, Scriptlets and declarations
The JSP page Directive:
Structuring Generated Servlets
Including Files in JSP Documents
Using JavaBeans components with JSP
Creating custom JSP tag libraries
Integrating servlets and JSP with the
MVC architecture

Mohammed Habeeb Vulla


What Is JSP???

JSP is a java based technology used to


simplify the development of dynamic
web pages. JSP is used to separate
dynamic content of web page from its
content with the help of HTML and JSP
tags.

Mohammed Habeeb Vulla


JSP Model Architecture

Mohammed Habeeb Vulla


JSP Model Architecture

Mohammed Habeeb Vulla


The JSP Framework

Idea:
Use regular HTML for most of page
Mark servlet code with special tags
Entire JSP page gets translated into a servlet (once), and
servlet is what actually gets invoked (for each request)
Example:
JSP
Thanks for ordering
<I><%= request.getParameter("title") %></I>
URL
http://host/OrderConfirmation.jsp?title=Core+Web+Programming
Result
Thanks for ordering Core Web Programming

Mohammed Habeeb Vulla


How JSP works?

1. Web browser send JSP request


2. JSP request send via Internet
to the web server
3. The web server send the JSP
8. HTML send back to the browser file (template pages) to JSP
servlet engine
4. Parse JSP file
5. Generate servlet source code
6. Compile servlet to class
7. Instantiate servlet
Mohammed Habeeb Vulla
Five Stages Of JSP Life Cycle

1. Page Translation stage


2. Page compilation stage
3. Loading & initialization stage
4. Request handling stage
5. Destroying stage

Mohammed Habeeb Vulla


JSP page translation and processing phases

Translation phase

Hello.jsp
Read
Request
helloServlet.java
Generate

Response
Client Server
Execute
helloServlet.class

Processing phase
Mohammed Habeeb Vulla
Template Pages

Server Page Template Resulting HTML


<html> <html>
<title> <title>
A simple example A simple example
</title> </title>

translation
<body color=#FFFFFF> <body color=#FFFFFF>
The time now is The time now is
<%= new java.util.Date() %> Tue Nov 5 16:15:11 PST 2002
</body> </body>
</html> </html>

Mohammed Habeeb Vulla


Introduction Comments

JSP Comments
<%-- {CODE HERE} --%>
Does not show the comments on the page
Does not show the comments in page source
Can only be used outside JSP Elements

HTML Comments
<! {CODE HERE} -->
Does not show the comments on the page
Shows the comments in page source
Can only be used outside JSP Elements
Single line Comments
// {CODE HERE}
When put outside JSP Elements, shows comments on the page & source
When put inside Scriptlets/Declarations, does not show on page &
source
Can be used inside scriptlets/declarations and outside JSP Elements

11
JSP Components

There are three main types of JSP


constructs that you embed in a page.
Scripting elements
You can specify Java code
Expressions, Scriptlets, Declarations
Directives
Let you control the overall structure of the servlet
Page, include, Tag library
Actions
Enable the use of server side Javabeans
Transfer control between pages

Mohammed Habeeb Vulla


JSP Elements Intro

Need to write some Java in your HTML?

Want to make your HTML more dynamic?

JSP Declarations :: Code that goes outside the service method

JSP Scriptlets :: Code that goes within the service method

JSP Expressions :: Code inside expressions is evaluated

JSP Directives :: Commands given to the JSP engine

13
JSP page consist of elements and templates data .These elements are described in
different elements type are:-
Directives:- JSP directives provides the page resources and their properties. Some
are as:-
1. Page directive:-Used to provide page specific properties
Eg:<%@page contentType=text/html language=java %>
Attributes are:-
1. Autoflush=true/false
2. Buffer=in KB
3. contentType=info
4. errorPage=page url
5. extends=classname
6. import=package separated by comma
7. info=infotext
8. isELIgnored=true/false
9. isErrorPage=pagenameurl
10.isThreadSafe=true/false
11.language=java
12.pageEncoding=ISO-8859-1
13.session=true/false
Mohammed Habeeb Vulla
2. Include Directives:-
The include directives tells the container to add the defined resource
content inline to the JSP page during the translation time .This tag used
to make reusability.
Ex :-
<%@ include file=header.html %>

3. Taglib Directives:-
Taglib tag is used to include external tag library in your web page.

Attributes are:-
1.Uri=url of taglibrary
2.tagDir=tag library directory
3.prefix=like object for library

Mohammed Habeeb Vulla


How many scripting Elements used
in JSP?????
1. <%! This is a declaration %>
2. <% this is a scriptlet %>
3. <% = this is an expression %>

Mohammed Habeeb Vulla


Structure of a web project
There are two kind of structures
The structure of the web application in a server
The structure of the IDE

A web project have three main elements


The JSPs files
The java classes
The Configuration file web.xml

Mohammed Habeeb Vulla


Structure of a web project
in the server

Mohammed Habeeb Vulla


Structure of a web project
in the server

Root of the project

Java classes (.class)

Java Libraries (.jar)

Configuration files

Anything web-related
- Directories
- JavaServer Pages (JSP)
- HTML
- Css files
- JavaScript Files
- Etc.

Mohammed Habeeb Vulla

Anda mungkin juga menyukai