Title: Java Server Pages
1Java Server Pages
- Can web pages be created specially for each user?
- What part does Java play?
2Java Server Pages
- Looking at .
- Introduction to JSP
- Static web content
- Dynamic web content
- CGI
- Servlet overview
- Introduction to JSPs
- JSP advantages
- How a JSP works
3Java Server Pages
- What is a Java Server Page (JSP)?
- Java Server Pages is a Java-based technology that
simplifies the process of developing dynamic web
sites. - Java Server Pages are text files that contain
HTML along with embedded code that allows Java
code to be run on the server.
4Static Web Content
Web Server
Browser
Browser requests index.htm from server
5Dynamic Web Content
Web Server
Browser
1
Browser requests sample.asp from server
Server recognizes request as script or program
2
3
program
Program runs, getting information about the
request from the server, interacts with server
resources, and generates response (HTML tags)
that is sent back to browser
4
Browser displays HTML it received from server.
6CGI
- First standard for generating dynamic web content
was Common Gateway Interface (CGI). - CGI specifies mechanism for servers to pass
request information to external programs (i.e.,
external to the server software). - These programs were then run on server to
generate responses sent back to browser. - Perl scripting language was popular choice, but
could be in any language
7CGI
- Principal problem with CGI is that each time a
browser requests a CGI URL, the web server has to
execute a separate instance/process of the CGI
application.
Browser
CGI App 1
Web Server
Browser
CGI App 1
Browser
CGI App 1
Browser
CGI App 1
Browser
CGI App 1
- Why is this a problem - Does not scale well
with large numbers
8Server process for CGI
Source Duane Fields and Mark Kolb, Web
Development with JSP (Manning Publications,
2000), p. 4
9Template Systems
- Due to the inefficiencies with CGI, other dynamic
content systems have been developed that avoid
spawning separate processes for each request. - Microsoft's ASP (Active Server Pages), Allaire's
ColdFusion, PHP, and Netscape's Server-Side
JavaScript. - All of these systems using scripting languages.
- These languages are interpreted by the server
rather than compiled using a compiler. - Advantage rapid development times for
developers. - Disadvantage slower execution speed.
- All are template systems.
- That is, scripts embedded within HTML.
- HTML for static elements, scripts for dynamic
elements.
10Servlets
- 1996 Sun introduced Servlets as small Java-based
applications for adding dynamic functionality to
web servers. - Servlets have programming model similar to CGI
scripts in that they are given an HTTP request
from a web browser as input, and are expected to
construct the appropriate content for the
server's response. - Unlike CGI, Servlets do not spawn a new process
for each request. - Instead, all the Servlets run inside a single
process that runs the Java Virtual Machine.
11Server process using Servlets
Source Duane Fields and Mark Kolb, Web
Development with JSP (Manning Publications,
2000), p. 8
12Servlets
- One method for creating dynamic web sites via
Servlets is to write Java code that outputs HTML
data. - Unfortunately, any change in design of web page,
no matter how minor, requires of intervention of
Java programmer, who must compile the code (and
perhaps turn off server to deploy).
13JSP
- Java Server Pages were created later (1999) by
Sun to provide a simpler system for creating
dynamic web pages. - It uses the template approach of embedding
programming code (that is run on the server) in
the HTML page. - It also uses the ColdFusion approach of unique
HTML-like tags that interact with Java objects on
the server.
14JSP
lthtmlgtltheadgtlttitlegtHellolt/titlegtlt/headgt ltbodygt Hel
lo ltPgt lt for (int i0 ilt5 i) gt Value of I
is lt i gt ltBRgt lt gt lt/bodygtlt/htmlgt
lthtmlgtltheadgtlttitlegtHellolttitlegtlt/headgt ltbodygt Hell
o ltPgt lt for (int i0 ilt5 i)
out.print("Value of I is "i"ltBRgt")
gt lt/bodygtlt/htmlgt
lthtmlgtltheadgtlttitlegtUsing JSP Tagslt/titlegtlt/headgt lt
bodygt The browser you are using is lt
request.getHeader("User-Agent") gt ltjspuseBean
id'clock' scope'page' class'dates.JspCalendar'
type"dates.JspCalendar" /gt ltPgtYear is
ltjspgetProperty name"clock" property"year"/gt lt/
bodygtlt/htmlgt
15JSP Advantages (I)
- Since it uses Java, JSP enjoys advantages of Java
(cross-platform, object-oriented, standard API
libraries, etc). - Better performance than CGI.
- JSP requests are executed within a single Java
servlet process/container. - Because all servlet and JSP requests share a
single process they can share resources
16JSP Advantages (II)
- JSP pages become compiled into class files by the
servlet container, and thus (theoretically) may
be quicker to execute than interpreted template
systems. - As we will see, however, there are more steps in
first-time processing a JSP page then an ASP
page, so that frequently-changed JSP pages are
significantly slower to execute.
17JSP Advantages (III)
- Able to use JavaBeans to create object-oriented,
component-based web applications. - ASP has same ability via ActiveX components
created in Visual Basic (VB) or C - A component is a stand-alone object representing
a collection of properties and behaviors. - JavaBean properties and methods are accessed via
HTML-like tags. - Ideally components are reusable and
self-contained. - Typically used to separate presentation from
implementation/logic.
18JSP and J2EE
- JSP is part of the Java Server API called the
Java 2 Enterprise Edition (J2EE). - JSP, along with Servlets, form the presentation
layer of J2EE web applications.
19JSP and J2EE
J2EE Application Server
Servlets
JSP
Servlets
JSP
Servlets
JSP
Presentation layer
Web container
Session Beans
Entity Beans
Session Beans
Entity Beans
EJB container
Session Beans
Entity Beans
Logic and data layers
JDBC
RMI
JAF
Service layer
JNDI
JMS
JavaMail
Source Simon Brown et al, Professional JSP , 2nd
Edition (Wrox, 2001), p. 8
20JSP Container
- To run JSP, software implementing a JSP container
is required. - Tomcat which runs with Apache, IIS, etc
- Oracle, IBM, Sun, etc's J2EE Application Server
software also contains a JSP container.
21How JSPs work
- When a request for JSP page is received, the Page
Compiler container will parse the JSP page and
turn it into Java Servlet source code (.java
file). - However, if compiled servlet code for that JSP
page already exists (and isnt older than the JSP
page), this step and the next step are skipped. - The Java Servlet source code is then compiled by
the Java compiler into a .class file.
22How JSPs work (contd)
- The servlet (the .class file) is then loaded.
- The servlet will run (being interpreted by the
JVM), interact with server resources, and
generate responses (i.e., HTML) sent back to the
browser.
23JSP Server process
24JSP reference
- Check out
- http//java.sun.com
- Contains many FAQ
- Examples
- Good web resource
25Summary
- We have looked at
- Static and Dynamic web content
- Dynamic web content
- Introduction to CGI
- Servlet overview
- Introduction to JSPs
- advantages
- How a JSP works