Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Java
Expertise: Advanced
Mar 2, 2005



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Share Information with Other Web Resources Using ServletContext

The Servlet API provides the class javax.servlet.ServletContext, which has methods that work with data stored in context. Any servlet can store Java objects in that context and other servlets can use them. The objects are stored with a String key for uniqueness.

Here's how it works:

  1. Use setAttribute(String key, Object value) to store an attribute in context. key is the name of the attribute and value is the Java object to be stored.
  2. Use getAttribute(String key) to retreive the Java object corresponding to the value of key in context. If no object is found, null is returned.
  3. Remove attributes from context using remove(String key).
ServletContext can be used in other ways besides data sharing—for instance, working with RequestDispatcher or logging into the servlet log file.
Amit Tuli
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date