Building Web Applications Using Servlets - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Building Web Applications Using Servlets

Description:

Thread. 13. Servlet. Servlet Life Cycle. init( ) doGet( ) HTTP ... Web Archive (WAR) target (optional) Design time visual class for drag-and-drop programming ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 39
Provided by: christophe224
Category:

less

Transcript and Presenter's Notes

Title: Building Web Applications Using Servlets


1
IA308 Servlets and JSPs
Christophe Coenraets Manager, Technical
Evangelists Internet Applications
Division ccoenrae_at_sybase.com
2
Servlets Topics
  • Dynamic Web Sites Architectures
  • Servlets Benefits
  • Servlet's Life Cycle
  • Building a Servlet
  • Tracking Sessions

3
Site Map
ListDest.jsp
Destination.jsp
TripFinder.jsp
LogonServlet
Logon.htm
Payment.jsp
LogonError.htm
Booking.jsp
Customer.jsp
4
Static Web Site
http//www.myvacation.com/ListDest.htm
HTTP Server
HTTP
HTML
5
Dynamic Page Generation Using CGI
http//www.myvacation.com/cgi/ListDest.exe
CGI Application
HTTP Server
ODBC / Native
HTTP
HTML
6
Dynamic Page GenerationUsing Servlets
http//www.myvacation.com/servlet/ListDest
Servlet Container
HTTP Server
JDBC
HTTP
HTML
7
What 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

8
Servlets 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

9
Building a ServletUsing the Servlet API
  1. Extend HttpServlet
  2. Code Servlet's life cycle Methods

Servlet
GenericServlet
HttpServlet
LogonServlet
10
Servlet Life Cycle
Servlets Container
HTTP Server
init( )
11
Servlet Life Cycle
Servlets Container
HTTP Server
init( )
doGet( )
Servlet
12
Servlet Life Cycle
Servlets Container
HTTP Server
init( )
doGet( )
Servlet
13
Servlet Life Cycle
Servlets Container
HTTP Server
init( )
doGet( )
destroy( )
Servlet
14
Servlet Life Cycle
Servlets Container
HTTP Server
15
Advanced 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

16
Advanced 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

17
Building the Logon ServletDemo
ListDest.jsp
LogonServlet
Logon.htm
LogonError.htm
18
Servlets 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
19
Java Server PagesTopics
  • JSP Architecture
  • Anatomy of a JSP
  • Directives
  • Scripting Elements
  • Standard Action Tags

20
Site Map
ListDest.jsp
Destination.jsp
TripFinder.jsp
LogonServlet
Logon.htm
Payment.jsp
LogonError.htm
Booking.jsp
Customer.jsp
21
Dynamic Page GenerationUsing Servlets
http//www.myvacation.com/servlet/ListDest
Servlet Container
HTTP Server
JDBC
HTTP
HTML
EA Server
22
Dynamic Page GenerationUsing Java Server Pages
(JSP)
http//www.myvacation.com/ListDest.jsp
JSPEngine
HTTP Server
JDBC
HTTP
HTML
Servlet Container
EA Server
23
Java 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

24
A 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

25
Java Server Pages and Servlets
  • Servlets
  • HTML enclosed in Java code
  • JSP
  • Java code enclosed in HTML
  • Easier to Author
  • Better tool support

26
How it Works
User Request
Server
FileChanged ?
Create Source
Compile
Execute Servlet
27
Web 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

28
Anatomy of a JSP
  • Static template text
  • Dynamic content
  • Directives
  • Scripting elements
  • Standard actions
  • Custom tags

29
Directives
  • 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

30
Creating ListDest.jspDemo
31
Scripting Elements
  • Declarations (variables and methods)
  • lt! String destination gt
  • Scriptlets
  • lt destination request.getParameter("destin
    ation")gt
  • Expressions
  • ltpgtDestination lt destination gt

32
Standard 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

33
Standard 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

34
Using 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

35
Creating Destination.jspDemo
36
JSP Advanced Support in PowerJ 3.6
  • HTML and JSP editor with syntax highlighting
  • Design time syntax checking
  • Automated deployment (WAR file)
  • Remote debugging

37
JSP 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

38
Summary
Write a Comment
User Comments (0)
About PowerShow.com