Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

RequestDispatcher foward cross web application error

 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried calling the context without the leading slash?

Dave
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David,
From the getContext's Api :
The given path must be begin with "/"
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That may be the same but did you try with :
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, I didn't say it was right, I asked if you had tried it
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 294
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right, the topic should be moved in Tomcat forum.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Santosh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic