Introduction to Java servlets  Installing and configuring servlets

Chapter 22: Creating Java Servlets

Writing servlets for EAServer

You can implement servlets for EAServer as you would for any other server that follows the Java servlet specification. Servlets for EAServer can be coded to the standard Java servlet API and use classes in the javax.servlet and javax.servlet.http packages. This section lists coding information specific to EAServer and describes EAServer’s extensions to the standard servlet API.

Connection caching

Servlets can use these classes to retrieve cached connections:

For more information, see “Using Java Connection Manager classes”.

Component invocations

Servlets in EAServer can instantiate component instances using the same technique used within EJB or Java/CORBA components. Use the EJB technique when portability to other J2EE servers is required.

Using the EJB technique

To invoke component methods, use the lookup method in class javax.naming.InitialContext to resolve the Bean’s home interface, then create a reference to the remote interface. For example:

import javax.ejb.*;
import javax.naming.*;

QueryBean _queryBean;
String _queryBeanName = 
   "java:comp/env/ejb/querybean" ;
Context ctx = getInitialContext();
try {
    Object h = ctx.lookup(_queryBeanName);
    QueryBeanHome qbHome = (QueryBeanHome)
    javax.rmi.PortableRemoteObject.narrow(h,        QueryBeanHome.class);
    _queryBean = qbHome.create();
catch (NamingException ne) 
    System.out.println("Error: Naming exception: "
       + ne.getExplanation() + ne.toString());
    throw new Exception(
      "Lookup failed for EJB " + _queryBeanName);

For more information on the EJB client interfaces, see Chapter 8, “Creating Enterprise JavaBeans Clients.” You can define an EJB reference in the Web application properties to alias the servlet name used in your source code. The EJB reference allows the Web application to be deployed on another J2EE server without changing your servlet code. See “EJB references” for more information.

Using the Java/CORBA technique

To invoke component methods, create an ORB instance to obtain a proxy for the components, then invoke methods on the proxy object reference. For components on the same server, call the string_to_object method with the IOR string specified as Package/Component. For example, the fragment below obtains a proxy object for a component called Payroll that is installed in the Finance package:

java.util.Properties props = new java.util.Properties();
ORB orb = ORB.init((java.lang.String[])null, props);
Payroll payroll = 

By default, servlets run without a user name and password. A servlet client, authenticated by EAServer, runs with the client’s user name and password. If an unauthenticated servlet client invokes a component method, the component is instantiated without a user name and password. If roles limit access to a component or method and the servlet has no user name, a method invocation attempt fails. To specify a user name, use this syntax:


You can retrieve the system user name and password with these methods in class com.sybase.CORBA.ORB, which both return strings:

When called from components, string_to_object returns an instance running on the same server if the component is locally installed; otherwise, it attempts to resolve a remote instance using the naming server.


If possible, servlets should be coded to be thread-safe, such that the service method can be called concurrently from multiple threads. This threading model is the default for servlets running in EAServer. In most cases, it offers the best performance. If your servlet cannot support this threading model, you must do one of the following to ensure that the servlet executes safely in EAServer:


Servlets can log error messages or other text to the EAServer servlet log file, using the standard servlet log methods in the ServletContext class (or the equivalent methods in the GenericServlet class). EAServer records servlet log messages in the httpservlet.log file, located in EAServer’s bin subdirectory. If you define additional servers, the name of the servlet log file is prepended with the server name. For example, if you create a server named Test_server, then servlet messages for that server are directed to the Test_serverhttpservlet.log file.

To enable trace logging in the EAServer servlet execution engine, add the property in the Server Properties dialog box on the Advanced tab, and set it to true.

Error pages

You can customize error and exception reports that are sent to clients by creating error pages. When the servlet engine detects an error or catches an exception thrown by a servlet, it searches for a corresponding error page to handle the response. You can declare error pages for a Web application, or at the server level.

This example illustrates how to declare an error page for a Web application in the deployment descriptor:


The location is the path relative to the Web application’s context root. For example, /etc/404.html corresponds to this file in your EAServer installation directory, where web-app is the name of the Web application:


For information about how to use EAServer Manager to set up an error page for a Web application, see “Error pages”.

To set up server-level error pages, see on page 553, in the EAServer System Administration Guide.

Request dispatching

A RequestDispatcher instance allows one servlet to invoke another and either forward a request, or include the target servlet’s response with its own. The RequestDispatcher interface provides methods to accomplish both. To obtain an object that implements the RequestDispatcher interface, use one of these ServletContext methods:

To forward a request, the initial servlet calls the forward method of the RequestDispatcher interface. The target servlet returns the response. This method can be called only if no output has been committed to the client. Before the forward method returns, the response must be committed and closed by the servlet container.

To include a target servlet’s response with its own, the initial servlet calls the include method of the RequestDispatcher interface. The target servlet has full access to the request object but can write only to the ServletOutputStream or Writer of the response object and it cannot modify the response headers. The target servlet can commit a response by either writing past the end of the response buffer, or explicitly calling the flush method of the ServletResponse interface.

URL interpretation

The ServletContext and ServletRequest objects both contain methods to retrieve a RequestDispatcher instance. ServletContext methods require an absolute URL. ServletRequest methods can interpret a relative URL. Both URL types must follow these guidelines:

A ServletContext.getRequestDispatcher URL must begin with a forward slash (‘/’). If a ServletRequest.getRequestDispatcher URL begins with a forward slash, the servlet engine interprets it as an absolute URL. Otherwise, the servlet engine appends the relative URL to the current request’s URI path. For example, if the current request is /catalog/garden.html and the relative URL is sports.html, then the new URL is /catalog/sports.html.


EAServer’s servlet engine passes all servlet invocation requests through a RequestDispatcherobject instance. When the servlet engine receives a request from a client, it calls the RequestDispatcher.service method. This method loads, initializes, and handles instance pooling of single-threaded servlets. It also invokes the servlet and handles errors.

Static content

A RequestDispatcher instance would typically be used for servlets and JSPs, but it can also be used for static content. If the servlet engine forwards a request to a static content RequestDispatcher, the RequestDispatcher must set the response status, the response headers, and the response data. If a static content RequestDispatcher is called to set the data for the current request, it only needs to return the content of the static page.

Response buffering

The Java servlet API supports response buffering that allows the servlet to control how the servlet container buffers responses, and when to send a response to a client. The ServletResponse interface provides these methods that allow a servlet to access buffering information:

See the Java Servlet Specification, v2.3 for detailed information about using response buffering.

Encoding responses and double-byte characters

When you compile a Java servlet, the characters are encoded according to the locale of your machine unless you specify encoding in the javac compile command. When a client sends a request from a browser, the parameters are always ISO 8859-1 encoded.

To provide a client’s browser with the encoding information it needs to translate the content of a response correctly, declare the encoding in the response header. If you specify the content type without the encoding information, for instance:


the client’s browser assumes that the content is ISO 8859-1 encoded. If the content has been encoded using some other standard, the client’s browser does not translate the data correctly. This example specifies the double-byte character set big5, the encoding name of traditional Chinese characters:


To encode the response content, compile the servlet with this encoding option:

javac -encode iso-8859-1 <java source file>

or convert static strings within the servlet code, for instance:

String origMsg = "<double-byte character string>";
String newMsg = new String(origMsg.getBytes(),                            "iso-8859-1");

Copyright © 2005. Sybase Inc. All rights reserved. Installing and configuring servlets