permaculture playing cards*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletContext question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServletContext question." Watch "ServletContext question." New topic
Author

ServletContext question.

Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
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

Joined: Aug 19, 2001
Posts: 341
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


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
Harpreet Hira
Ranch Hand

Joined: Sep 27, 2001
Posts: 72
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

Joined: Jul 05, 2001
Posts: 2370

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!


Ashik Uzzaman
Senior Member of Technical Staff, Salesforce.com, San Francisco, CA, USA.
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
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

Joined: Aug 19, 2001
Posts: 341
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

Joined: Dec 18, 2001
Posts: 61
For one ServletContext for each web application


SCSecA,SCNA,SCSA,SCWCD,SCJP
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
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

Joined: Aug 26, 2000
Posts: 1242
Thanks guys...i get the idea.
Ashik Uzzaman
Ranch Hand

Joined: Jul 05, 2001
Posts: 2370

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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletContext question.
 
Similar Threads
marcus green mock exam
Aleksander's Clarification needed
ServletContext Confusion...
doubt in jsp implicit objects......
Servlet Context