21

Method getServletContextName() returns the name of the "web application". That means, "ServletContext" is nothing but "web application". Ok.

API defines:

a ServletContextListener receives notifications about changes to the servlet context of the web application they are part of.

What does "servlet context" of the "web application" mean? What actually is "Context" in "ServletContext"?

lweller
  • 11,077
  • 3
  • 34
  • 38
Faisal
  • 645
  • 3
  • 11
  • 24
  • 2
    Related: [What exactly is a context in Java?](http://stackoverflow.com/questions/3918083/what-exactly-is-a-context-in-java) – BalusC Jan 19 '11 at 15:46

6 Answers6

22

The name is indeed, IMO, very badly chosen.

We must read ServletContext as "the general context of a servlet API based web application". Whereas we must read ServletConfig (another standard class) as "The config of a servlet".

They should IMO have named ServletContext as "WebAppContext" or "ApplicationContext", and ServletConfig as "ServletContext".

BTW, in JSP, the scope linked to a JspPage is named "page"; the scope linked to a HttpServletRequest is named "request"; the scope named to a HttpSession is named "session", and the scope linked to a ServletContext is named ... "application".

sofs1
  • 3,834
  • 11
  • 51
  • 89
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 2
    I agree with you, ServletContext should have been named either WebAppContext or ApplicationContext. Because, the "context" (contextual information) isn't only meant for servlet. It is also meant for JSP. However, at the end JSP is a servlet. So, they might have called it ServletContext. :) But, ApplicationContext sounds more appropriate. – Faisal Jan 19 '11 at 16:06
10

"Context" means.. context - it has contextual information and functionality for a particular web application:

  • application-wide parameters
  • application event listeners
  • metadata about the application

ServletContext is the context of a Java web application (because it uses servlets)

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Thanks! But, what does "servlet context" of the "web application" mean? – Faisal Jan 19 '11 at 15:36
  • Thanks so much! Very nice explanation. But, "ApplicationContext" would have been more appropriate than "ServletContext". Because, the context(contextual information) isn't only meant for servlet, but for a JSP as well. – Faisal Jan 19 '11 at 16:08
  • "servlet context" gives sense of "contextual information meant for a servlet". Ok. Do we have any other "contextual information" that is not meant for servlet? – Faisal Jan 19 '11 at 16:16
  • for servletS. It's meant for all servlets in the application. Check the methods of `ServletContext` for all the information you can get and set there. – Bozho Jan 19 '11 at 18:05
  • 1
    and btw, JSPs were introduced at a later stage. And they are compiled to servlets in the end. So it's both historically, and theoretically correct. – Bozho Jan 19 '11 at 18:14
6

Context means web app here.

A ServletContextListener gets notified when a Web App is started or stopped. That way you can run tasks automatically that need to be run when the web app starts or stops.

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
  • Then, look at what API says: "servlet context" of the "web application". What does this sentence mean? Looks strange, right? – Faisal Jan 19 '11 at 15:35
  • @Faisal A web app can also contain non-servlet stuff. Servlet Context refers to what's configured via web.xml – Sean Patrick Floyd Jan 19 '11 at 15:37
4

A ServletContext is the runtime representation of the web application.

Faisal
  • 645
  • 3
  • 11
  • 24
1

ServletContext implies Context or Runtime environment of servlet. Servlets runs in Servlet containers like tomcat. Servlet container creates and provides runtime environment for the servlet to get executed and it manages its lifecycle. It also holds other information, as :-

  • application-wide parameters
  • application event listeners
  • metadata about the application
Mangu Singh Rajpurohit
  • 10,806
  • 4
  • 68
  • 97
0

ServletContext is interface that contain a set of methods to communicate with its own servlet container.

  • Context that stand as a one per web application on per jvm .

That allow servlets to gain access to the context for various parts of the server to communicate.

Life Cycle of ServletContext

  1. Servlet container reads the DD (Deployment Descriptor – web.xml) and creates the name/value string pair for each when web application is getting started.
  2. Container creates the new Instance of ServletContext.
  3. Servlet container gives the ServletContext a reference to each name/value pair of the context init parameter.
  4. Every servlet and JSP in the same web application will now has access to this ServletContext. ServletConfig vs ServletContext

    • ServletContext is available to all servlet & jsp in web application while ServletConfig will be available only specific servlet.
    • Servlet config is one per servlet and Servlet context is one per web application
Sonu patel
  • 353
  • 1
  • 8