| Previous | Table of Contents | Next |
Because the content type is used to determine the character encoding for the output writer, set the content type before accessing the writer. Also, in the case of HTTP style servlets, the content type is returned to the client in the header of the HTTP reply, while the output stream and writer return data in the body. This means that you should set the content type first, before using either method to return data to the client. Bottom line: Set the content type before sending any output. Also, use the content length, if possible.
| Table 7.5 ServletResponse Methods | |
| METHOD | DESCRIPTION |
|---|---|
| String getCharacterEncoding() | Returns the character set encoding used for this MIME body. Use the setContentType method to change this encoding. This method is used to create the appropriate writer for sending text data to the client. |
| ServletOutputStream getOutputStream() | Returns an output stream for writing binary response data. |
| PrintWriter getWriter() | Returns a print writer for writing formatted text responses. |
| void setContentLength(int) | Sets the content length for this response in bytes. If the servlet knows how much data is going to be written, it should provide this information here. |
| void setContentType(String) | Sets the content type for this response as a standard MIME type. The default is text/plain. |
When a servlet is initialized, it is passed an object that implements the ServletConfig interface. Table 7.6 lists the methods defined in this interface. This object is designed to provide information to the servlet about the server and how the servlet is currently configured. Essentially, the configuration object provides two pieces of information. First, the configuration provides access to configuration parameters defined when the servlet was installed on the server. Second, the configuration object provides access to the servlets context.
Depending on the server you install, a servlet on the initialization parameters may be defined differently. For example, the Java Web Server provided by Sun uses a configuration applet to set initialization parameters; the JSDK uses a properties file.
The ServletConfig interface provides specific information about the servlets configuration, but the ServletContext interface defines methods that provide information about the environment in which the servlet itself is running. These methods are listed in Table 7.7. The context is accessed via the configuration object using the method getServletContext.
The context provides access to both the servlet log file and the other servlets on the server. Like the similar feature in AppletContext, access to other servlets may be considered a security risk and therefore not to be implemented on all servers. Refer to your servers documentation for information on what these methods do.
Perhaps the easiest way to create a servlet is to subclass GenericServlet. GenericServlet implements the Servlet interface and provides a number of convenient methods for accessing information from the servlet configuration. These methods are listed in Table 7.8.
| Table 7.6 ServletConfig Methods | |
| METHOD | DESCRIPTION |
|---|---|
| String getInitParameter(String) | Returns a string containing the value of the named initialization parameter of the servlet or null if the parameter does not exist. |
| Enumeration getInitParameterNames() | Returns the names of the servlets initialization parameters as an enumeration of strings or an empty enumeration if there are no initialization parameters. Use the getInitParameter method to get the value for each named parameter. |
| ServletContext getServletContext() | Returns the servlets context, as defined by the server. |
| Table 7.7 ServletContext Methods | |
| METHOD | DESCRIPTION |
|---|---|
| Object getAttribute(String) | Like the request method of the same name, this method provides access to server-specific information. |
| String getMimeType(String) | Returns the MIME type of the specified file or null if not known. This method may or may not be implemented by the server, and servlets should plan for the possible null return value. |
| String getRealPath(String) | Applies alias rules to the specified virtual path and returns the corresponding real path. |
| String getServerInfo() | Returns the name and version of the network service under which the servlet is running. |
| Servlet getServlet(String) | Returns the servlet of the specified name or null if not found. |
| Enumeration getServletNames() | Returns an enumeration of the Servlet object names in this server. |
| void log(Exception, String) | Writes the stacktrace and the given message string to the servlet log file. |
| void log(String) | Writes the given message string to the servlet log file. |
Many of these methods are covers for ServletConfig or ServletContext methods. These methods simply forward messages to the appropriate object.
| Previous | Table of Contents | Next |