JSPs

advertisement
Comp2513
Java Server Pages
Daniel L. Silver, Ph.D.
Objectives





Introduce Java Server Pages and their advantages
over Servlets
To understand how JSPs work: The JSP
Operational Model
To understand the basic components of JSPs
To understand how JSPs work with Java Beans
Review examples of JSPs
– Web Reference:
» JSP Intro
» Servlet and JSP Programming with IBM Websphere – Ch. 5
– Paper Reference: EJP – Chapter 10
2001
Daniel L. Silver
2
The Problems With Java Servlets





The presentation code (HTML) and business logic
are closely coupled within java servlet
Large amount of source code required to display
simple HTML of web-page
If HTML presentation needs to be changed then
servlet source code must be modified
If business logic needs to be changed then it must
be found within the mix of HTML and Java
E-Commerce Web developers are typically not
Java programmers and visa versa
2001
Daniel L. Silver
3
JSPs – Java Server Pages

JSPs – a relatively new Java programming model
from SUN (similar to Netscape Server-side Javascript
and MS Active Server Pages, but more powerful)



Developed to separate the dynamic content (Java)
of pages from the more static HTML content
I.E.: application processing (business logic) code
from the HTML presentation code
Looks like HTML but allows execution of Java*
within the HTML file or access to component
objects (Java Beans)
* the specification allows for coding/scripting languages other than
Java
2001
Daniel L. Silver
4
Skeleton of a JSP
<HTML>
<HEAD>
Standard HTML Code
<TITLE> HelloJSP1</TITLE>
</HEAD>
<BODY>
<H1>Hello JSP - Example 1 </H1>
<%-- JSPComments -- >
<% Scriptlets %>
Special JSP Tags
<%! Declarations %>
<!– JSP Action Commands -- >
<jsp:useBean
Java Object Created
id="textProvider"
from a JavaBean
class="exampleBean.HelloJSP1"
scope="request">
</jsp:useBean>
<!- Expressions -- >
Calling a method
<B><%= textProvider.getTextMessage() %></B>
of the Java Object
</BODY>
</HTML>
2001
Daniel L. Silver
5
How useful are JSPs?
Web page developers design and create the
web-pages, incorporating styles and
aesthetics. (Presentation Layer) The
dynamic content is left to the Java code.
 The JSP file can call other servlets and
perform the business logic features of the
site. The Java programmer can concentrate
on the functionality (Application Layer) of
the web site.

2001
Daniel L. Silver
6
Advantages of JSPs

Separation of dynamic and static content
– Web developer creates and maintains the HTML
content
– Java programmer creates and maintains dynamic
content and business logic

Platform independence
– Standards promote portability (but …beware)

Component reuse
– Promotes the use of JavaBeans and Enterprise
JavaBeans
– Speeds up website development and support
2001
Daniel L. Silver
7
Advantage of JSPs

Scriptlets and Tags
– JSP Scriptlets
» Small pieces of Java code embedded directly in
HTML
– JSP Tags
» Provide an easy way to embed and modify JavaBean
properties for specific use
» Issue JSP directives and other actions
2001
Daniel L. Silver
8
The JSP Operational Model
1.
2.
3.
4.
5.
6.
7.
8.
2001
HTTP server receives request for .jsp file
The .jsp file is located (can be in any directory)
Tomcat is called and passed the .jsp file
Tomcat invokes a special servlet called pageCompile
(the JSP engine, comes as part of JSP distribution)
pageCompile builds a servlet based on JSP code (that
may include a reference to a JavaBean)
The servlet .java and .class files are written to disk
(internal to Tomcat)
The .class file is processed by the JVM within Tomcat
like any other servlet
HTML code is generated by the servlet and returned
Daniel L. Silver
9
Tomcat Java Servlet
Request Processing
Client
http://eagle.acadiau.ca/store05/HelloWorld.jsp
Browser
HelloWorld.java
6 HelloWorld.class
8
1
HTTP
Server
HTML
mod_jserv
3
Internet
Tomcat
App.
Server
7
pageCompile 4
5
2
../Store05/WEB-INF/classes/exampleBean/HelloJSP1
../Store05/HelloWorld.jsp
HelloJSP1.class
HelloWorld.jsp
2001
Daniel L. Silver
10
Components of JSPs
JSP are composed of standard HTML tags
and JSP tags
 The standard set of JSP tags can be
categorized as follows:

–
–
–
–
–
2001
Comments
Directives
Declarations
Scriptlets
Expressions
Daniel L. Silver
11
JSP Comments

HTML comment will appear in the output
stream to the browser
<!-- comments … -- >

JSP comment will not be processed by the
JSP compiler
<%-- comments … --%>
2001
Daniel L. Silver
12
Directives
JSP directive is a global definition for the
JSP engine (compiler) that normally appears
at the top of the JSP page
 Page directive

<%@ page language=“java” %>
<%@ page import=“java.util.*” %>

Include directive
<%@ include file=“copyright.html” %>
2001
Daniel L. Silver
13
Declarations

A declaration block defines Java variables and
methods that are accessible to other code within
the page (such as an expression block)
<%! Declaration %>

Example:
<%!
private int getDateCount = 0;
private String getDate(GergorianCalendar gcl)
{ …. method code …};
%>
2001
Daniel L. Silver
14
Expressions

An expression is a scriptlet fragment that
produces a result & emits it as String object.
<%= expression %>
Used to generate dynamic text, prevents
having to use println() statements
 Example:

<%= getDate(new GregorianCalendar()) %>
<%= incrementCounter() %>
2001
Daniel L. Silver
15
JSP Scriptlets

Java code fragments can be embedded within a
JSP to provide small pieces of logic that
dynamically manipulate HTML and do not
warrant creation of a JavaBean
<% scriptlet code %>

Example:
<% if (Calendar_Time.AM_PM== Calendar.AM)
{ %>
How are you this morning,
<% } else { %>
How are you this afternoon,
<% } %>
2001
Daniel L. Silver
16
Access to Implicit Objects

When writing scriptlets or expressions there
is often a need to access standard Java
objects:
– request - access to request object
» Example of use: request.getParemeter()
– response – access to response object
» Example of use: response.setHeader()
– out – output stream writer
» Example of use: out.println()
2001
Daniel L. Silver
17
Putting it all Together
DateDisplay is a basic JSP that you can
begin to learn from
 Here is a link to the JSP source (view in source mode)
 Here is a link to the Java source resulting
from the JSP compilation

2001
Daniel L. Silver
18
THE END
danny.silver@acadiau.ca
Download