Thursday, April 3, 2008

How to get context of another war in tomcat

Lets say you have two web applications that run in the same tomcat server.
And for some weird reason you want to call code of the other war.


You can write something like this in your servlet/jsp to get the context of the other war:
javax.servlet.ServletContext context = application.getContext("otherWarName");


Now, when you have context to the war, you can access the objects under it. In example, you can include the other jsp in your request:
javax.servlet.RequestDispatcher rd = context.getRequestDispatcher("other.jsp");
rd.include(request, response);


Looks simple? Well, it is. It even works on jboss, but it does not in tomcat: application.getContext("otherWarName") returns null, so you'll get the NullPointerException.

The missing part is configured in the conf/server.xml. You'll need to define cross context for the applications that you want to be accessible from the other applications. Under the host element add:
<context path="/otherWarName" debug="0" reloadable="true" crosscontext="true"></context>


Recommended Reading

1. Professional Apache Tomcat 6 (WROX Professional Guides)
2. Tomcat 6 Developer's Guide
3. Pro Apache Tomcat 6


Original post

7 comments:

john bailo said...

How do I get or instantiate "application" ?

Tarlog said...

You don't need to instantiate it. In jsp you just have it. Just type 'application' inside your code.

In servlet, if you extend javax.servlet.GenericServlet, you have getServletContext() method. Otherwise, you can get it from the ServletConfig.

KODOK.782 said...

Hello..
I tried your tips but it doesn't work...
I'm Using NetBeans 6.7.1, JDK 1.6.0_14, Java Hotspot Client VM 14.0-b16, Tomcat 6.0.24

I've done step by step what you said, but I allways got null value when executing "application.getContext("ProjectName");"

it seems that Tomcat cannot load/understand context tag in server.xml file.
This is Tomcat log when I start Tomcat server :

Using CATALINA_BASE: D:\-= KOMPONEN JAVA =-\apache-tomcat-6.0.24
Using CATALINA_HOME: D:\-= KOMPONEN JAVA =-\apache-tomcat-6.0.24
Using CATALINA_TMPDIR: D:\-= KOMPONEN JAVA =-\apache-tomcat-6.0.24\temp
Using JRE_HOME: C:\Program Files\Java\jdk1.6.0_14
Using CLASSPATH: D:\-= KOMPONEN JAVA =-\apache-tomcat-6.0.24\bin\bootstrap.jar
Listening for transport dt_shmem at address: tomcat_shared_memory_id
Feb 1, 2010 3:11:48 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:\Program Files\Java\jdk1.6.0_14\bin;.;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Borland\Delphi7\Bin;C:\Program Files\Borland\Delphi7\Projects\Bpl\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\80\Tools\BINN;C:\Program Files\MySQL\MySQL Server 5.1\bin;c:\Program Files\Microsoft SQL Server\90\Tools\binn\
Feb 1, 2010 3:11:48 PM org.apache.tomcat.util.digester.Digester endElement
WARNING: No rules found matching 'Server/Service/Engine/Host/context'.
Feb 1, 2010 3:11:48 PM org.apache.tomcat.util.digester.Digester endElement
WARNING: No rules found matching 'Server/Service/Engine/Host/context'.

Feb 1, 2010 3:11:48 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Feb 1, 2010 3:11:48 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 689 ms
Feb 1, 2010 3:11:48 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Feb 1, 2010 3:11:48 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.24
Feb 1, 2010 3:11:48 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor FirstServletProject.xml
Feb 1, 2010 3:11:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor host-manager.xml
Feb 1, 2010 3:11:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor manager.xml
Feb 1, 2010 3:11:49 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor SessionServletProject.xml
Feb 1, 2010 3:11:49 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory docs
Feb 1, 2010 3:11:49 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory examples
Feb 1, 2010 3:11:49 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory ROOT
Feb 1, 2010 3:11:49 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Feb 1, 2010 3:11:50 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Feb 1, 2010 3:11:50 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/62 config=null
Feb 1, 2010 3:11:50 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 1120 ms


thank you... sorry for my bad english

Tarlog said...

Hi Niksen,

I didn't try cross-context in Tomcat 6,
but it seems to be a little different from the previous releases.

See http://tomcat.apache.org/tomcat-6.0-doc/config/context.html

I'll try to run it myself, when I have time. If you figure it out before, I'll appreciate if you post a remark here.

KODOK.782 said...

I'm sorry, until now I haven't success to make it run in Tomcat 6. I've tried step by step in tomcat documentation, but it doesn't work.

Smita said...

Hello,

I am facing same issue as the one described in http://tarlogonjava.blogspot.com/2008/04/how-to-get-context-of-another-war-in.html. Have you got any solution to this? It will be highly helpful!

Thanks!

Mehmet Fatih Ercik said...

To get contex

application.getContext("otherWarName");

"otherWarName" should be "/otherWarName" (start with /)
and both aplications must have crossContext="true" atribute