This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Tomcat and the fly likes RequestDispatcher foward  cross web application error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Products » Tomcat
Bookmark "RequestDispatcher foward  cross web application error" Watch "RequestDispatcher foward  cross web application error" New topic
Author

RequestDispatcher foward cross web application error

Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
Hi,
I am not able to forward the request to another Web Application running on my tomcat server. I am facing one problem related to ServletContext's getContext() method and getting NullPointerException. chapter02Context at line number 3 is null.

1. ServletContext context = getServletConfig().getServletContext();
2. System.out.println("context="+context);
3. ServletContext chapter02Context = context.getContext("/chapter02");
4. System.out.println("chapter02Context="+chapter02Context);
5. RequestDispatcher chapter02RD = chapter02Context.getRequestDispatcher("/index.html");

6. System.out.println("chapter01RD="+chapter02RD);
7. chapter02RD.forward(req, res);

Here is the brief description I am trying to run this servlet on jakarta-tomcat-5.0.30. I have two web applications "chapter02" and "chapter04" and I am trying to forward the request from web application "chapter04" to "chapter02" index.html page using above code.

In jakarta-tomcat-5.0.30\conf\server.xml I have made these changes (refer line 1 and 2 below)..

<?xml version='1.0' encoding='utf-8'?>
<Server debug="1">
...
<Service debug="1" name="Catalina">
...
</Service>

1. <Context path="/chapter02" docBase="C:\jakarta-tomcat-5.0.30\webapps\chapter02" crossContext="true" debug="0" reloadable="true" privileged="true"/>
2. <Context path="/chapter04" docBase="C:\jakarta-tomcat-5.0.30\webapps\chapter04" crossContext="true" debug="0" reloadable="true" privileged="true"/>

</Server>

Please let me know where I am making mitake, the correct format of string parameter of context.getContext(String URI). Because when I am trying to call context.getContext("/chapter02"), its returning null.

Thanks
Santosh Kumar


Santoh Kumar<br /> India<br />SCJP 1.4 <br />SCWCD 1.4
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Have you tried calling the context without the leading slash?

Dave
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

David,
From the getContext's Api :
The given path must be begin with "/"


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

That may be the same but did you try with :
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by Satou kurinosuke:
From the getContext's Api :
The given path must be begin with "/"


Agreed. But a leading slash means relative to the same app. Its worth trying it without leading slash.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

Yep, I didn't say it was right, I asked if you had tried it
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
Hi,
I tried both removing leading slash from getServletContext() and changing the properties "docbase" of context element in server.xml file, but nothing worked same error NullpointerException. The "chapter02Context" object is null, it means getServletContext is not working for across application.

Any body having any idea, what else can be done?

dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
It's allowed by specification to do not forward to other application. So some servlet containers will work, some won't. I'd suggest to use sendRedirect for more portable solution.


Tough in space?, <a href="http://tjws.sf.net" target="_blank" rel="nofollow">Get J2EE servlet container under 150Kbytes here</a><br />Love your iPod and want it anywhere?<a href="http://mediachest.sf.net" target="_blank" rel="nofollow">Check it here.</a><br /><a href="http://7bee.j2ee.us/book/Generics%20in%20JDK%201.5.html" target="_blank" rel="nofollow">Curious about generic in Java?</a><br /><a href="http://7bee.j2ee.us/bee/index-bee.html" target="_blank" rel="nofollow">Hate ant? Use bee.</a><br /><a href="http://7bee.j2ee.us/addressbook/" target="_blank" rel="nofollow">Need contacts anywhere?</a><br /><a href="http://searchdir.sourceforge.net/" target="_blank" rel="nofollow">How to promote your business with a search engine</a>
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

It's allowed by specification to do not forward to other application. So some servlet containers will work, some won't.


100% correct.

Once I tried with tomcat. It didn't work for me.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Rahul Bhattacharjee
Ranch Hand

Joined: Nov 29, 2005
Posts: 2308
As you are not getting the context of the application..you application is not initialized so all the further statements are of no use..frist try to make a proper web context and run a sample servlet..after that try with you full code..that would be a better idea when struck..if struck for making and defining a new context..just make a war file and put it in the webapps for self-deploy..that would create the context for you and with that you can try out your code..


Rahul Bhattacharjee
LinkedIn - Blog
Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
Hi All,
Thanks for your reply, I tried even making war file chapter02.war and chapter04.war and using the cross webapplication forward. But it did not work. I hope this is the problem with tomcat web server only, bacause specification says that its possible.
Allthough sendRedirect("/chapter02/index.html") worked.

Thanks once again all your suggestion...

Now this thread can be closed.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Has anybody thought :
"Why does Tomcat provide a crossContext="true" option " ?

Did you read Tomcat's documentation ?
http://tomcat.apache.org/tomcat-5.0-doc/config/context.html
[ May 18, 2006: Message edited by: Satou kurinosuke ]
dema rogatkin
Ranch Hand

Joined: Oct 09, 2002
Posts: 294
You are right, the topic should be moved in Tomcat forum.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Ok, I've managed to make it work, but I'm not convinced.
I'd be glad to here from Tomcat's specialists.

I have two applications:
/webapps/servlet/
/webapps/jstl/
And I'll try to jump from the servlet application to the jstl application, using Tomcat's crossContext.

This is what I did (under Tomcat 5.5) :

1. Create a context files for each application, respectively called servlet.xml and jstl.xml in $CATALINA_HOME/conf/[enginename]/[hostname]/

2. Content of servlet.xml


3. Content of jstl.xml


4. I defined context parameters in my jstl application (web.xml):


5. Then, I tried to access the jstl application context from the servlet application:


4. Execute the servlet application, and here's the log:
ctx=org.apache.catalina.core.ApplicationContextFacade@c791b9
jstlCtx=org.apache.catalina.core.ApplicationContextFacade@3020ad
[JSLT] myName=Bob
[JSTL] myCat=Qoo
jstlDispatcher=org.apache.catalina.core.ApplicationDispatcher@1db05b2

I was successfully forwarded to my jstl's jsp file.

You can check if your application's context is shared in Tomcat's Administration tool.

You'll have to read this document :
http://tomcat.apache.org/tomcat-5.5-doc/config/context.html
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Originally posted by Satou kurinosuke:
...
I'd be glad to here from Tomcat's specialists. ...


I'll move this to the Apache Tomcat site for you.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Santosh Kumar
Greenhorn

Joined: Jan 03, 2002
Posts: 28
Hi
Thanks Satou kurinosuke for your detailed explanation. I went through the URL provided you and I found that Tomcat this statment "Please note that for tomcat 5, unlike tomcat 4.x, it is NOT recommended to place <Context> elements directly in the server.xml file.". And this was the mistake I made. Now its working thanks.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: RequestDispatcher foward cross web application error