Appendix A. Servlet API Quick ReferenceThe javax.servlet package is the core of the Servlet API. It includes the basic Servlet interface, which all servlets must implement in one form or another, and an abstract GenericServlet class for developing basic servlets. This package also includes classes for communication with the host server and client (ServletRequest and ServletResponse) and communicating with the client (ServletInputStream and ServletOutputStream). The class hierarchy of the javax.servlet package is shown in Figure A-1. Servlets should confine themselves to the classes in this package in situations where the underlying protocol is unknown.Figure A-1. The javax.servlet package
SynopsisClass Name: javax.servlet.GenericServletSuperclass: java.lang.Object Immediate Subclassesjavax.servlet.http.HttpServlet Interfaces Implemented:javax.servlet.Servletjavax.servlet.ServletConfigjava.io.Serializable Availability: Servlet API 1.0 and later DescriptionGenericServlet provides a basic implementation of the Servlet interface for protocol-independent servlets. As a convenience, it also implements the ServletConfig interface. Most servlet developers subclass this class or HttpServlet, rather than implement the Servlet interface directly. GenericServlet includes basic versions of the init() and destroy() methods, which perform basic setup and cleanup tasks, such as managing the server's ServletConfig object. It's good form for a servlet that overrides one of these methods to call the superclass version of the method. GenericServlet also includes a log() method that provides easy access to the logging functions from ServletContext. The service() method is declared as abstract and must be overridden. Well written servlets also override getServletInfo(). Class Summarypublic abstract class GenericServlet implements Servlet, ServletConfig, java.io.Serializable { // Constructors public GenericServlet(); // Instance Methods public void destroy(); public String getInitParameter(String name); public Enumeration getInitParameterNames(); public ServletConfig getServletConfig(); public ServletContext getServletContext(); public String getServletInfo(); public void init(ServletConfig config) throws ServletException; public void log(String msg); public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; } ConstructorsGenericServlet()public GenericServlet() Instance Methodsdestroy()public void destroy() getInitParameter()public String getInitParameter(String name) getInitParameterNames()public Enumeration getInitParameterNames() getServletConfig()public ServletConfig getServletConfig() getServletContext()public ServletContext getServletContext() getServletInfo()public String getServletInfo() init()public void init(ServletConfig config) throws ServletException log()public void log(String msg) service()public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
SynopsisInterface Name: javax.servlet.ServletSuper-interface: None Immediate Subinterfaces: None Implemented By: javax.servlet.GenericServlet Availability: Servlet API 1.0 and later DescriptionAll servlets implement the Servlet interface, either directly or by subclassing the GenericServlet or HttpServlet class. Most servlet developers find it easier to subclass one of the two existing servlet classes than to implement this interface directly. The interface declares the basic servlet functionality--initializing a servlet, handling a client request, and destroying a servlet. Interface Declarationpublic interface Servlet { // Methods public abstract void destroy(); public abstract ServletConfig getServletConfig(); public abstract String getServletInfo(); public abstract void init(ServletConfig config) throws ServletException; public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException; } Methodsdestroy()public abstract void destroy() getServletConfig()public abstract ServletConfig getServletConfig() getServletInfo()public abstract String getServletInfo() init()public abstract void init(ServletConfig config) throws ServletException service()public abstract void service(ServletRequest req, ServletResponse res) throws ServletException, IOException
SynopsisInterface Name: javax.servlet.ServletConfigSuperinterface: None Immediate Subinterfaces: None Implemented By: javax.servlet.GenericServlet Availability: Servlet API 1.0 and later DescriptionServers use ServletConfig objects to pass initialization and context information to servlets. The initialization information generally consists of a series of initialization parameters (init parameters) and a ServletContext object, which provides information about the server environment. A servlet can implement Servlet-Config to allow easy access to init parameters and context information, as GenericServlet does. Interface Declarationpublic interface ServletConfig { // Methods public abstract String getInitParameter(String name); public abstract Enumeration getInitParameterNames(); public abstract ServletContext getServletContext(); } MethodsgetInitParameter()public abstract String getInitParameter(String name) getInitParameterNames()public abstract Enumeration getInitParameterNames() getServletContext()public abstract ServletContext getServletContext()
SynopsisInterface Name: javax.servlet.ServletContextSuperinterface: None Immediate Subinterfaces: None Implemented By: None Availability: Servlet API 1.0 and later DescriptionThe ServletContext interface defines a set of methods that can be used to communicate with the server in a non-request-specific manner. This includes finding path information, accessing other servlets running on the server, and writing to the server log file. Different virtual servers may return different servlet contexts. Interface Declarationpublic interface ServletContext { // Methods public abstract Object getAttribute(String name); public abstract String getMimeType(String file); public abstract String getRealPath(String path); public abstract String getServerInfo(); public abstract Servlet getServlet(String name) throws ServletException; public abstract Enumeration getServletNames(); // New in 2.0 public abstract Enumeration getServlets(); // Deprecated public abstract void log(Exception exception, String msg); // New in 2.0 public abstract void log(String msg); } MethodsgetAttribute()public abstract Object getAttribute(String name) getMimeType()public abstract String getMimeType(String file) getRealPath()public abstract String getRealPath(String path) getServerInfo()public abstract String getServerInfo() getServlet()public abstract Servlet getServlet(String name) throws ServletException getServletNames()public abstract Enumeration getServletNames() getServlets()public abstract Enumeration getServlets() throws ServletException log()public abstract void log(Exception exception, String msg) public abstract void log(String msg)
SynopsisClass Name: javax.servlet.ServletExceptionSuperclass: java.lang.Exception Immediate Subclasses:javax.servlet.UnavailableException Interfaces Implemented: None Availability: Servlet API 1.0 and later DescriptionA generic exception thrown by servlets encountering difficulties. Class Summarypublic class ServletException extends java.lang.Exception { // Constructors public ServletException(); // New in 2.0 public ServletException(String msg); } ConstructorsServletException()public ServletException() public ServletException(String msg)
SynopsisClass Name: javax.servlet.ServletInputStreamSuperclass: java.io.InputStream Immediate Subclasses: None Interfaces Implemented: None Availability: Servlet API 1.0 and later DescriptionProvides an input stream for reading binary data from a client request, including an efficient readLine() method for reading data one line at a time. A ServletInputStream is returned by the getInputStream() method of ServletRequest. A servlet that filters binary output from other sources generally gets its input via this stream. Class Summarypublic abstract class ServletInputStream extends java.io.InputStream { // Constructors protected ServletInputStream(); // Instance Methods public int readLine(byte b[], int off, int len) throws IOException; } ConstructorsServletInputStream()protected ServletInputStream()
Instance MethodsreadLine()public int readLine(byte b[], int off, int len) throws IOException
SynopsisClass Name: javax.servlet.ServletOutputStreamSuperclass: java.io.OutputStream Immediate Subclasses: None Interfaces Implemented: None Availability: Servlet API 1.0 and later DescriptionProvides an output stream for sending binary data back to a client. A servlet obtains a ServletOutputStream object from the getOutputStream() method of ServletResponse. Although it includes a range of print() and println() methods for sending text or HTML, the ServletOutputStream has been superseded by PrintWriter. It should be used only for sending binary data or with early servlet implementations built on the Servlet API 1.0. If you subclass ServletOutputStream, you must provide an implementation of the write(int) method. Class Summarypublic abstract class ServletOutputStream extends java.io.OutputStream { // Constructors protected ServletOutputStream(); // Instance Methods public void print(boolean b) throws IOException; public void print(char c) throws IOException; public void print(double d) throws IOException; public void print(float f) throws IOException; public void print(int i) throws IOException; public void print(long l) throws IOException; public void print(String s) throws IOException; public void println() throws IOException; public void println(boolean b) throws IOException; public void println(char c) throws IOException; public void println(double d) throws IOException; public void println(float f) throws IOException; public void println(int i) throws IOException; public void println(long l) throws IOException; public void println(String s) throws IOException; } ConstructorsServletOutputStream()protected ServletOutputStream() Instance Methodsprint()public void print(boolean b) throws IOException public void print(char c) throws IOException public void print(double d) throws IOException public void print(float f) throws IOException public void print(int i) throws IOException public void print(long l) throws IOException public void print(String s) throws IOException println()public void println() throws IOException public void println(boolean b) throws IOException public void println(char c) throws IOException public void println(double d) throws IOException public void println(float f) throws IOException public void println(int i) throws IOException public void println(long l) throws IOException public void println(String s) throws IOException
SynopsisInterface Name: javax.servlet.ServletRequestSuperinterface: None Immediate Subinterfaces: javax.servlet.http.HttpServletRequest Implemented By: None Availability: Servlet API 1.0 and later DescriptionA ServletRequest object encapsulates information about a single client request, including request parameters, implementation-specific attributes, and an input stream for reading binary data from the request body. ServletRequest can be subclassed to provide additional protocol-specific information. HttpServletRequest, for instance, includes methods to manipulate HTTP headers. Interface Declarationpublic interface ServletRequest { // Methods public abstract Object getAttribute(String name); public abstract String getCharacterEncoding(); // New in 2.0 public abstract int getContentLength(); public abstract String getContentType(); public abstract ServletInputStream getInputStream() throws IOException; public abstract String getParameter(String name); public abstract Enumeration getParameterNames(); public abstract String[] getParameterValues(String name); public abstract String getProtocol(); public abstract BufferedReader getReader() throws IOException;// New in 2.0 public abstract String getRealPath(String path); public abstract String getRemoteAddr(); public abstract String getRemoteHost(); public abstract String getScheme(); public abstract String getServerName(); public abstract int getServerPort(); } MethodsgetAttribute()public abstract Object getAttribute(String name) getCharacterEncoding()public abstract String getCharacterEncoding() getContentLength()public abstract int getContentLength() getContentType()public abstract String getContentType() getInputStream()public abstract ServletInputStream getInputStream() throws IOException getParameter()public abstract String getParameter(String name) getParameterNames()public abstract Enumeration getParameterNames() getParameterValues()public abstract String[] getParameterValues(String name) getProtocol()public abstract String getProtocol() getReader()public abstract BufferedReader getReader() throws IOException getRealPath()public abstract String getRealPath(String path) getRemoteAddr()public abstract String getRemoteAddr() getRemoteHost()public abstract String getRemoteHost() getScheme()public abstract String getScheme() getServerName()public abstract String getServerName() getServerPort()public abstract int getServerPort()
SynopsisInterface Name: javax.servlet.ServletResponseSuperinterface: None Immediate Subinterfaces: javax.servlet.http.HttpServletResponse Interfaces Implemented: None Availability: Servlet API 1.0 and later DescriptionServlets use ServletResponse objects to send MIME encoded data back to the client. The servlet engine creates this object and passes it to the servlet's service() method. To send binary data, use the ServletOutputStream returned by getOutputStream(). To send character data, use the PrintWriter returned by getWriter(). You can explicitly set the output's MIME type using the setContentType() method. If you elect to set this manually, do so before calling getWriter(), as getWriter() consults the content type to determine which charset to use. Consult RFC 2045 at http:/www.ietf.org/rfc/rfc2045.txt for more information on MIME. Interface Declarationpublic interface ServletResponse { // Methods public abstract String getCharacterEncoding(); // New in 2.0 public abstract ServletOutputStream getOutputStream() throws IOException; public abstract PrintWriter getWriter() throws IOException; // New in 2.0 public abstract void setContentLength(int len); public abstract void setContentType(String type); } MethodsgetCharacterEncoding()public abstract String getCharacterEncoding() getOutputStream()public abstract ServletOutputStream getOutputStream() throws IOException getWriter()public abstract PrintWriter getWriter() throws IOException setContentLength()public abstract void setContentLength(int len) setContentType()public abstract void setContentType(String type)
SynopsisInterface Name: javax.servlet.SingleThreadModelSuperinterface: None Immediate Subinterfaces: None Implemented By: None Availability: New as of Servlet API 2.0; found in JSDK 2.0, JWS 1.1 DescriptionSingleThreadModel is a tag interface with no methods. If a servlet implements this interface, the server ensures that each instance of the servlet handles only one service request at a time. Servers implement this functionality by maintaining a pool of servlet instances and dispatching incoming requests to free servlets within the pool. SingleThreadModel provides easy thread safety, but at the cost of increased resource requirements as more servlet instances are loaded at any given time. public interface SingleThreadModel { }
SynopsisClass Name: javax.servlet.UnavailableExceptionSuperclass: javax.servlet.ServletException Immediate Subclasses: None Interfaces Implemented: None Availability: Servlet API 1.0 and later DescriptionA servlet can throw an UnavailableException at any time to indicate that it is not available to service client requests. There are two types of unavailability: permanent (where some corrective action must be taken on the server) and temporary. A servlet is temporarily unavailable if some system-wide problem momentarily prevents it from servicing requests. This may include network troubles or a crashed or overloaded database server. To mark a servlet as temporarily unavailable, specify a duration (in seconds) when constructing the exception. Well-written servers check back after this time. Servlet implementations are not required to treat temporary and permanent unavailability differently. Servers generally provide clients with polite error messages when handling requests for unavailable servlets. For example, the Java Web Server returns a 404 (Unavailable) message. Class Summarypublic class UnavailableException extends ServletException { // Constructors public UnavailableException(int seconds, Servlet servlet, String msg); public UnavailableException(Servlet servlet, String msg); // Instance Methods public Servlet getServlet(); public int getUnavailableSeconds(); public boolean isPermanent(); } ConstructorsUnavailableException()public UnavailableException(int seconds, Servlet servlet, String msg) public UnavailableException(Servlet servlet, String msg) getServlet()public Servlet getServlet() getUnavailableSeconds()public int getUnavailableSeconds() isPermanent()public boolean isPermanent()
Copyright © 2001 O'Reilly & Associates. All rights reserved.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|