Download JSP Intro

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Java Server Pages
• Can web pages be
created specially for
each user?
• What part does Java
play?
1
Java 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
2
Java 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.
3
Static Web Content
Web
Server
Browser
Browser requests index.htm from server
Server responds by sending index.htm
to browser
4
Dynamic Web Content
Web
Server
Browser
1
Browser requests sample.asp
from server
Server recognizes
request as script or
program
2
3
program
4
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
Browser displays HTML it
received from server.
5
CGI
• 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
6
CGI
• 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
Browser
Browser
Browser
Browser
Web
Server
CGI App 1
CGI App 1
CGI App 1
CGI App 1
CGI App 1
• Why is this a problem - Does not scale well with large
numbers
7
Server process for CGI
HTTP Server
Receive CGI
Request
CGI Process
Spawn CGI Process
Generate Response
Receive CGI Output
Send
Response
Source: Duane Fields and Mark Kolb, Web Development with JSP (Manning Publications,
2000), p. 4
8
Template 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.
9
Servlets
• 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
10
that runs the Java Virtual Machine.
Server process using Servlets
HTTP Server
Receive
Request
Servlet Container
Spawn Thread
Generate Response
Servlet
Send
Response
Source: Duane Fields and Mark Kolb, Web Development with JSP (Manning Publications, 11
2000), p. 8
Servlets
• 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).
12
JSP
• 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.
13
JSP
<html><head><title>Hello</title></head>
<body>
Hello <P>
<% for (int i=0; i<5; i++) { %>
Value of I is <%= i %> <BR>
<% } %>
</body></html>
<html><head><title>Hello<title></head>
<body>
Hello <P>
<%
for (int i=0; i<5; i++) {
out.print("Value of I is "+i+"<BR>");
}
%>
</body></html>
<html><head><title>Using JSP Tags</title></head>
<body>
The browser you are using is <%= request.getHeader("User-Agent") %>
<jsp:useBean id='clock' scope='page' class='dates.JspCalendar' type="dates.JspCalendar" />
<P>Year is <jsp:getProperty name="clock" property="year"/>
</body></html>
14
JSP 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
15
JSP 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.
16
JSP 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 selfcontained.
– Typically used to separate presentation from
implementation/logic.
17
JSP 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.
18
JSP and J2EE
J2EE Application Server
Web container
Servlets
Servlets
Servlets
JSP
JSP
JSP
EJB container
Session
Beans
Session
SessionBeans
Beans
Entity
Beans
Entity
EntityBeans
Beans
JDBC
RMI
JAF
JNDI
JMS
JavaMail
Presentation layer
Logic and data layers
Service layer
Source: Simon Brown et al, Professional JSP , 2nd Edition (Wrox, 2001), p. 8
19
JSP 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.
20
How 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 isn’t 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.
21
How 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.
22
JSP Server process
HTTP Server
Receive
Request
JSP Container
Page Compiler Servlet
JSP Servlet
Current?
No
Parse JSP
Yes
JSP Servlet
Loaded?
Yes
Generate JSP
Servlet Code
No
Load Servlet
Compile JSP Servlet
Servlet
Generate Response
JSP Page Servlet
Send
Response
23
JSP reference
• Check out
–
–
–
–
http://java.sun.com
Contains many FAQ
Examples
Good web resource
24
Summary
• 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
25