Meaningless Drivel is fun!*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes forward() and include() 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 "forward() and include()" Watch "forward() and include()" New topic
Author

forward() and include()

R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
as core Servlets says that for these methods one should supply URL rel-ative to the server root.
so does it mean that we can include/forward any resource which is not on the same server or be precise on same JVM??
TIA


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi ravish,
it says "Relative" and if we read the servlet api it says
"Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server..."
for the RequestDispatcher object applied for forward() and include() calls.
so we can't use resources on the other server. the other tomcat server (if we use tomcat) i mean if we have two tomcat server running.
hope i understood ur question and answered as well.
regards
maulin.
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
hi,
Can I foward a request thru RequestDispatcher to a servlet that exists in another context but in the same server...
If yes how, bcoz, forward and include methods of RequestDispatcher takes a parameter of a relative path(begin from '/'), so how can we forard or include from a different context of the same server.


Best wishes,
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hi Maulin,
Thanks, if I am getting your answer right then it means that answer is NO, one can not forward/include resources from other server.
Now as Sam has raise a question, can we forward/include any resource from other context ?
What is my thinking that one server(let us say Tomcat) can have only one JVM running and one ServletContext.
Am I right?
Plz correct me if I am worng.
OR there is any way that we can have more than one JVM running from same Tomcat server and have more than one ServletContext ?
OR every web-Application has different ServletContext but one JVM?
waiting for reply .........
TIA
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Use servletContext.getContext() method to get another ServletContext and then get request dispatcher from this context and finally forward ur request..
Originally posted by Sam Cala:
hi,
Can I foward a request thru RequestDispatcher to a servlet that exists in another context but in the same server...
If yes how, bcoz, forward and include methods of RequestDispatcher takes a parameter of a relative path(begin from '/'), so how can we forard or include from a different context of the same server.


Groovy
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi,
my 2 cents. what i understand is,
servlet context is each web application we r having on the tomcat. so i fully agree with Pradeep.
each servlet runs in ONE jvm. so we have as many JVMs as number of servlets. thats true for any java program. whenever we run one java program with "java" command line it creates one instance of JVM. if run the same program second time (parallely i mean) it will create another JVM instance.
and of course the Tomcat process is only ONE.
hope i am clear here.
regards
maulin.
Sam Cala
Ranch Hand

Joined: Jan 03, 2001
Posts: 147
Originally posted by Maulin Vasavada:
hi,
my 2 cents. what i understand is,
servlet context is each web application we r having on the tomcat. so i fully agree with Pradeep.
<B>each servlet runs in ONE jvm. so we have as many JVMs as number of servlets. thats true for any java program. </B> whenever we run one java program with "java" command line it creates one instance of JVM. if run the same program second time (parallely i mean) it will create another JVM instance.
and of course the Tomcat process is only ONE.
hope i am clear here.
regards
maulin.


I tend to disagree that we have as many JVMs as number of servlets. To be precise, I should say that There is only one JVM in a server which handles all the requests and there are as many Contexts as Web applications in a server...
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

U r right.
Originally posted by Sam Cala:


I tend to disagree that we have as many JVMs as number of servlets. To be precise, I should say that There is only one JVM in a server which handles all the requests and there are as many Contexts as Web applications in a server...
Ricardo Cortes
Ranch Hand

Joined: Jan 23, 2002
Posts: 140
And to further elucidate, the servlet spec. states in SRV.14.2.8:
"There is one context per "web application" per Java Virtual Machine."


Sun Certified J2EE Architect for the J2EE Platform (Part 1)<br />Sun Certified Web Component Developer for the J2EE Platform<br />Sun Certified Programmer for the Java 2 Platform
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
so May I say that there are as many JVM and ServletContext as many WebApps are in one Tomcat ???
plz corrcet me ...
TIA
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Ravish Kumar:
so May I say that there are as many JVM and ServletContext as many WebApps are in one Tomcat ???
No.
For every web-app there is exactly one ServletContext. There is only one JVM (with an arbitrary number of web-apps/ServletContexts) unless you are using a distributed application server.
- Peter
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
thanks Peter
James Horton
Greenhorn

Joined: May 13, 2002
Posts: 5
If we are using a Distributed Application Server, then how do we access the ServletContext of a Web Application from another Server (and another JVM)? Plz let me know.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You can't -- the only thing shared between the servers is the HttpSession. If you need to communicate on the web-application level, for instance to co-ordinate caching, you'll have to use some other way: sockets, JMS, a database...
- Peter
James Horton
Greenhorn

Joined: May 13, 2002
Posts: 5
Thanks Peter.
What happens if in a distributed application, one request from the client goes to server1 and the subsequent request from the same client goes to server2? If I store an attribute in the SevletContext on server1, am I not handicapped in not able to access that attribute directly from ServletContext on server2 since the same ServletContext is not shared by both server1 and server2 for the same web application.
Correct me if I am wrong, I am writing purely from my theoritical understanding of this concept.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by James Horton:
What happens if in a distributed application, one request from the client goes to server1 and the subsequent request from the same client goes to server2?
The HttpSession (where you should store everything pertaining to that specific user that you want to keep track of in memory) will have been shared across the two: no problems there.
If I store an attribute in the SevletContext on server1, am I not handicapped in not able to access that attribute directly from ServletContext on server2 since the same ServletContext is not shared by both server1 and server2 for the same web application.
You won't be able to access it. So you have to take care in the types of attributes you store in the ServletContext. But I must say that in my career so far this has rarely been a restriction: stuff stored in the session tends to be cached data of various kinds, pools, Singleton instances, that kind of thing. Generally resources that inherently cannot be shared across JVMs or that are recreatable anywhere.
- Peter
James Horton
Greenhorn

Joined: May 13, 2002
Posts: 5
Thanks for the reply.
Can you please elaborate on this?
Originally posted by Peter den Haan:
Generally resources that inherently cannot be shared across JVMs or that are recreatable anywhere.
- Peter
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Examples are in the preceding sentence... cached data, for instance, is recreatable anywhere. Pools, object pools or connection pools or whatever, inherently cannot be shared across JVMs.
- Peter
James Horton
Greenhorn

Joined: May 13, 2002
Posts: 5
Thanks Peter. I appreciate your clarification.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: forward() and include()
 
Similar Threads
ReqDisptacher to forward/ include EXTERNAL content
RequestDispatcher
What's the different between "forward" and "include"?
how to specify domain in "url" , in getServletContext() . getContext("url")
Difference b/w forward and sendRedirect