Java and Distributed Computing

indexadjustmentInternet και Εφαρμογές Web

13 Νοε 2013 (πριν από 8 χρόνια και 2 μήνες)

302 εμφανίσεις

UMass Lowell Computer Science 91.460

Java and Distributed Computing

Prof. Karen Daniels

Fall, 2000

Lecture 21

Java Servlets

Wed. 11/22/00

based on material from

and Deitel, Chapter 19

What is a Servlet?

Servlets are somewhat of a

analog of client
side applets

but, servlets have no GUI

Java module that extends request/response
oriented servers

Works with Java
enabled web (HTTP)
servers that support the Java Servlet API

executes in a web server thread

What is a Servlet’s Context?

Java 2 Platform

Standard Edition

Enterprise Edition

Micro Edition

What can a Servlet do?

Handle HTTP client requests

Handle multiple requests concurrently

Forward requests to other servers and servlets

Generate dynamic documents

Work with databases (e.g. using JDBC)

How is a Servlet different from…?

From Common Gateway Interface (CGI)

Can perform similar tasks, but…


platform independence

efficient: executes in a web server thread

CGI script spawns a new process

can access server resources

How is a Servlet different from…?

From Java Server Pages (JSP)? JSP:

is an extension of the Servlet concept

in principle, can do JSP tasks with a Servlet

but JSP is more convenient for some tasks

allows mixing of static HTML with
generated HTML

dynamic part is written in Java

separates display aspects from content

JSP for display aspects

Servlet for content management

supports XML

How is a Servlet different from…?

From JavaScript? JavaScript:

works on the
client side

generates HTML dynamically on the
client side

How does a Servlet work?

invoked via URL in .html file

URL = web server address + location of Servlet .class file

executes in a Java
enabled web server thread

supports the Java Servlet API

implements the Servlet interface

extends (typically) HttpServlet Class

uses HTTP request types/methods:

GET: gets info. from the web server (e.g. HTML document)

POST: sends info. to the web server

overrides HttpServlet Class doGet( ) and/or doPost( )

doGet( ), doPost( ) called by HttpServlet service( )

How does a Servlet work?

HttpServletRequest interface (key methods)

doDelete( ): called for HTTP

request (e.g. file)

doOptions( ): called for HTTP


doPut( ): called for HTTP


doTrace(): called for HTTP


String getParameter( String

Returns value for a parameter sent to Servlet in GET or POST

Enumeration getParameterNames( )

Returns names of all parameters sent to Servlet in POST

String[ ] getParameterValues( String name )


for a parameter sent to Servlet in GET or POST

Cookie[ ] getCookies( ):
Cookie objects stored on client by server

HttpSession getSession( boolean create ):
client session object

How does a Servlet work?

HttpServletResponse interface (key methods)

void addCookie( Cookie cookie )

ServletOutputStream getOutputStream( )

Sets up byte
based output stream for sending data to client

PrintWriter getWriter( )

Sets up character
based output stream for sending data to client

void setContentType( String type )

Specifies type of response to browser

MIME type (e.g. text/html)

Servlet Code Samples & Demos


Deitel Chapter 19 sample code


Internet & Web Systems