Java Server Pages

Download Report

Transcript Java Server Pages

Java Server Pages
Server
- Apache Tomcat Server
Server-side scripts
- Java Server Pages
What are Java Server Pages?

Java Server Pages technology combines Java code
and HTML tags in the same document to produce a
JSP file.
Java
+
<HTML>
=
JSP
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.
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 commercialquality websites.

Apache Tomcat.
Typical html Request/Response cycle
1. requests URL for html page
server
client
3. sends html page to client
4. browser interprets
html page & displays
2. retrieves
html page
Request/Response for page - JavaScript commands
2. retrieves
html page
1. requests URL for html page
server
client
(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
with embedded
JavaScript
commands
Request/Response for file with Java Server Page parts
1 - requests
JSP page
2 - retrieves page
browser
server
3 - responds with
html+results in page
executes
server-side script
may have embedded
JSP server script &
Java-client-Script
SERVER-SIDE
SCRIPT
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
4. browser
displays page
* compiled first time only - thereafter uses compiled copy
experiment on effect of extensions like .jsp or .html
executes JSP code
replaces code with exec results
Scripts in Web Pages
1 - requests
JSP page
browser
server
3 - responds &
html + results
executes
client-side script
executes
server-side script
2 - retrieves page
using URL addr &
server configuration
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
.jsp page
retrieved is:
Making a Request for a JSP File
<html>
<head>
<title> current server time </title>
</head>
<font face = "Arial" size =4>
The current Date and time on the web server are:
<BR>
jsp instruction
<%= new java.util.Date() %>
- executed
on "server-side"
</font>
- result replaces code
Scripting Element
- view source shows
</body>
date that is printed,
</html>
not scripting element
Page on server has embedded jsp instruction
1. "source" as shown in browser
<html>
<head>
<title> current server time </title>
</head>
<font face = "Arial" size =4>
The current Date and time on the web server are:
<BR>
Wed Nov 27 20:27:02 EST 2002
</font>
</body>
</html>
2. Note how Date’s
text replaces original
JSP in page sent to
browser
Making a Request for Java Script HTML Page
requested source page is
same as displayed in browser
<HTML>
<HEAD>
<TITLE>Client-side script </TITLE></HEAD>
<BODY>
THE TIME ON THE CLIENT IS:
Current time is:
<%= new java.util.Date( ) %>
<script language="JavaScript" >
document.write (new Date() )
</script>
</BODY>
</HTML>
File type is html
Example: DateTime.html
Why not
executed
on server ?
sent to
browser
and
executed
on browser
Auto-Refresh Example
requested
every 5 sec
from current
directory
of original
request
<HTML>
<HEAD>
<TITLE> server-side scripts </TITLE>
<META HTTP-EQUIV = "REFRESH" CONTENT = "5, URL=CurrentDateTime.jsp">
</HEAD>
<BODY>
The time on the server is:
<%= new java.util.Date( ) %>
</BODY>
</HTML>
JSP result replaces this code
& is sent to browser
URL: http://acad.kutztown.edu:10001/JSP/CurrentDateTime.jsp
Deciphering the URL
URL: http://acad.kutztown.edu:10001/JSP/CurrentDateTime.jsp
Requested
File’s address
on the server
http://acad.kutztown.edu 10001
WHERE
IP address
of server
WHICH
port that
server
listens on
JSP
helloWorld.js
WHAT
Path from webapps
directory in Tomcat
installation
remainder of
file address path
Structure of a JSP file.

Four basic tags:
Scriplet
 Expression
 Declaration
 Definition

JSP Comments

Regular Comment


<!-- comment -->
Hidden Comment

<%-- comment --%>
Example.jsp:
<html>
<!-- Regular Comment -->
<%-- Hidden Comment --%>
</html>
<html>
<!-- Regular Comment -->
</html>
Declaration Element


Form: <%! Declaration %>
Used to declare class members:





variables
methods.
Declaratives only.
These declarations last as long as the class object is alive.
Example:
<%!
int x = 0;
int square(int x){
return x * x;
}
%>
Expression Elements



Form: <%= expression %>
An expression in this context is not a complete java
statement; it is just a part of it.
Examples:
<p> The square of <%= x%> is <%= square(x) %> as
calculated by a JSP program.</p>
<html>
<body>
<p><%= Integer.toString( 5 * 5 ) %></p>
</body>
</html>
<html>
<body>
<p>25</p>
</body>
</html>
Note: no semi-colon “;” following expression.
Scriptlets





A scriptlet is a piece of Java code sandwiched between <% and
%>
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:
<html>
<body>
<% for (int i = 0; i < 2; i++) { %>
<p>Hello World!</p>
<% } %>
</body>
</html>
<html>
<body>
<p>Hello World!</p>
<p>Hello World!</p>
</body>
</html>
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


request


used to handle the current session
the incoming request
response

the outgoing response
Processing HTML Forms


JSP eliminates manual parsing of data
submitted from a form on a client
browser.
Instead:

request.getParameter(“param-name”)
JSP Directives



Form: <%@ directive %>
There are three directives defined by
JSP; include, page, and taglib
Examples:
<%@ page language=“java”
import=“java.util.*”%>
<%@ include file=“filename”%>

See StatesDB JSP Example