Title: Building Web Applications Using Servlets
1IA308 Servlets and JSPs
Christophe Coenraets Manager, Technical
Evangelists Internet Applications
Division ccoenrae_at_sybase.com
2Servlets Topics
- Dynamic Web Sites Architectures
- Servlets Benefits
- Servlet's Life Cycle
- Building a Servlet
- Tracking Sessions
3Site Map
ListDest.jsp
Destination.jsp
TripFinder.jsp
LogonServlet
Logon.htm
Payment.jsp
LogonError.htm
Booking.jsp
Customer.jsp
4Static Web Site
http//www.myvacation.com/ListDest.htm
HTTP Server
HTTP
HTML
5Dynamic Page Generation Using CGI
http//www.myvacation.com/cgi/ListDest.exe
CGI Application
HTTP Server
ODBC / Native
HTTP
HTML
6Dynamic Page GenerationUsing Servlets
http//www.myvacation.com/servlet/ListDest
Servlet Container
HTTP Server
JDBC
HTTP
HTML
7What is a Servlet?
- Server side Java program that extends the
functionality of a Web Server - Used to dynamically generate HTML documents
- Comparable to
- CGI
- Netscape NSAPI
- Microsoft ISAPI
- Apache Modules
8Servlets Benefits
- Written in pure Java
- Platform independent
- Can take advantage of JDBC, EJB, JMS, JavaMail,
JavaIDL, RMI, ... - Server independent
- Scalability
- Dont start new process for each request
- Can run in same server process as HTTP server
- Multi-threaded
9Building a ServletUsing the Servlet API
- Extend HttpServlet
- Code Servlet's life cycle Methods
Servlet
GenericServlet
HttpServlet
LogonServlet
10Servlet Life Cycle
Servlets Container
HTTP Server
init( )
11Servlet Life Cycle
Servlets Container
HTTP Server
init( )
doGet( )
Servlet
12Servlet Life Cycle
Servlets Container
HTTP Server
init( )
doGet( )
Servlet
13Servlet Life Cycle
Servlets Container
HTTP Server
init( )
doGet( )
destroy( )
Servlet
14Servlet Life Cycle
Servlets Container
HTTP Server
15Advanced Servlet Support in PowerJ 3.6
- Java Servlet 2.2 wizard
- Generates Servlet class
- Deployment descriptors
- Web Archive (WAR) target (optional)
- Design time visual class for drag-and-drop
programming - Automated deployment
- Remote debugging
16Advanced Servlet Support in EA Server 3.6
- Access to connection caches
- Configurable threading model
- Access to external parameters
- Inter-server component invocations
- Refresh
- Remote debugging
- Logging support
17Building the Logon ServletDemo
ListDest.jsp
LogonServlet
Logon.htm
LogonError.htm
18Servlets as Part of J2EE
- Contribution
- Provides platform/server independent and scalable
architecture for developing HTTP-based
applications - Limitations
- Modification of static content requires
recompilation - Business logic not reusableby non Web clients
Addressed by JSP
Addressed by EJB
19Java Server PagesTopics
- JSP Architecture
- Anatomy of a JSP
- Directives
- Scripting Elements
- Standard Action Tags
20Site Map
ListDest.jsp
Destination.jsp
TripFinder.jsp
LogonServlet
Logon.htm
Payment.jsp
LogonError.htm
Booking.jsp
Customer.jsp
21Dynamic Page GenerationUsing Servlets
http//www.myvacation.com/servlet/ListDest
Servlet Container
HTTP Server
JDBC
HTTP
HTML
EA Server
22Dynamic Page GenerationUsing Java Server Pages
(JSP)
http//www.myvacation.com/ListDest.jsp
JSPEngine
HTTP Server
JDBC
HTTP
HTML
Servlet Container
EA Server
23Java Server Pages
- Web scripting technology
- Creation of dynamic content using static
templates - Combine markup (HTML or XML) with Java Code to
produce a dynamic Web Page - Similar to Microsoft ASP and PowerDynamo
- Uses Java as its scripting language
- Full power of the Java language
- Simple tags allow non Java developer to generate
dynamic content
24A Simple JSP
- lthtmlgt
- ltbodygt
- lt_at_ page language"java" import"" gt
- ltH1gtWelcome back,
- lt
- String user (String) session.getAttribute("user
") - out.println(user)
- gt
- lt/H1gt
- lt/bodygt
- lt/htmlgt
25Java Server Pages and Servlets
- Servlets
- HTML enclosed in Java code
- JSP
- Java code enclosed in HTML
- Easier to Author
- Better tool support
26How it Works
User Request
Server
FileChanged ?
Create Source
Compile
Execute Servlet
27Web Application Development Roles
- JSP technology promotes application partitioning
and separation of tasks - Java developer builds components encapsulating
the application logic - Page designer assembles the application with a
few method calls
28Anatomy of a JSP
- Static template text
- Dynamic content
- Directives
- Scripting elements
- Standard actions
- Custom tags
29Directives
- Gives page information to JSP engine
- Page directive
- lt_at_ page language"java" import"java.sql."
errorPage"ErrorPage.jsp" gt - Include directive
- lt_at_ include file"header.htm" gt
30Creating ListDest.jspDemo
31Scripting Elements
- Declarations (variables and methods)
- lt! String destination gt
- Scriptlets
- lt destination request.getParameter("destin
ation")gt - Expressions
- ltpgtDestination lt destination gt
32Standard Action Tags
- ltjspforwardgt
- Forwards request to HTML page, JSP, or servlet
- ltjspforward page"relativeUrl" /gt
- ltjspincludegt
- Includes data in a JSP page from another file
- ltjspinclude page"relativeUrl" flush"true" /gt
- ltjspplugingt
- Downloads Java plugin to browser to execute
applet or bean
33Standard Action TagsUsing JavaBeans Component
- Locates or instantiates a Bean with a specific
name and scope - ltjspuseBean id"cd" class"samples.Cd" /gt
- Sets a property value or values in a Bean
- ltjspsetProperty NAME"cd" PROPERTY"artist"
VALUE"Sting" /gt - Gets the value of a Bean property
- artistltjspgetProperty NAME"cd"
PROPERTY"artist" /gt
34Using Custom Tags
- Taglib directive
- Defines a custom tag library
- Provides a way to extend JSP's native
functionality without breaking compatibility - Syntax
- lt_at_ taglib uri"URIToTagLibrary"
prefix"tagPrefix" gt
35Creating Destination.jspDemo
36JSP Advanced Support in PowerJ 3.6
- HTML and JSP editor with syntax highlighting
- Design time syntax checking
- Automated deployment (WAR file)
- Remote debugging
37JSP Advanced Support in EA Server 3.6
- Remote debugging
- Access to connection caches
- Configurable threading model
- Access to external parameters
- Inter-server component invocations
- Refresh
- Logging support
38Summary