Title: Java Servlet
1Java Servlet
- Presented by Hsin-Yi Wu
- http//www.albany.edu/hw8054
- March 13, 2000
2Agenda
- What are Servlets?
- Comparison of CGI, Server Extension APIs, and
Servlet - Servlet Basics
- Support for Servlet
3What 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.
4Examples of Web Applications
- Create dynamic content for a web page
- keyword search on a document archive
- electronic storefront
5Comparison of CGI,Server Extension APIs, and
Servlet
6CGI
- 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
7CGI 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
8Server 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
9Server Extension APIs life cycle
Request for Server Extension 1
Request for Server Extension 2
Request for Server Extension 1
10Server Extension APIs (continued)
- difficult to develop and maintain
- pose significant security and reliability hazards
- by crashed server extension
11Java 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)
12Servlet life cycle
Servlet1
Servlet2
13Interface Response
From the May 5, 1998 issue of PC Magazine
14Comparison 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
15Comparison 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
16Comparison 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
17Support for Servlet
18Tools 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
19Servlet 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
20Servlet 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
21For a list of available servlet engines, see the
official list at
- Http//jserv.java.sun.com/products/java-server/ser
vlets/environments.html
22Servlet Basics
- Java servlets are the first standard extension to
Java,including two packages - javax.servlet
- javax.servlet.http
23Servlet Interface
Servlet interface void init(ServletConfig sc)
throws ServletException void
service(ServletRequest req, ServletResponse
res) throws
ServletException, IOException void
destroy()
24Servlet Interface
Servlet interface void init(ServletConfig sc)
throws ServletException void
service(ServletRequest req, ServletResponse
res) throws
ServletException, IOException void
destroy()
25Servlet Framework
26A GenericServlet handling a request
GenericServlet subclass
Server
request
service( )
response
27An HTTP servlet handling GET and POST request
HttpServlet subclass
Server
Get request
doGet( )
response
service( )
Post request
doPost( )
response
28Servlet life cycle
Init()
Servlet1
service()
Servlet2
29import 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")
30Saying Hello with JSP (hello.jsp)
ltHTMLgt ltHEADgtltTITLEgtHellolt/TITLEgtlt/HEADgt ltBODYgt ltH
1gt lt out.println(Hello World) gt lt/H1gt lt/BODY
gt ltHTMLgt
31For reference, see my web page http//www.albany.e
du/hw8054
32(No Transcript)