29.01.2013 Views

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

The servlet configuration<br />

This object must implement the ServletConfig interface and is provided by the<br />

servlet container as a parameter of the init method of the servlet. The servlet<br />

container is responsible for providing a ServletConfig object.<br />

The servlet container uses this object to pass configuration information to its<br />

servlet instances, such as the name and the value of a parameter located in the<br />

servlet container. The ServletConfig object is accessible to the servlet through its<br />

getServletConfig() method. Moreover, a ServletConfig object allows the access<br />

to a ServletContext object.<br />

The servlet context<br />

This object implements the ServletContext interface and is provided to the servlet<br />

within the ServletConfig object. During its life cycle, a servlet can access its<br />

servlet context through the getServletContext() method.<br />

The servlet container is also responsible for providing an implementation of this<br />

interface.<br />

The servlet context provides information about its environment to a servlet. It can<br />

be shared by a group of servlets and provides access to the following:<br />

► A set of attributes: a set of name/object pairs that can be shared between<br />

servlets and other components to maintain persistency. A servlet can either<br />

add or remove an attribute or simply change its content. A servlet can also<br />

access its init parameters, which are name/value pairs.<br />

► Resources: servlets sharing the same context can also share resources such<br />

as files. They can access these resources via their servlet context.<br />

► A log file: a servlet can write a set of events to a log file.<br />

► A request dispatcher: this object, created and managed by the servlet<br />

container, allows a servlet to forward its request to another servlet or to<br />

include the content of another servlet into its own response. It allows servlets<br />

sharing the same context to communicate with each other.<br />

There can be several servlet context objects in a servlet container at the same<br />

time, each managing its pool of servlets. Each servlet instance is running in one<br />

of these contexts and all the servlet instances running in the same context will<br />

share the same resources. Following that, a servlet class may have more than<br />

one instance in a container, with each of these instances attached to a different<br />

servlet context.<br />

Finally, the servlet context is the way for the servlet to interact with its<br />

environment (its container, but also other resources) without having to reconsider<br />

its own logic. Every servlet container has at least a default servlet context.<br />

Chapter 7. Working with servlets 219

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!