Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "Context" in "ServletContext" mean?

Tags:

java

servlets

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"?

like image 929
Faisal Avatar asked Jan 19 '11 15:01

Faisal


People also ask

What is a servlet context?

Interface ServletContext. public interface ServletContext. 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. There is one context per "web application" per Java Virtual Machine.

What is context in web application?

WebApplicationContext is used to create web applications. ApplicationContext is the parent of the WebApplicationContext interface. WebApplicationContext is the child of the ApplicationContext interface. In the case of ApplicationContext, we have to create and destroy the container objects manually.

What is context and config?

ServletConfig is used for sharing init parameters specific to a servlet while ServletContext is for sharing init parameters within any Servlet within a web application.

Why servlet context is used?

The object of ServletContext provides an interface between the container and servlet. The ServletContext object can be used to get configuration information from the web. xml file. The ServletContext object can be used to set, get or remove attribute from the web.


1 Answers

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".

like image 129
JB Nizet Avatar answered Oct 13 '22 12:10

JB Nizet