novocode.com
About Novocode.com  |  Contact  |  Site Map  |  Search

Need more information? Shop for books about Servlets at Amazon.com:

Appendix

A. Compiling Servlets

Servlets can be compiled with the JDK (available from http://www.javasoft.com) and other Java compilers, just like Applets and Applications. If the Servlet packages javax.servlet and javax.servlet.http are not included with the compiler or your Servlet engine you have to install them separately by downloading the JSDK (Java Servlet Development Kit) and including the JSDK classes in your CLASSPATH environment variable.

Example. Compiling the HelloClientServlet on a Unix machine with a JDK installation and the JSDK 2.0 unpacked in /usr/local/JSDK2.0:

$ echo $CLASSPATH
.:/usr/local/jdk/lib/classes.zip
$ export CLASSPATH=$CLASSPATH:/usr/local/JSDK2.0/lib/jsdk.jar
$ echo $CLASSPATH
.:/usr/local/jdk/lib/classes.zip:/usr/local/JSDK2.0/lib/jsdk.jar
$ ls HelloClientServlet.*
HelloClientServlet.java
$ javac HelloClientServlet.java
$ ls HelloClientServlet.*
HelloClientServlet.class HelloClientServlet.java
$ |

B. Running the Example Servlets with NetForge

The Servlets from this tutorial can be tested easily with the NetForge Web Server which supports the most current version 2.1 of the Servlet API. The server has a preconfigured Servlet directory netforge/servlets/. Compile the servlets and copy the *.class files to netforge/servlets/. When the server is running, the Servlets can be accessed via http://localhost:22722/servlet/ClassName, e.g. http://localhost:22722/servlet/HelloClientServlet for the HelloClientServlet.

Example. Copying the compiled HelloClientServlet to the NetForge Servlet directory and starting the server on a Unix machine with NetForge installed in /usr/local/netforge/:

$ cp HelloClientServlet.class /usr/local/netforge/servlets/
$ /usr/local/netforge/bin/netforge -i
[22 Jun 1998 9:39:05 GMT] <note> Starting up NetForge 0.48
[22 Jun 1998 9:39:05 GMT] <note> Home directory is "/usr/local/netforge/."
[22 Jun 1998 9:39:06 GMT] <note> Security manager installed
[22 Jun 1998 9:39:12 GMT] <note> HTTP server "Example Web" started on port 22722
[22 Jun 1998 9:39:28 GMT] <note> Local administration GUI opened
[22 Jun 1998 9:39:28 GMT] <note> Startup completed

The ListManagerServlet requires an init parameter, so it has to be accessed through an alias:

  1. Start NetForge with the "-i" option (as shown above) to open an administration GUI.

  2. Use the popup menu which is attached to the "/servlet/" object to open a configuration window.

  3. Enter the line "listman ListManagerServlet addressfile=/tmp/addresses" into the "Aliases" field.

  4. Click the "Apply" button to perform the configuration change.

The ListManagerServlet can now be accessed via http://localhost:22722/servlet/listman. The addresses are written to the file /tmp/addresses.

See the NetForge manual for details on configuring and running the server.

C. Servlet Resources on the Internet

The Servlet home page on the Javasoft website
http://jserv.javasoft.com/products/java-server/servlets/
A list of Servlet engines and Servlet-enabled Web Servers
http://jserv.javasoft.com/products/java-server/servlets/environments.html
The Servlet API Specification
http://java.sun.com/products/servlet/2.1/index.html

The current version of the Servlet API is 2.1. You can download the specification from this location or browse it online. This is also the place to download the javadoc API documentation and the Servlet interface classes.

The JSDK download page
http://java.sun.com/products/java-server/servlets/index.html#sdk

Sun offers packages for Windows and Solaris/Sparc. The JSDK does not contain any native code so it can be used on other platforms as well. You should download the Solaris version for all platforms other than Windows.

Paul Flavin's Servlet Resources page
http://www.frontiernet.net/~imaging/servlets_intro.html
The SERVLET-INTEREST mailing list
http://java.sun.com/products/servlet/list.html
Servlet Central, the Server-Side Java Online Magazine
http://www.servletcentral.com

D. Glossary

CGI
"Common Gateway Interface", a language-independent interface that allows a server to start an external process which gets information about a request through environment variables, the command line and its standard input stream and writes response data to its standard output stream. Each request is answered in a separate process by a separate instance of the CGI program... (continued in section 1.2)
HTTP
"HyperText Transfer Protocol", a request-response protocol which is used by web browsers and other clients to access a web server. See section 1.4 for details.
JSP
"Java Server Pages", a server-side scripting language which is based on the Servlet framework and allows combining HTML text with Java source code in the same document. See http://java.sun.com/products/jsp/ for more information.
JVM
"Java Virtual Machine", a program which interprets Java bytecode and provides the environment for the execution of Java programs.
RFC
"Request For Comments", an Internet standard or proposed standard. The RFC documents can be found at many mirror sites, including ftp://ftp.internic.net/rfc/.
Sandbox
A feature of a Java application (e.g. a Web Server) which restricts the possible operations a program module (e.g. a Servlet) can perform. For example, in a shared server where users can install their own custom Servlets, the server might not allow Servlets to access local files outside a specific directory or call System.exit to quit the server.
Session Tracking
State can be maintained on top of a stateless networking protocol by associating individual requests with a client by using a session ID, a unique code which is sent by the client with every request. Session tracking techniques for Servlets are described in section 2.3.
Servlet
Servlets are modules of Java code that run in a server application (hence the name "Servlets", similar to "Applets" on the client side) to answer client requests... (continued in section 1.1)
Servlet Engine
An integral part of a Web Server or a third-party add-on to a Web Server which provides the environment for the execution of Servlets.
State
A networking protocol is said to be stateful if it maintains a state (a set of variables) between multiple requests by the same client. An example for such a protocol is FTP. The opposite is a stateless protocol like HTTP where client requests are not connected to each other. State can be maintained on top of a stateless protocol by using a session tracking technique.
Unicode
The 16-bit character encoding which is used by Java for the data types char and java.lang.String. See http://www.unicode.org for more information.
Web Server
a) (as used in this document) A server software which speaks HTTP,
b) a machine which runs such a software.

