aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes How can a RequestDispatcher call a servlet in another web application? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "How can a RequestDispatcher call a servlet in another web application?" Watch "How can a RequestDispatcher call a servlet in another web application?" New topic
Author

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

Yorck Zhou
Greenhorn

Joined: Jan 04, 2003
Posts: 22
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.


----------------------------------<br />SCJP SCWCD<br />dive into EJB, drinking hot Java
Anthony Watson
Ranch Hand

Joined: Sep 25, 2003
Posts: 327
Use the code below to get the servlet context reference. From there, you can retrieve an appropriate RequestDispatcher.

this.getServletContext().getContext(uripath)


Anthony W.<br />MCP, SCJP 1.4, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
Yorck Zhou
Greenhorn

Joined: Jan 04, 2003
Posts: 22
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

Joined: Nov 07, 2003
Posts: 4982
You need to specify the context path of your Web application, not the URL that accessing your website.

Nick


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Yorck Zhou
Greenhorn

Joined: Jan 04, 2003
Posts: 22
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?
 
jQuery in Action, 2nd edition
 
subject: How can a RequestDispatcher call a servlet in another web application?