Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between - servlet context, context and web application

 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's confusion with the terms - "web application", "servlet context" and "context". What is the difference between these terms? They are so heavily used in the api documentation!

For eg:
public java.lang.String getContextPath() - What is "context" here?

And look at the definition of ServletContextListener:
Implementations of this interface receive notifications about changes to the servlet context of the web application they are part of. To receive notification events, the implementation class must be configured in the deployment descriptor for the web application. - What is "servlet context" and "web application" here?

Further, look at the following definitions of few methods:
public void contextInitialized(ServletContextEvent sce):: Notification that the web application initialization process is starting.

public void contextDestroyed(ServletContextEvent sce):: Notification that the servlet context is about to be shut down.

Again, what do the above terms actually mean? If there's no difference, why use them causing lot of confusion?

I truly appreciate if anyone helps me resolve the confusion. Many thanks in advance!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2535
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Faisal,

A web-application is not the same as a servlet context. You should see a web-application as the combination of servlet's, filters and jsp's that are configured in a web.xml. Together with maybe some static resources and other objects they are packaged as a .war file. A .war file is an executable that can run in a servlet container.

The ServletContext is an object that is created when the web-application is started (in a servlet container) and destroyed when the web-application is taken out of service (undeployed or stopped). The ServletContext object can contain initialization parameters (from the web.xml) and you can store attributes in it so that all the servlets and jsps have access to those attributes (call it a global space).

Then the ContextPath (or context-root) of a web-application indicates the root of your web-application (have a look at the picture in a previous thread)

Does this clear some of you doubts?

Regards,
Frits
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your reply and explanation. I printed out the figure you've shown and referring it made many things clear. Thanks so much again!

However, I still have confusion regarding the term "context". Why can't the method getContextPath() be called getWebAppPath()? From the API doc of the method: Returns the portion of the request URI that indicates the context of the request. What actually does "context of the request" mean? Do both "web application" and "context" mean same here?

The term "web application" is very much straight forward and clear. "context" is just obscure and is many times used in many places.

I look forward to your reply! Many thanks in advance!
 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2535
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, I still have confusion regarding the term "context". Why can't the method getContextPath() be called getWebAppPath()? From the API doc of the method: Returns the portion of the request URI that indicates the context of the request. What actually does "context of the request" mean? Do both "web application" and "context" mean same here?


Let's look at the servlet spec (version 2.4)
SRV.11.1 Use of URL Paths
Upon receipt of a client request, the Web container determines the Web application to which to forward it. The Web application selected must have the the longest context path that matches the start of the request URL. The matched part of the URL is the context path when mapping to servlets.

Mapping of a request URL is done in two steps:
1) Map the URL to the web-application
2) Map the URL to the servlet inside the selected web-application

example:
You have three web-applications on your servlet-container, called FooStuff, FooStuffOne and FooStuffTwo.

Step 1) The request URL is http://localhost:8080/FooStuffOne/StartServlet/logon.do. Although all web-applications contain the same prefix (i.e. http://localhost:8080/FooStuff) The longest path from the request URL that matches a web-application is /FooStuffOne. This /FooStuffOne is now called the context path: from here you will map an URL to a Servlet.

Step 2) The remaining part of the request URL is now /StartServlet/logon.do this is now used to map to a Servlet. The mapping is done with the web.xml of the FooStuffOne web-application. The 4 rules for mapping are described below. The order of the mapping rules is important. If a URL can be mapped on rule 1 and rule 4, it will be mapped on rule 1

Map the URL to the Servlet
The following order is used to map an URL to a servlet. The URL is mapped against the <url-pattern>s of the <servlet-mapping> elements
1) Exact path - the URL pattern exactly matches a servlet-mapping
2) Longest prefix path - the longest match is used
3) Extensions - like *.jsp
4) Default - "/" single slash indicates default (if others didn't succeed)

Note: the querystring (e.g. ?name="bla") of an URL is never part of the mapping strategy http://localhost:8080/FooStuffOne/StartServlet/logon.do?name="bla"

Regards,
Frits
 
Faisal Ahmad
Ranch Hand
Posts: 355
Chrome Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for your replies and helping me understand. Still I have few questions.

What is the "Context" in ServletContext actually? I know ServletContext is an interface and container will create an object of such type etc. But, I am really not able to understand why the name - ServletContext. What exactly "context" means here? What is the idea behind "context" in the name ServletContext? When I look at the definition of ServletContext, it says: There is one context per "web application" per Java Virtual Machine.
Of course, I understand that statement technically. But what exactly the "context" means in that statement? The name Filter says clearly that it is for some information filtering. The name Servlet says clearly that it is for some server side application. ServletRequest says clearly that it is for carrying information for a servlet. Truly, I am not able to understand the meaning of the word ServletContext. Servlet's context? What is that exactly?

Please help me understand. Thanks so much in advance!

 
Frits Walraven
Creator of Enthuware JWS+ V6
Saloon Keeper
Pie
Posts: 2535
113
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the "Context" in ServletContext actually?

Yes, I agree the name doesn't exactly say what it is.

When you are learning implicit objects of a jsp it the implicit object associated with the ServletContext will be called "application", which makes more sense. The scope is the whole web-application, not only one request or a one Servlet or one Session, no, you can store attributes in a "application wide space"; available to all requests and all Servlets and all jsps.

Regards,
Frits
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic