close

Вход

Забыли?

вход по аккаунту

код для вставкиСкачать
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 http://java.sun.com
and Deitel, Chapter 19
What is a Servlet?

Servlets are somewhat of a server-side
analog of client-side applets
 but,
servlets have no GUI
Java module that extends request/responseoriented 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
Enterprise Edition
Standard 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)
script?
 Can
perform similar tasks, but…
 Servlet:
provides 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
dynamically-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






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:




URL = web server address + location of Servlet .class file
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 DELETE request (e.g. file)
doOptions( ): called for HTTP OPTIONS request
doPut( ): called for HTTP PUT request
doTrace(): called for HTTP TRACE request
String getParameter( String parameterName )


Enumeration getParameterNames( )



Returns names of all parameters sent to Servlet in POST
String[ ] getParameterValues( String name )


Returns value for a parameter sent to Servlet in GET or POST
Returns values 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( )


PrintWriter getWriter( )


Sets up byte-based output stream for sending data to client
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
http://kdaniels:8080
Deitel Chapter 19 sample code
Resources
http://java.sun.com
 UML CS Internet & Web Systems courses

1/--страниц
Пожаловаться на содержимое документа