Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

forward() and include()

 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8933
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to further elucidate, the servlet spec. states in SRV.14.2.8:
"There is one context per "web application" per Java Virtual Machine."
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Peter
 
James Horton
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Peter. I appreciate your clarification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic