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

How can a RequestDispatcher call a servlet in another web application?

 
Yorck Zhou
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the servlet specification 2.3 there is such a statement:
To illustrate this requirement with an example: if a servlet uses the RequestDispatcher to call a servlet in another web application, any sessions created for and visible to the callee servlet must be different from those visible to the calling servlet.
 
Anthony Watson
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the code below to get the servlet context reference. From there, you can retrieve an appropriate RequestDispatcher.

this.getServletContext().getContext(uripath)
 
Yorck Zhou
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Anthony!
But when I tried the following fragment code in the Tomcat 4.1. It return a NullpointerException. Is it a bug of Tomcat 4.1?

try{
ServletContext rootContext = getServletContext().getContext("http://localhost:8080/");
System.out.println( ( rootContext.getAttribute("root") ).toString() );
}catch(Exception e){
System.out.println(e);
}
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to specify the context path of your Web application, not the URL that accessing your website.

Nick
 
Yorck Zhou
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I have also tried a uri as an argument of the getContext() method, but it is still return a NullpointerException!

In the servlet API, I found the following statement about the getContext():
In a security conscious environment, the servlet container may return null for a given URL.


So, should make any change in the Tomcat to make it work?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic