Transcript Document
Java Server Pages
Jeffrey Jongko
Introduction
Java Server Pages (JSP) technology was
created by Sun Microsystems and is built
on top of Sun’s Java Servlet technology
Java Servlets was Sun’s first answer for
developing web-based applications use
the Java programming language.
Introduction
Java Servlet technology suffered similar
maintenance problems that many traditional
web-technologies like CGI had (presentation and
logic are combined)
JSP is designed to facilitate development of
dynamic web sites by more easily dissociating
presentation from application logic
– allowing page developers to develop page
presentation without interfering with application logic
development
JSP Architecture
The purpose of JSP is to provide a
declarative, presentation-centric method
of developing servlets.
JSP specification is defined as a standard
extension on top the Servlet API.
Consequently, it should not be too
surprisingly that under the covers, servlets
and JSP pages have a lot in common.
JSP Architecture
Typically, JSP pages exist as simple text
files and are subject to a translation phase
and a request processing phase.
The translation phase is carried out only
once, unless the JSP page changes, in
which case it is repeated.
– The JSP page is transformed into a servlet
which subsequently processes all future
requests to the page
JSP Architecture
Sample JSP file
<html>
<body>
<center>
<%
String hello = "Hello World";
%>
<h1><%= hello %></h1>
</center>
</body>
</html>
The following JSP file is
saved to a file
HelloWorld.jsp
It looks like regular
HTML will special
regions delimited with
special markers like
“<%” and “%>” which
represent JSP features
JSP Syntax core elements
There are four basic core elements in JSP
– comments
– declarations
– expressions
– scriptlets
JSP Comments
There are 2 types of JSP comments
– HTML comment
– Hidden comment
HTML Comment Syntax
<!-- comment [ <%= expression %>] -->
Hidden Comment Syntax
<%-- comment --%>
A JSP HTML comment is a comment that is sent to the
client (appears on the page data)
JSP expressions (seen later) can be included inside an
HTML comment
Sample JSP Comments
<html>
<body>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<!-- Loaded on
<%=
DateFormat.getDateInstance(Date
Format.MEDIUM,
Locale.KOREAN).format(new
Date()) %> -->
<%
String hello = "Hello World";
%>
<h1><%= hello %></h1>
<%-- This will not appear --%>
</body>
</html>
DATA RECEIVED BY CLIENT:
<html>
<body>
<!-- Loaded on 2000-02-16 -->
<h1>Hello World</h1>
</body>
</html>
JSP Declarations
JSP Declarations are used to define variables
and methods that are visible to the whole page
– variables are translated into an instance variable in
the compiled servlet
JSP Declaration Syntax
<%! variable / method declaration %>
Variables declared in this way are not threadsafe. The JSP page has to be declared as
single-threaded if thread safety is needed for
these variables.
Sample JSP Declaration
<%!
int counter = 0;
boolean isSameString(String a,
String b)
{
return a.equals(b);
}
%>
<html>
<body> bjlee and hjk is
<%= (isSameString(“bjlee”,”hjk”))
? ”” : ”not” %>
same string
</body>
</html>
OUTPUT ON CLIENT:
<html>
<body>
bjlee and hjk is not same string
</body>
</html>
JSP Expressions
Scripting language expression that is
evaluated and converted into a String for
insertion into the output page
JSP Expression Syntax
<%= variable / method call %>
Sample JSP Expressions
<html>
<body>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<!-- Loaded on
<%=
DateFormat.getDateInstance(Date
Format.MEDIUM,
Locale.KOREAN).format(new
Date()) %> -->
<%
String hello = "Hello World";
%>
<h1><%= hello %></h1>
<%-- This will not appear --%>
</body>
</html>
DATA RECEIVED BY CLIENT:
<html>
<body>
<!-- Loaded on 2000-02-16 -->
<h1>Hello World</h1>
</body>
</html>
JSP Scriptlets
Scripting language code fragment that is
run within the service() method of the
compiled servlet
– variables declared within a scriptlet are local
unlike those declared by JSP Declarations
JSP Scriptlet Syntax
<% scripting language code %>
Sample JSP Scriptlet
<%@ page import=“java.util.*”%>
<html><body bgcolor=“white”>
<%
String name = “Byung Joon Lee”;
StringTokenizer st=
new StringTokenizer(name, “ “);
while ( st.hasMoreTokens() )
{
%>
<%= st.nextToken() %> <BR>
<%
}
%>
</body></html>
OUTPUT ON CLIENT:
<html><bodybgcolor=“white”>
Byung<BR>
Joon<BR>
Lee<BR>
</body></html>
<%@page %> Directive
<%@page %> directive defines attributes that
apply to a whole JSP page
Example of some attributes:
[
[
[
[
[
[
[
[
language=“java”]
extends=“package.class”]
import=“{package.class | package.*}, ...”]
session=“true|false”]
isThreadSafe=“true|false”]
info=“text”]
errorPage=“relativeURL”]
isErrorPage=“true|false”]
Implicit Objects
Data is passed to JSP pages from the outside via
HTTP POST or GET
This data is accessed via an implicit object
– the name of this object is called request of type
javax.servlet.ServletRequest
– implicit objects are accessed via the scriptlets
Other implicit objects exist such as
–
–
–
–
response of type javax.servlet.ServletResponse
pageContext
session
application
Other JSP features
Ability to access JavaBean components
using JSP tags, e.g.
– <jsp:useBean>
– <jsp:setProperty>
– <jsp:getProperty>
This allows access to JavaBean objects
without the use of scriptlets/Java code
Other JSP features
Ability to extend the usable JSP tags using a
custom tag library
– this is used to reduce the number of scriptlets on the
page by encapsulating their logic behind tags.
Both these features reduce the need for people
with actual Java language experience which is
needed for coding scriptlets
– allows for the development of presentation (JSP
page) and the actual business logic (JavaBeans) to
developed separately
Sites
Some sites that use Java Server Pages
– http://www.sun.com
– http://www.friendster.com
References
http://java.sun.com/products/jsp/
http://www.swpark.pe.kr/lecture/jsp.pdf
http://developer.java.sun.com/developer/o
nlineTraining/JSPIntro/
http://archive.coreservlets.com