Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext question.

 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, servletcontext are share by all servlets and jsp.
Question, one JVM has only one ServletContext or ..
lets say i install tomcat, so there will be only one Servletcontext for all servlets? or one web application under one root has a servletContext? Not sure.. becuase i am reading 2 books. One of them is kinda out dated and one is not. They just confuse me. ?_?
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think one web application will have one servletContext under one JVM. If the web application is distributable, there will be one servletContext per JVM.
Given servletContext will have all the servlets in THAT web application. If you want to access another servlet (in different web application) you will have to call getContext(java.lang.String uripath) method of ServletContext interface.
Hope this helps
Chintan
 
Harpreet Hira
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are as many servlet contexts per jvm as the number of webapplications deployed for a particular web server.
To avoid the confusion you can associate the servlet context with deployment descriptor.For each web application there is unique xml-DeploymentDescriptor(web.xml), so is a unique ServletContext. In a servlet context you get initialization parameters as -
ServletContext.getInitParameter(java.lang.String name)
Set different initialization params in DD. Calling this method, from different web applications within same jvm and in same webserver, would clear your doubts.

------------------
Harpreet S Hira
Sun Certified Java2 Programmer
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One JVM under one Tomcat Server can have one servlet context for each web application. So if 3 web applications are deplyed there will be 3 servlet contexts. But one servlet may be shared in different servlet contexts also....

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashik,
Your statement:
"But one servlet may be shared in different servlet contexts also"
My comments:
ServletContext.getServletContaxt (String path) can obtain servlet context of another web application. With this servlet context, a servlet can communicate with another web application.
Are we talking about the same/similar concept?
 
Chintan Rajyaguru
Ranch Hand
Posts: 341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think things are becoming confusing. Or may be I am confused. Tell me this:
What happens to the ServletContext of a distributed web applications? Distributed web applications operate in more than one VM. Does this mean that there will be a ServletContext for each VM? I don't think so because even for a distributed web application there is only one web.xml file and hence there is only one ServletContext. Am I right?
The following statement comes from SRV 10.6:
In distributed web containers, HttpSession instances are scoped to the particluar
VM servicing session requests, and the ServletContext object is scoped to the web
container�s VM.
This means that ServletContext runs in the same JVM as the webcontainer (say Tomcat). This gives another question, what happens to the web container of a distributed web application? Are there more web containers? or there is only one? Man, I am totally lost!!Please help me.

[This message has been edited by Chintan Rajyaguru (edited December 29, 2001).]
 
sun ram
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For one ServletContext for each web application
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quote from the 2.3 API JavaDocs (ServletConfig Interface):
There is one context per "web application" per Java Virtual Machine.
Regards, Guy
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys...i get the idea.
 
Ashik Uzzaman
Ranch Hand
Posts: 2373
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chintan :

My comments:
ServletContext.getServletContaxt (String path) can obtain servlet context of another web application. With this servlet context, a servlet can communicate with another web application.

Yes, i was telling that in my post.
In a distributed web application, more than one web container (say 2 tomcat servers) may be used for load balancing but not really necessary. Abt contexts, each URI starts with a specific context path. And one web application is simply dealing with one context. So the many web web applications u r using, the same many contexts they will use. But i'll look for a strict URL example from someone else... ...i myself m a little bit ...

------------------
Muhammad Ashikuzzaman (Fahim)
Sun Certified Programmer for the Java� 2 Platform
--When you learn something, learn it by heart!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic