Java Server Pages - PowerPoint PPT Presentation

About This Presentation
Title:

Java Server Pages

Description:

Java Server Pages technology combines Java code and HTML ... Example: Countdown. Page Content Upon Arrival html head title current server time /title ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 24
Provided by: drdanielsp
Category:
Tags: countdown | java | pages | server

less

Transcript and Presenter's Notes

Title: Java Server Pages


1
Java Server Pages
Server - Apache Tomcat Server Server-side
scripts - Java Server Pages
2
What are Java Server Pages?
  • Java Server Pages technology combines Java code
    and HTML tags in the same document to produce a
    JSP file.

JSP


Java
ltHTMLgt
3
Why use JSP Technology?
  • Convenient
  • Integrates Java and HTML
  • Provides an extensive infrastructure for
  • Tracking sessions.
  • Managing cookies.
  • Reading and sending HTML headers.
  • Parsing and decoding HTML form data.
  • Efficient
  • Every request for a JSP is handled by a simple
    Java thread. Hence, the time to execute a JSP
    document is not dominated by starting a process.

4
Why use JSP technology?
  • Portable
  • JSPs follow a standardized API.
  • The Java VM, which is used to execute a JSP file,
    is supported on many architectures and operating
    systems.
  • Inexpensive
  • There are a number of free or inexpensive Web
    Servers that are good for commercial-quality
    websites.
  • Apache Tomcat.

5
Typical html Request/Response cycle
1. requests URL for html page
2. retrieves html page
client
server
3. sends html page to client
4. browser interprets html page displays
6
Request/Response for page - JavaScript commands
2. retrieves html page
1. requests URL for html page
client
server
with embedded JavaScript commands
(browser)
3. responds w. page to client
4. interprets page -
executes Java script commands
eg check for bad or missing data on form
CLIENT-SIDE SCRIPT
7
Request/Response for file with Java Server Page
parts
1 - requests JSP page
2 - retrieves page
may have embedded JSP server script
Java-client-Script
server
browser
3 - responds with htmlresults in page
SERVER-SIDE SCRIPT
executes server-side script
8
Request/Response for Java Server Page
1. sends URL for JSP page
2. retrieves page from storage
compiles embedded JSP code
client
server
3. sends html results to client
executes JSP code replaces code with exec results
4. browser displays page
compiled first time only - thereafter uses
compiled copy
experiment on effect of extensions like .jsp or
.html
9
Scripts in Web Pages
2 - retrieves page
using URL addr server configuration
10
Page Content Upon Arrival
JSP versus JavaScript - client v. server execution
1. Start Apache Tomcat server listening on
a port (often 8080)
2. Request a Java Server page from server
- source file will have Results Example
Add2Int (shortly)
3. Request html page with JavaScript -
source page will have the JavaScript Example
Countdown
11
.jsp page retrieved is
Making a Request for a JSP File
lthtmlgt ltheadgt lttitlegt current server time
lt/titlegt lt/headgt ltfont face "Arial" size
4gt The current Date and time on the web server
are ltBRgt lt new java.util.Date()
gt lt/fontgt lt/bodygt lt/htmlgt
  • jsp instruction
  • - executed
  • on "server-side"
  • result replaces code
  • view source shows date that is printed, not
    scripting element

