Table of Contents | Previous | Next | Index | Bookshelf

Programmer's Guide to Servlets


Appendix G
API Clarifications

This appendix clarifies the way some of the standard Servlet API methods work in iPlanet Web Server 4.1. For the official documentation for the methods discussed here (and for all servlet API methods) see the Servlets API Class Reference published by Sun Microsystems at:

http://java.sun.com/products/servlet/2.1/html/api-reference.fm.html
This appendix provides clarifications for using the following methods with iPlanet Web Server 4.1:

HttpUtils.getRequestURL

public static StringBuffer getRequestURL(HttpServletRequest request);
This method reconstructs the URL used by the client to make the given request on the server. This method accounts for difference in scheme (such as http, https) and ports, but does not attempt to include query parameters.

This method returns a StringBuffer instead of a String so that the URL can be modified efficiently by the servlet.

Clarification

To determine the server name part of the requested URL, iPlanet Web Server first tries to use the "Host" header and then looks at the value of ServerName in magnus.conf. By default, the server name is the machine name, but this value is editable during iPlanet Web Server 4.1 installation. If the server name has been changed, HttpUtils.getRequestURL might not return the host name that is needed to reconstruct the request.

For example, suppose the request is http://abc/index.html. However, the server name has been changed to xyz. In this case, HttpUtils.getRequestURL might return http://xyz/index.html, which is not the original URL that was requested.

HttpSession.setMaxInactiveInterval

public void setMaxInactiveInterval(int interval);
Sets the amount of time that a session can be inactive before the servlet engine is allowed to expire it.

Clarification

It is not possible to set the maximum inactive interval so that the session never times out. The session always has a timeout value.

If you pass a negative or zero value, the session expires immediately.

GenericServlet.getInitParameter and getInitParameterNames

public String getInitParameter(String name);
This method returns a String containing the value of the servlet's named initialization parameter, or null if this parameter does not exist.

public Enumeration getInitParameterNames();
This method returns an enumeration of String objects containing the names of the initialization parameters for the calling servlet. If the calling servlet has no initialization parameters, getInitParameterNames returns an empty enumeration.

Clarification

For servlets running on iPlanet Web Server 4.1, the methods getInitParameter and getInitParameterNames for the class ServletConfig only work for servlets that are invoked through virtual path translations. The same restriction applies to the convenience methods of the same names in the class GenericServlet, which invoke the corresponding methods on ServletConfig.

For information about setting virtual path translations, see the section "Specifying Servlet Virtual Paths."

These methods do not work if the servlet is invoked by a client request that specifies a servlet in a registered servlet directory rather than using a virtual path translation to access the servlet.

ServletContext.getAttributeNames

public java.util.Enumeration getAttributeNames()
This method returns an enumeration containing the attribute names available within the servlet's context.

Clarification

If you are using MMapSessions, iPlanet Web Server truncates names retrieved by ServletContext.getAttributeNames to 128 characters.

ServletContext.getContext

public ServletContext getContext(String uripath);
Returns the servlet context object that contains servlets and resources for a particular URI path, or null if a context cannot be provided for the path.

Clarification

This method only works if both the following conditions are true:

ServletRequest.getAttribute

public java.lang.Object getAttribute(java.lang.String name)
Returns the value of the named attribute as an object, or returns null if no attribute of the given name exists.

Clarification

ServletRequest.getAttribute returns a CGI variable if it exists. However, the getAttributeNames method does not show these variables within its enumeration.

RequestDispatcher.forward and include

public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException;
Used for forwarding a request from this servlet to another resource on the web server. This method is useful when one servlet does preliminary processing of a request and wants to let another object generate the response.

The request object passed to the target object will have its request URL path and other path parameters adjusted to reflect the target URL path of the target object.

You cannot use this method if a ServletOutputStream object or PrintWriter object has been obtained from the response. In that case, the method throws an IllegalStateException.

public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException;
Used for including the content generated by another server resource in the body of a response. In essence, this method enables programmatic server-side includes. The request object passed to the target object reflects the request URL path and path info of the calling request. The response object only has access to the calling servlet's ServletOutputStream object or PrintWriter object.

An included servlet cannot set headers. If the included servlet calls a method that needs to set headers (such as cookies), the method is not guaranteed to work. As a servlet developer, you must ensure that any methods that might need direct access to headers are properly resolved. To ensure that a session works correctly, start the session outside the included servlet, even if you use session tracking.

Clarification

In iPlanet Web Server 4.1, the dispatcher.forward method may or may not throw an IllegalStateException when either Writer or OutputStream have been obtained. This behavior follows the 2.2 draft and is needed for JSP error page handling. It throws the exception only if the actual data has been flushed out and sent to the client. Otherwise, the data pending in the buffer is simply discarded.

The forward and include methods may throw a ServletException if the target URI is identified as an unsafe URI (that is, it includes insecure path characters such as //, /./, /../ and/., /.. (and also ./ for NT) at the end of the URI.

Request.getInputStream and getReader

There are two ways for a servlet to read the raw data posted by a client:

Clarification

A servlet hangs if it attempts to use an InputStream to read more data than is physically available. (To find how much data is available, use request.getContentLength.) However, if the servlet reads data using a BufferedReader returned from a call to getReader, the allowed content length is automatically taken into account.

You can also set the inputStreamLengthCheck parameter to true in the contexts.properties file to prevent this problem.


Table of Contents | Previous | Next | Index

Last Updated: 02/25/00 16:19:15

© Copyright © 2000 Sun Microsystems, Inc. Some preexisting portions Copyright © 2000 Netscape Communications Corp. All rights reserved.