• Post Reply Bookmark Topic Watch Topic
  • New Topic

cross-context request dispatching

 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

Can any one explain me how to include or forward a servlet which is in different wep application using RequestDispatcher?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, this (cross context communication) is not spelled out in great detail in the servlet spec. This means that behavior will vary from container to container.

The syntax for doing it is easy.
Call getServletContext("contextName").getRequestDispatcher(...) and use the request dispatcher to forward to the other application.
 
Mike Kaufman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As per Ben Souther's reply, you need ServletContext.getServletContext(String).

But be aware that it can return null. The javadoc says the container can restrict which contexts can be accessed this way, and returns null if you're not permitted to do cross-context calls to the specified context (even assuming it exists). So you might, for example, need to set a container-specific configuration option to explicitly permit such cross-context dispatching, either in general or to the particular context involved.

There are other issues with cross-context dispatching that can be tricky due to being somewhat under-specified, and which you might need to watch out for e.g. how session-handling works across multiple contexts. I believe these issues caused a fair bit of grief for "portlets" in their time, though each new version of the spec tends to clear things up a bit.
 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ben Souther & Mike Kaufman for your reply.

Still i have got of couple doubts.
1) I couldn't find any method like getServletContext(string contextname) which takes context name as a parameter.
2) If so where can i get the name of the ServletContext of other web application.

Please clarify these doubts...
 
Mike Kaufman
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The getServletContext(String) method is on ServletContext itself.

That is, from the ServletContext of the servlet or filter that is going to do the dispatching (as given by its ServletConfig/FilterConfig), and subject to security restrictions, you can retrieve other ServletContexts and then use their own getRequestDispatcher to obtain request dispatchers for their resources as usual.

The string argument to ServletContext.getServletContext is not a "context name", but is a path relative to the container's document root. You get back the ServletContext of whichever web-app that path would invoke.

See the Javadoc of ServletContext for full explanation and details.
 
kamalakannan kamal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mike & Ben.

I tried getContext() method and got null as you mentioned.
:roll:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!