E. Servlet API Changes

The following changes have been made during the evolution of the Servlet API:

Changes from Version 1.0 to Version 2.0

Version 2.0 added support for Internationalization, Cookies, Session Tracking, HTTP/1.1 and single-threaded Servlets.

New Classes and Interfaces
  • Interface javax.servlet.SingleThreadModel
  • Interface javax.servlet.http.HttpSession
  • Interface javax.servlet.http.HttpSessionBindingListener
  • Interface javax.servlet.http.HttpSessionContext
  • Class javax.servlet.http.Cookie
  • Class javax.servlet.http.HttpSessionBindingEvent
New Methods, Constructors and Fields
  • Method javax.servlet.ServletContext.getServletNames()
  • Method javax.servlet.ServletContext.log(Exception, String)
  • Method javax.servlet.ServletRequest.getCharacterEncoding()
  • Method javax.servlet.ServletRequest.getReader()
  • Method javax.servlet.ServletResponse.getCharacterEncoding()
  • Method javax.servlet.ServletResponse.getWriter()
  • Constructor javax.servlet.ServletException.ServletException()
  • Method javax.servlet.http.HttpServletRequest.getCookies()
  • Method javax.servlet.http.HttpServletRequest.getRequestedSessionId()
  • Method javax.servlet.http.HttpServletRequest.getSession(boolean)
  • Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromCookie()
  • Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromUrl()
  • Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdValid()
  • Field javax.servlet.http.HttpServletResponse.SC_CONFLICT
  • Field javax.servlet.http.HttpServletResponse.SC_CONTINUE
  • Field javax.servlet.http.HttpServletResponse.SC_GATEWAY_TIMEOUT
  • Field javax.servlet.http.HttpServletResponse.SC_GONE
  • Field javax.servlet.http.HttpServletResponse.SC_HTTP_VERSION_NOT_SUPPORTED
  • Field javax.servlet.http.HttpServletResponse.SC_LENGTH_REQUIRED
  • Field javax.servlet.http.HttpServletResponse.SC_METHOD_NOT_ALLOWED
  • Field javax.servlet.http.HttpServletResponse.SC_MULTIPLE_CHOICES
  • Field javax.servlet.http.HttpServletResponse.SC_NON_AUTHORITATIVE_INFORMATION
  • Field javax.servlet.http.HttpServletResponse.SC_NOT_ACCEPTABLE
  • Field javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT
  • Field javax.servlet.http.HttpServletResponse.SC_PAYMENT_REQUIRED
  • Field javax.servlet.http.HttpServletResponse.SC_PRECONDITION_FAILED
  • Field javax.servlet.http.HttpServletResponse.SC_PROXY_AUTHENTICATION_REQUIRED
  • Field javax.servlet.http.HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE
  • Field javax.servlet.http.HttpServletResponse.SC_REQUEST_TIMEOUT
  • Field javax.servlet.http.HttpServletResponse.SC_REQUEST_URI_TOO_LONG
  • Field javax.servlet.http.HttpServletResponse.SC_RESET_CONTENT
  • Field javax.servlet.http.HttpServletResponse.SC_SEE_OTHER
  • Field javax.servlet.http.HttpServletResponse.SC_SWITCHING_PROTOCOLS
  • Field javax.servlet.http.HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE
  • Field javax.servlet.http.HttpServletResponse.SC_USE_PROXY
  • Method javax.servlet.http.HttpServletResponse.addCookie(Cookie)
  • Method javax.servlet.http.HttpServletResponse.encodeRedirectUrl(String)
  • Method javax.servlet.http.HttpServletResponse.encodeUrl(String)
  • Method javax.servlet.http.HttpServlet.doDelete(HttpServletRequest, HttpServletResponse)
  • Method javax.servlet.http.HttpServlet.doOptions(HttpServletRequest, HttpServletResponse)
  • Method javax.servlet.http.HttpServlet.doPut(HttpServletRequest, HttpServletResponse)
  • Method javax.servlet.http.HttpServlet.doTrace(HttpServletRequest, HttpServletResponse)
