1

As I knew, Java ServletContext and Context Objects are common used inside Java EE program development. However, I'm not quite sure about what are the differences between them, especially the Context Class usages.

From what I understood, ServletContext is the Object which contains all initializations read from Web.xml so that all servlets in the web application can share those global configurations.

On the other hand, I usually only used Context Object when I needed to dynamically establish database connection with JNDI lookup. Beside that, I'm not clear about other possible usages of this class.

My main concern is does the "context" word of both ServletContext and Context classes represent the same thing or component inside web application?

4
  • What exactly is this Context class? Do you mean javax.naming.Context? Mar 28, 2013 at 3:04
  • Yes, Dear DiogoSantana, I meant the javax.naming.Context class Mar 28, 2013 at 3:12
  • 1
    Related: What exactly is Context in Java?
    – BalusC
    Mar 28, 2013 at 3:21
  • Thanks a lot, BalusC. Quite clear explanation in the link your provided. Mar 28, 2013 at 3:40

1 Answer 1

3

From the ServletContext javadoc

Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.

Basically it's an accessor to the servlet environment. Anything that is relevant to the servlet, you can get it from there. It's really only useful with a Servlet Container.

The JNDI Context javadoc

This interface represents a naming context, which consists of a set of name-to-object bindings. It contains methods for examining and updating these bindings.

This is an interface to a resource. You configure a resource with a some identifier which you can retrieve through this interface. You are not limited to using this only in a Servlet Container. For example, you might store a list of connected devices on your network in an LDAP repository. To access this repository, you could write custom code implementing the protocol and interacting with an LDAP server or you could use the LdapContext class, which implements the Context class. Or, you could use it to lookup a JDBC DataSource.

Don't let the fact that they have the same name bother you. They have different goals.

This answer might be of help for when you see Context in a different context.

5
  • Dear SD, thank you for your sharing. I got your point. Can you further explain some other usages of Context class as you mentioned it is not limited to using only in Servlet Container? Mar 28, 2013 at 3:13
  • @ShadowScorpion Added two example resources you could access. There are many more. Mar 28, 2013 at 3:20
  • I wouldn't say "unfortunate". That's just a typical suffix for classes following the facade design pattern.
    – BalusC
    Mar 28, 2013 at 3:23
  • @BalusC I agree, Context is a very encompassing word. It can mean a lot of things. Mar 28, 2013 at 3:25
  • Thanks again for everyone's sharing. This query will not bother me anymore. Mar 28, 2013 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.