Scripting Element
Page on server has embedded jsp instruction
12
1. "source" as shown in browser
lthtmlgt ltheadgt lttitlegt current server time
lt/titlegt lt/headgt ltfont face "Arial" size
4gt The current Date and time on the web server
are ltBRgt Wed Nov 27 202702 EST
2002 lt/fontgt lt/bodygt lt/htmlgt
2. Note how Dates text replaces original JSP in
page sent to browser
13
Making a Request for Java Script HTML Page
requested source page is same as displayed in
browser
ltHTMLgt ltHEADgt ltTITLEgtClient-side script
lt/TITLEgtlt/HEADgt ltBODYgt THE TIME ON THE CLIENT IS
Current time is lt new java.util.Date(
) gt ltscript language"JavaScript" gt
document.write (new Date() ) lt/scriptgt lt/BODYgt lt
/HTMLgt
Why not executed on server ?
sent to browser and executed on browser
File type is html
Example DateTime.html
14
from current directory of original request
Auto-Refresh Example
requested every 5 sec
ltHTMLgt ltHEADgt ltTITLEgt server-side scripts
lt/TITLEgt ltMETA HTTP-EQUIV "REFRESH" CONTENT
"5, URLCurrentDateTime.jsp"gt lt/HEADgt ltBODYgt The
time on the server is lt new java.util.Date( )
gt lt/BODYgt lt/HTMLgt
JSP result replaces this code is sent to browser
URL http//acad.kutztown.edu10001/JSP/CurrentDat
eTime.jsp
15
Deciphering the URL
URL http//acad.kutztown.edu10001/JSP/CurrentDat
eTime.jsp
Requested Files address on the server
http//acad.kutztown.edu
helloWorld.js
10001
JSP
WHERE
WHICH
WHAT
IP address of server
remainder of file address path
port that server listens on
Path from webapps directory in Tomcat installation
16
Structure of a JSP file.
  • Four basic tags
  • Scriplet
  • Expression
  • Declaration
  • Definition

17
JSP Comments
  • Regular Comment
  • lt!-- comment --gt
  • Hidden Comment
  • lt-- comment --gt

Example.jsp lthtmlgt lt!-- Regular Comment --gt
lt-- Hidden Comment --gt lt/htmlgt
lthtmlgt lt!-- Regular Comment --gt lt/htmlgt
18
Declaration Element
  • Form lt! Declaration gt
  • Used to declare class members
  • variables
  • methods.
  • Declaratives only.
  • These declarations last as long as the class
    object is alive.
  • Example
  • lt!
  • int x 0
  • int square(int x)
  • return x x
  • gt

19
Expression Elements
  • Form lt expression gt
  • An expression in this context is not a complete
    java statement it is just a part of it.
  • Examples
  • ltpgt The square of lt xgt is lt square(x) gt as
    calculated by a JSP program.lt/pgt

lthtmlgt ltbodygt ltpgtlt Integer.toString( 5
5 ) gtlt/pgt lt/bodygt lt/htmlgt
lthtmlgt ltbodygt ltpgt25lt/pgt lt/bodygt lt/htmlgt
Note no semi-colon following expression.
20
Scriptlets
  • A scriptlet is a piece of Java code sandwiched
    between lt and gt
  • Embeds Java code in the JSP document that will be
    executed each time the JSP page is processed.
  • A scriptlet can make use of any java API as long
    as it is appropriate for the purpose.
  • Variables defined in a scriptlet are local.
  • Example

lthtmlgt ltbodygt ltpgtHello World!lt/pgt
ltpgtHello World!lt/pgt lt/bodygt lt/htmlgt
lthtmlgt ltbodygt lt for (int i 0 i lt 2
i) gt ltpgtHello World!lt/pgt lt gt
lt/bodygt lt/htmlgt
21
Implicit Objects
  • A JSP container provides the tools necessary for
    a JSP document to interact with the environment
    surrounding it.
  • Three most commonly used implicit objects
  • Session
  • used to handle the current session
  • request
  • the incoming request
  • response
  • the outgoing response

22
Processing HTML Forms
  • JSP eliminates manual parsing of data submitted
    from a form on a client browser.
  • Instead
  • request.getParameter(param-name)

23
JSP Directives
  • Form lt_at_ directive gt
  • There are three directives defined by JSP
    include, page, and taglib
  • Examples
  • lt_at_ page languagejava importjava.util.gt
  • lt_at_ include filefilenamegt
  • See StatesDB JSP Example
Write a Comment
User Comments (0)
About PowerShow.com