View
218
Download
0
Tags:
Embed Size (px)
Citation preview
1
Java Server Pages
• Can web pages be created specially for each user?
• What part does Java play?
2
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
3
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.
4
Static Web Content
Server responds by sending index.htm to browser
Browser
Browser requests index.htm from server
Web Server
5
Dynamic Web ContentBrowser
1Browser requests sample.asp from server
Web Server
program
2
Server recognizes request as script or program
3
Program runs, getting information about the request from the server, interacts with server resources, and generates response (HTML tags) that is sent back to browser4
Browser displays HTML it received from server.
6
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
7
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
8
Server process for CGI
Receive CGIRequest
Spawn CGI Process
Receive CGI Output
SendResponse
Generate Response
HTTP Server
CGI Process
Source: Duane Fields and Mark Kolb, Web Development with JSP (Manning Publications, 2000), p. 4
9
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.
10
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 that runs the Java Virtual Machine.
11
Server process using Servlets
Source: Duane Fields and Mark Kolb, Web Development with JSP (Manning Publications, 2000), p. 8
ReceiveRequest
Spawn Thread
SendResponse
HTTP Server
Generate Response
Servlet Container
Servlet
12
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).
13
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.
14
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>
15
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
16
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.
17
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 self-contained.– Typically used to separate presentation from
implementation/logic.
18
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.
19
JSP and J2EE
ServletsServletsServletsJSPJSPJSP Presentation
layerWeb container
Session BeansSession BeansSession BeansEntity BeansEntity BeansEntity BeansEJB container Logic and data
layers
JDBC
JNDI
RMI
JMS
JAF
JavaMailService layer
J2EE Application Server
Source: Simon Brown et al, Professional JSP , 2nd Edition (Wrox, 2001), p. 8
20
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.
21
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.
22
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.
23
JSP Server processReceiveRequest
SendResponse
HTTP Server
JSP Container
Page Compiler Servlet
JSP ServletCurrent?
Parse JSP
JSP ServletLoaded?
Servlet
Generate JSPServlet Code
Load Servlet
Generate Response
JSP Page Servlet
Compile JSP Servlet
Yes No
No
Yes
24
JSP reference
• Check out– http://java.sun.com– Contains many FAQ– Examples– Good web resource
25
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