John M. Thompson
| Introduction |
| The Web Server - Servlet Container Communication Model |
![]()
Web Applications are hosted by a cooperating Web Server and Servlet Container (aka Servlet Engine) fielding HTTP-based requests and invoking Servlets for processing.
The Web Server and Servlet Container are configured upon installation to communicate. For example, an Apache LoadModule statement is used to load a socket-based connector in the Apache web server.
Both processes must be running, perhaps on different machines, to support the Web App.
- HTTP requests come from a browser session (A) over the Internet to the Web Server (B).
- The Web Server is configured to send specific URLs (e.g., http://www.goaskem.com/iscs/app/assay?id=P01) and file types (e.g., .jsp's) from the Web Server (C) to the Servlet Container (D).
- The Servlet container handles a request through execution of one (or more) servlets, and returns a response.
- The Web Server (B) returns the response to the Browser Session (A).
The role of the Web App is to handle requests to specific URLs and generate appropriate responses.
Using Servlets, a Web App has full use of one or more JVMs along with any of the J2EE Technologies - JDBC, EJB, JavaMail, etc.
Distributing Web Apps across multiple contexts can offer scaling advantages. But, the cost is increased complexity and hassle in handling individual users' session data and Web App state.
| The Servlet Packages - javax.servlet and javax.servlet.http |
Here is a subset of the interfaces, classes, and methods more frequently used -
Package javax.servlet contains important interfaces and classes for web application management.
- Interface ServletContext defines methods to communicate with the Servlet Container.
- Interfaces ServletRequest and ServletResponse are extended in the http package (below).
- ServletException is defined in this package.
Package javax.servlet.http contains additional important interfaces and classes for use in web apps.
- Class HttpServlet is an abstract class which most Web App servlets will extend.
- Interface HttpServletRequest and HttpServletResponse are parameters on most of HttpServlet's request-handling methods.
- HttpSession is the magic object that will maintain state across multiple HTTP requests from a single end-user.
- ServletException also appears in this package.
| Base Class for all HTTP Servlets - HttpServlet |
Extends javax.servlet.GenericServlet.
Selected HttpServlet HTTP Request-handling (service) methods:
- void <- doGet( HttpServletRequest req, HttpServletRequest resp ) -
- typical HTTP request type for querying information
- void <- doPost( HttpServletRequest req, HttpServletRequest resp ) -
- typical HTTP request type for updating or deleting information
| Service Method Parameters - Request and Response Objects |
Selected HttpServletRequest methods:
- HttpSession <- getSession()
- java.lang.String <- getParameter( String ) (inherited)
- Cookie[] <- getCookies()
Selected HttpServletResponse methods:
- void <- setContentType( String ) (inherited)
- java.io.PrintWriter <- getWriter() (inherited)
- void <- addCookie( Cookie )
- String <- encodeUrl( String )
| Session Management - Class HttpSession |
Selected HttpSession methods:
- void <- setAttribute( String, java.lang.Object )
- java.lang.Object <- getAttribute( String )
- void <- removeAttribute( String )
- void <- invalidate()
| A Working Example |
As an example, we'll do a code walkthrough on the goAskem servlet that fields assessment delivery requests -
iwaypublishing.iscs.servlets.assay
Originals are accessible from http://www.iwaytechnology.com/jt/index.html.
| Copyright
© 2001 by John M. Thompson Boulder, Colorado USA jt@iwaytechnology.com |
A
limited right to copy this page for individual (non-commercial) educational use only is hereby granted. |