Java Servlet - PowerPoint PPT Presentation

About This Presentation
Title:

Java Servlet

Description:

add-on. embeddable. Servlet Engines. Standalone servlet engines. built-in ... Add-on servlet engines. plug-in to an existing server. Embeddable servlet engines ... – PowerPoint PPT presentation

Number of Views:95
Avg rating:3.0/5.0
Slides: 33
Provided by: hom4226
Learn more at: http://www.cs.albany.edu
Category:
Tags: addon | java | servlet

less

Transcript and Presenter's Notes

Title: Java Servlet


1
Java Servlet
  • Presented by Hsin-Yi Wu
  • http//www.albany.edu/hw8054
  • March 13, 2000

2
Agenda
  • What are Servlets?
  • Comparison of CGI, Server Extension APIs, and
    Servlet
  • Servlet Basics
  • Support for Servlet

3
What are Servlets?
  • Java servlets are a key component of server-side
    Java development.
  • A servlet is a small, pluggable extension to a
    server that enhances the servers functionality.

4
Examples of Web Applications
  • Create dynamic content for a web page
  • keyword search on a document archive
  • electronic storefront

5
Comparison of CGI,Server Extension APIs, and
Servlet
6
CGI
  • CGI (Common Gateway Interface)
  • one of the first techniques for creating dynamic
    content
  • intented purpose
  • to define a standard method for a server to talk
    with external applications

7
CGI life cycle
Child Process for CGI1
Request for CGI 1
Request for CGI 2
Child Process for CGI2
Request for CGI 1
Child Process for CGI1
8
Server Extension APIs
  • Proprietary server extension APIsfor example
  • Netscape provides an internal API called NSAPI
  • Microsoft provides ISAPI
  • exist within the main process of a web server
  • Run extremely fast and make full use of the
    servers resources

9
Server Extension APIs life cycle
Request for Server Extension 1
Request for Server Extension 2
Request for Server Extension 1
10
Server Extension APIs (continued)
  • difficult to develop and maintain
  • pose significant security and reliability hazards
  • by crashed server extension

11
Java Servlets
  • similar to a proprietary server extension, except
    that it runs inside a Java Virtual Machine(JVM)
    on the server
  • safe and portable
  • handled by separate threads within the web server
    process
  • efficient and scalable
  • compromise between CGI and native-interface
    applications (ex ISAPI/NSAPI)

12
Servlet life cycle
Servlet1
Servlet2
13
Interface Response
From the May 5, 1998 issue of PC Magazine
14
Comparison of Dynamic Web Interfaces- CGI
  • Portability Fair
  • Performance Fair
  • Pros Can use familiar programming environment.
  • Cons Slow.
  • How it Works The Web server executes the CGI
    application as a separate process.

From the May 5, 1998 issue of PC Magazine
15
Comparison of Dynamic Web Interfaces-
NSAPI/ISAPI
  • Portability Poor
  • Performance Excellent
  • Pros Very fast.
  • Cons Native API. Programming bugs can crash the
    Web server.
  • How it Works The application is part of the Web
    server address space.

From the May 5, 1998 issue of PC Magazine
16
Comparison of Dynamic Web Interfaces- Java
Servlet
  • Portability Good
  • Performance Good
  • Pros Portable. Uses the JDK environment.
  • Cons New and unstable.
  • How it Works The Web server use Java Virtual
    Machine with a servlet add-on to interpret the
    Java applets.

From the May 5, 1998 issue of PC Magazine
17
Support for Servlet
18
Tools you need
  • Java Development Kit (JDK)
  • Java Servlet Development Kit(JSDK)
  • include packagesjavax.servletjavax.servlet.http
  • available from http//java.sun.com/products/servl
    et
  • Servlet Engine

19
Servlet Engines
  • Used to test and deploy your servlets
  • How to choose a servlet engine you need?
  • Depends on the web server(s) you are running
  • three flavors of servlet engines
  • standalone
  • add-on
  • embeddable

20
Servlet Engines
  • Standalone servlet engines
  • built-in support for servlets
  • hard to keep on with the latest version of servlet
  • Add-on servlet engines
  • plug-in to an existing server
  • Embeddable servlet engines
  • lightweight servlet deployment platform that can
    be embedded in another application

21
For a list of available servlet engines, see the
official list at
  • Http//jserv.java.sun.com/products/java-server/ser
    vlets/environments.html

22
Servlet Basics
  • Java servlets are the first standard extension to
    Java,including two packages
  • javax.servlet
  • javax.servlet.http

23
Servlet Interface
Servlet interface void init(ServletConfig sc)
throws ServletException void
service(ServletRequest req, ServletResponse
res) throws
ServletException, IOException void
destroy()
24
Servlet Interface
Servlet interface void init(ServletConfig sc)
throws ServletException void
service(ServletRequest req, ServletResponse
res) throws
ServletException, IOException void
destroy()
25
Servlet Framework
26
A GenericServlet handling a request
GenericServlet subclass
Server
request
service( )
response
27
An HTTP servlet handling GET and POST request
HttpServlet subclass
Server
Get request
doGet( )
response
service( )
Post request
doPost( )
response
28
Servlet life cycle
Init()
Servlet1
service()
Servlet2
29
import java.io. import javax.servlet. import
javax.servlet.http. public class
HelloWorldServlet extends HttpServlet
public void doGet (HttpServletRequest req,
HttpServletResponse res) throws
ServletException, IOException
res.setContentType("text/html") ServletOutput
Stream out res.getOutputStream() out.println("
lthtmlgt") out.println("ltheadgtlttitlegtHello
Worldlt/titlegtlt/headgt") out.println("ltbodygt") o
ut.println("lth1gtHello Worldlt/h1gt") out.println("
lt/bodygtlt/htmlgt")
30
Saying Hello with JSP (hello.jsp)
ltHTMLgt ltHEADgtltTITLEgtHellolt/TITLEgtlt/HEADgt ltBODYgt ltH
1gt lt out.println(Hello World) gt lt/H1gt lt/BODY
gt ltHTMLgt
31
For reference, see my web page http//www.albany.e
du/hw8054
32
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com