This document was uploaded by user and they confirmed that they have the permission to share
it. If you are author or own the copyright of this book, please report to us by using this DMCA
report form. Report DMCA
Servlets vs. Applets Servlets have no GUI Server-side, not client-side Different security model Installed, not downloaded But you can download remote servlets too
Servlets vs. CGI (Cont.) Easy to manage state share data across successive requests share data between concurrent requests use hidden fields, cookies, or sessions
Write once, run anywhere It's easy to write unportable Perl Servlets have standard API
Supports all methods GET, POST, PUT, DELETE, et al. 11/04/08
Servlets vs. FastCGI FastCGI sends multiple requests to a single separate process requires process context switch Servlets send multiple requests to multiple threads in same process requires lightweight thread context switch (Also applies to ISAPI) Nice diagram in White Paper Servlets also automatically take advantage of multiprocessors if the underlying JVM does 11/04/08
Authenticating the user’s identity HTTP Authentication Username/password sent to server on every request (like cookies) Very light encryption (uuencode)
Digest Authentication Cryptographic handshaking between client and server Very good encryption Not supported by all servers/browsers 11/04/08
API Availability Standard Java Extension API From white paper: "This means that while it is not part of the core Java framework which must always be part of all products bearing the Java brand, it will be made available with such products by their vendors as an add-on package."
Servlet Architectures: Web Publishing SSI Servlets JSP Servlets Best to keep business logic inside Java objects Keep the JSP light so designers don’t get scared
Chaining servlets Multiple servers data gathering, collecting, serving, load balancing, etc. 11/04/08
Remote Servlets Three ways to configure configure with Administration Tool invoke inside a server-side include configure inside a servlet chain
Standard Servlets DateServlet echoes current date/time EchoServlet echoes CGI parameters (good for testing) MailServlet sends email in response to a CGI form RedirectServlet used by server to manage HTTP redirects SessionServlet used by server to manage sessions Many more... 11/04/08
A Note on CLASSPATH and JWS JWS uses its own JRE Three ways to add classes Put the class files into the “classes” subdirectory Jar them, and put the jar files into the “lib” subdirectory Start the server with the -classpath option httpd -classpath c:\projects\utils
Servlet Architecture Overview ServletRequest What the client says to the server Access to information like protocol, client IP#, parameters, and body
ServletResponse What the servlet says to the client
HttpServletRequest, HttpServletResponse HTTP-specific communication and information State-tracking and session management 11/04/08
Servlet Lifecycle Overview Server loads and instantiates servlet Server calls init() Loop Server receives request from client Server calls service() service() calls doGet() or doPost()
Server calls destroy() More detail to come later... 11/04/08
ServletRequest - Client Info getRemoteAddr() Returns the IP address of the agent that sent the request getRemoteHost() Returns the fully qualified host name of the agent that sent the request getProtocol() Returns the protocol and version of the request as a string of the form <protocol>/<major version>.<minor version>.
ServletRequest - URL Info getScheme() Returns the scheme of the URL used in this request, for example "http", "https", or "ftp". getServerName() Returns the host name of the server that received the request getServerPort() Returns the port number on which this request was received getServletPath() Returns the URI path that got to this script, e.g. “/servlet/com.foo.MyServlet” Useful for putting in a
Can use right in a URL http://localhost:8080/servlet/HelloHttpServlet?name=Fred