Title: Java 2 Enterprises Edition (J2EE)
1Java 2 Enterprises Edition (J2EE)
- Java Server Pages (JSP) Technology
Jun Ni, Ph.D.M.E. Academic Technology,
ITS Department of Computer Science The
University of Iowa June 5, 2001
2What is JSP?
- Stands for Java Server Pages
- New technology for creating dynamic web content
to be with Java codes - Server-side computing
- Portable by supporting the JavaBean and
Enterprises JavaBean component models
3What is JSP?
- The base of JSP is Java Servlet technology
- Simplified Java Servlet programming
- Convert JSP to Servlet automatically
- De-couple business logic and presentation
- Does not do page layout design
4Review Static vs. Dynamic Web pages
- With a static Web page, the client requests a Web
page from the Web server, and the server responds
by sending back the requested file to the client.
Then client displays the file.
request
Server (maintain static Web page)
Client (Web browser)
response
5Review Static vs. Dynamic Web pages (cont.)
- In dynamic Web design, the content of the page is
generated at the time when it is requested, or
changed the content based on client requested
criteria. It is generated by application on the
server, receiving input from the client,
computing on the server, and responding back to
the client.
6Review Static vs. Dynamic Web pages (cont.)
Server (Web Application)
request
Client (Web browser)
Server (generate Web page)
response
7Other methods to create dynamic Web pages (CGI)
- Most prolific from of Web application in use
today. - Requests to be sent to an external program, in
Perl, C/C, or other languages - Shortcomings using CGI
- Large amount of system resources
- Independent from Web server
- Not portable to other platforms
- FastCGI (http//www.fastengines.com/)
- Apache Web server (http//perl.apache.org/)
- PerlEx (http//www.activestate.com/plex/)
8Other methods to create dynamic Web pages (Server
APIs)
- Web server specific API
- Netscapes Web Application Interface (WAI)
- Microsofts Internet Information Server (ISAPI)
- Tremendous performance speed and resources
- Limited to particular platform and Web server
- Server API extension can create several security
issues, since it is as part of the Web server,
which could cause the Web server to crash.
9Other methods to create dynamic Web pages
(Client-side scripting)
- Merits
- Very useful to create client-side scripting
- Computing is performed on the clients machine
- More secure and efficient
10Other methods to create dynamic Web pages
(Client-side scripting)
- drawbacks
- Client needs to support the scripting language in
exactly the ways expected - Differences between web browsers due to the
interpretation of client-side scripts. - Various scripts (JavaScript (Nescape,
Jscript(MS), VBScript (MS), ECMAScript
(ECMA-262). - Can not access server-side database
- JavaScript and JSP plays a different rule
- The best technology is to combine them together.
11Other methods to create dynamic Web pages
(Server-side scripting)
- Scripts run on the server before the page is sent
to user - Netscapes Server Side JavaScript (SSJS)
- Script is executed on the server to modify HTML
pages and scripts are pre-compiled to improve
server performance
12Other methods to create dynamic Web pages
(Server-side scripting)
- Microsofts Active Server Pages (ASP)
- Very similar to JSP
- Allows developer to embed VBScript or Jscript
code directly into a Web page. - The ASP pages have to be compiled every time they
run. - Only available to MSs Internet Information
Server 3.0 or later - Platform dependent.
13Other methods to create dynamic Web pages (Java
applets)
- Java applets are pre-designed by Web developers
- They can be executed of client-side of machine.
- They can be used for certain dynamic features
such as multimedia and interactive Web
application - Can not applied to database or file system
- Performance depends on clients machines
- Loading Byte files through internet.
14Other methods to create dynamic Web pages (Java
servlets)
- Opposite to Java applets, which are small Web
applications run on the client side, Java
servlets are powerful alternative to CGI programs
and scripting languages - Easy to be ported to any environment that
supports the Servlet API. - Bypassing the security problems that effect the
server APIs, since they run in Java VM. - Scalable and supported by all major Web servers
- Drawback Java programming
- See J2EEs Java Servlet technology
15Mechanism of JSP
- JSP is extremely powerful fro Web Applications
- JSP is a technology using server-side script that
is actually translated into Servlets and compiled
before they are run. - It provides developers a scripting interface to
create powerful Java Servlets - JSP provides tags that allows developers to
perform most dynamic content operations without
writing complex java code. - Advanced Java Programmers can perform advanced
operations in JSP pages
16Mechanism of JSP
- On a server page, the client requests a Web page,
server replaces some sections of a template with
new data, and send this newly modified page to
client. - JSP has special tags "lt" and "gt" to note the
start and end of a JSP section, respectively.
17Mechanism of JSP
Server Page Engine
request
Client (Web browser)
Template Page
response
Generate Web Page
18Power of JSP
- Write once and run anywhere
- No AWT and Swing
- Full power of Java API including networking,
multithreading, database connectivity,
internationalization, image manipulation, object
serialization, RMI, CORBA, JNDI, Mail API, Java
classes, JavaBeans and Enterprise JavaBeans - Communicate with Java Applet directly for
advanced client server application development
19Power of JSP
- Security and safety (avoid memory issues with
automatic garbage collections and absence of
pointers established exception Java security
manager) - Scalability
- Extensibility (Java servlet extensions support
other scripting languages close relationship to
XML - Components JavaBean component framework
20Power of JSP
- How to run your JSP on your web server?
- You need to download JRUN from Allaire
(Macromedia). Web site http//www.jrun.com/ - You can also download from Apache software (
foundation's (http//www.apache.org/ ) Javarta
project web site at http//jakarta.apache.org/
21HelloWorld.jsp
lthtmlgt ltheadgt lttitlegtHelloWorld JSP
Versionlt/titlegt lt/headgt ltbodygt Hellow World from
JSP! lt/bodygt lt/htmlgt
That is nothing but html syntax! You can write
.jsp file instead of .html file
URL http//localhost8100/HelloWorld.jsp
22simpleDate.html (static)
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Final//EN"gt ltHTMLgt ltHEADgt ltTITLEgtA simple date
examplelt/TITLEgt lt/HEADgt ltBODY COLORffffffgt The
time on the server is Friday June 8 151209 PST
2001 lt/BODYgt lt/HTMLgt
URL http//localhost8100/simpleDate.html
23simpleDate.jsp (dynamic)
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Final//EN"gt ltHTMLgt ltHEADgt ltTITLEgtA simple date
examplelt/TITLEgt lt/HEADgt ltBODY COLORffffffgt The
time on the server is lt new java.util.Date()
gt lt/BODYgt lt/HTMLgt
URL http//localhost8100/simpleDate.jsp
24HelloWorldLoop.jsp
lthtmlgt ltheadgt lttitlegtGreetingslt/titlegtlt/headgt ltbod
ygt lt for(int i0ilt5i) gt lth1gtHello
World!lt/h1gt lt gt lt/bodygt lt/htmlgt
Interesting to embedded Java into html syntax
URL http//localhost8100/HelloWorld.jsp
25JSP HelloWorld Lopp in Servlet version
Alternatively you can explicitly write your code
in term of servlet Java code, for example
26Example Servlet based Java codeHelloWorldServlet
Loop.java
import javax.servlet. import javax.servlet.http.
import java.io. public class
HelloWorldServletLoop extends HttpServlet
public void service (HttpServletRequest req,
HttpServletResponse res) throws
IOException PrintWriter out
res.getWriter() res.setContentType("text/ht
ml") out.println("ltHTMLgt ltBODYgt ")
for(int i0ilt10i) out.println("Hello
World!ltBRgt") out.println("ltBODYgt lt/HTMLgt
") out.close()
27Compile HelloWorldServletLoop.java URL
http//localhost8100/servlet/HelloWorldServletLoo
p
28Scripting Elements of JSP
JSPs combine HTML with special tags that define
dynamic behaviors. The tags have the following
features
- Scriptlets (embedded into HTML page)
- Expressions (access value of Java code)
- Declarations (define methods and variables for
initialization) - Directives (apply global information to the page)
- Actions (information for translation phase)
29Scripting Elements of JSP
- Different formats for JSP elements (Tag formats)
- JSP syntax lt script code gt (recommended)
- XML standard syntax ltjspelement /gt
30JSP's Scriptlet
- JSP syntax
- lt script code gt
- ltjspscriptletgt code lt/jspscriptletgt
- Small bits of Java code inserted into JPS pages
- Scriplets may contain single or multiple
statements - Scriplets may be combined with original HTML to
provide dynamic control - Scriplets use "lt" and "gt" tags
- For example
31Example date.jsp
lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Final//EN"gt ltHTMLgt ltHEADgt ltTITLEgtCurrent date is
displayed using JSP's Scriptletlt/TITLEgtlt/HEADgt ltB
ODYgt lth1gt Current date is displayed using JSP's
Scriptlet lt/h1gt The current date is lt
out.println(new java.util.Date()) gt
lt/BODYgt lt/HTMLgt
Small bits of Java code
URL http//localhost8100/date.jsp
32Example ScriptletHelloWorld.jsp
lthtmlgt ltheadgt lttitlegtDynamic HelloWorld JSP
Versionlt/titlegt lt/headgt ltbodygt ltcentergtltfont
size5gtHelloWorld using JSP Scriptle
versionlt/fontgtlt/centergt ltbrgt The content of the
Web materials is based on if-else with 50
randomization. ltbrgt Either Albert Gore or Patrick
Buchanan. ltpgt In order to test, please keep
re-loading or re-flashing.
33Example ScriptletHelloWorld.jsp
ltpgt ltdouble chadMath.random() if (chadlt0.5)
gt Albert Gore lt else gt Patrick
Buchanan ltgt ltbrgt lt/bodygtlt/htmlgt
Dynamic control
http//localhost8100/ScriptletHelloWorld.jsp
34JSP's Expressions
- Expression JSP allows developers to insert the
value of a Java expression directly into a web
page. - Expression do not end with a semi-colon and must
evaluate to a basic type or String - It uses "" sign.
Scriptlet lt out.println(Math.random() )
gt Expression lt math.radom() gt
35lt!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0
Final//EN"gt lt! public java.util.Date
PrintDate() return(new
java.util.Date()) gt ltHTMLgt ltHEADgt ltTITLE
gtCurrent Datelt/TITLEgt lt/HEADgt ltBODYgt The current
date is lt PrintDate() gt lt/BODYgt lt/HTMLgt
36JSP's Declaration
- Declaration is used to declare methods and
variables that are initialized with the page.
37JSP's Directives
- Directive contains global information that is
applicable to the whole page.
38JSP's Action
- Provide information for the translation phase of
the JSP page and consist of a set of standard,
built-in methods
39JSP Reference
- W3C (http//www.w3.org/)
- Apache software foundation (http//www.apache.org)
- Sun Java resources (http//www.javasoft.com)
- Tomcat, small-scale web server (http//jakarta.apa
che.org) - Jrun server (http//www.jrun.com)
40JSP Reference
- Javasofts JSP web site (http//java.sun.com/produ
cts/jsp/index.html) - Tomcats FQ (http//java.sun.com/products/jsp/tom
cat/faq.html) - JSPs Tagkib
- (http//jakarta.apache.org/taglibs)