Win a copy of Hands On Software Engineering with Python this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

ServletContext  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A)Which class implements ServletContext interface. Why is the concrete class not present in API?(src:Tomcat servlet API)

B)The only handle to ServletContext is via ServletConfig.i.e. getServletConfig().getServletContext()
getServletConfig():API doc says that this method invokes abive method to fetch ServletContext.
Why is ServletContext coupled with ServletConfig?

C)thread-safety of context attributes(src:HFSJ pg no:197)
It mentions that ServletContext should be syncrhonized in every Servlet.
Does it mean that there is seperate instance of ServletContext for every Servlet?
But its said that there is a single ServletContext for every web-app(lets not talk abt distributed).What does the preivous statement signify.
 
Rancher
Posts: 42974
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Which class implements ServletContext interface. Why is the concrete class not present in API?


It's up to container which package and classname to give to that class. It will also contain many container-specific methods, which should not be used by application developers.

This is common way of designing an API. JDBC works the same way - it consists of a number of interfaces (Connection, Statement, ResultSet, ...), and it's up to the driver implementation to create objects that implement these.

The only handle to ServletContext is via ServletConfig.i.e. getServletConfig().getServletContext()
getServletConfig():API doc says that this method invokes abive method to fetch ServletContext. Why is ServletContext coupled with ServletConfig?


It's not. The GenericServlet class -which just about all servlets extend via the HttpServlet class- also has a getServletContext method, so that's another way to get at it.

thread-safety of context attributes(src:HFSJ pg no:197)
It mentions that ServletContext should be syncrhonized in every Servlet.
Does it mean that there is seperate instance of ServletContext for every Servlet?
But its said that there is a single ServletContext for every web-app(lets not talk abt distributed). What does the preivous statement signify.


There is one ServletContext per web app. But servlets are multi-threaded, so whenever they access a mutable shared resource (like ServletContext), access to it must be protected, e.g. by synchronization.

Even if servlets were not multi-threaded, access to it would still need to be protected, because different servlets could use it at the same time.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since ServletContext is the shared object in an web application and should be available for all servlet instances running, it should be synchronized.

Otherwise, the purpose of having such an entity will not be saved.
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!