Deprecated Methods
  • Method javax.servlet.ServletContext.getServlets()
    (use getServletNames in conjunction with getServlet instead)

Changes from Version 2.0 to Version 2.1

Version 2.1 introduced the new model for inter-servlet communication and request delegation. There are some new minor features like nested Exceptions. Several inconsistencies in the API have been corrected.

New Interfaces
  • Interface javax.servlet.RequestDispatcher
Deprecated Interfaces
  • Interface javax.servlet.http.HttpSessionContext
    (removed for security reasons)
New Methods and Constructors
  • Method javax.servlet.ServletContext.getAttributeNames()
  • Method javax.servlet.ServletContext.getContext(String)
  • Method javax.servlet.ServletContext.getMajorVersion()
  • Method javax.servlet.ServletContext.getMinorVersion()
  • Method javax.servlet.ServletContext.getRequestDispatcher(String)
  • Method javax.servlet.ServletContext.getResource(String)
  • Method javax.servlet.ServletContext.getResourceAsStream(String)
  • Method javax.servlet.ServletContext.log(String, Throwable)
  • Method javax.servlet.ServletContext.removeAttribute(String)
  • Method javax.servlet.ServletContext.setAttribute(String, Object)
  • Method javax.servlet.ServletRequest.getAttributeNames()
  • Method javax.servlet.ServletRequest.setAttribute(String, Object)
  • Method javax.servlet.GenericServlet.init()
  • Method javax.servlet.GenericServlet.log(String, Throwable)
  • Constructor javax.servlet.ServletException.ServletException(String, Throwable)
  • Constructor javax.servlet.ServletException.ServletException(Throwable)
  • Method javax.servlet.ServletException.getRootCause()
  • Method javax.servlet.http.HttpServletRequest.getSession()
  • Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromURL()
  • Method javax.servlet.http.HttpServletResponse.encodeRedirectURL(String)
  • Method javax.servlet.http.HttpServletResponse.encodeURL(String)
  • Method javax.servlet.http.HttpSession.getMaxInactiveInterval()
  • Method javax.servlet.http.HttpSession.setMaxInactiveInterval(int)
Deprecated Methods
  • Method javax.servlet.ServletContext.getServlet(String)
    (potentially unsafe)
  • Method javax.servlet.ServletContext.getServletNames()
    (makes no sense without getServlet)
  • Method javax.servlet.ServletContext.log(Exception, String)
    (use log(String, Throwable) instead)
  • Method javax.servlet.ServletRequest.getRealPath(String)
    (use ServletContext.getRealPath instead)
  • Method javax.servlet.http.HttpServletRequest.isRequestedSessionIdFromUrl()
    (use isRequestedSessionIdFromURL instead)
  • Method javax.servlet.http.HttpServletResponse.encodeRedirectUrl(String)
    (use encodeRedirectURL instead)
  • Method javax.servlet.http.HttpServletResponse.encodeUrl(String)
    (use encodeURL instead)
  • Method javax.servlet.http.HttpSession.getSessionContext()
    (HttpSessionContext is deprecated)