?

Log in

No account? Create an account
How to get context of another war in tomcat - Мишка

> Recent Entries
> Archive
> Friends
> Profile
> Tarlog on Java

Links
Tarlog on Java

August 21st, 2007


Previous Entry Share Next Entry
08:42 am - 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" />

Restart tomcat and it will work.

I'm almost sure that there is a way to define cross context in tomcat without changing the server.xml, but I didn't figure it out yet.

Tags:

(11 comments | Leave a comment)

Comments:


[User Picture]
From:limilom
Date:August 21st, 2007 07:45 am (UTC)
(Link)
Put a file with this line under your Catalina/localhost/otherWarName.xml ?
[User Picture]
From:tarlog
Date:August 21st, 2007 07:57 am (UTC)
(Link)
Ok, this is another option. But I believe that there should be an option to define it under the war itself. I mean under WEB-INF of the war.
Am I wrong?
[User Picture]
From:khitrenovich
Date:August 21st, 2007 11:39 am (UTC)
(Link)
Don't think so. The web server administrator MUST be aware of the fact that some application may access others - so it should be explicitly stated.

Security reasons, u'now... :)
[User Picture]
From:tarlog
Date:August 21st, 2007 11:41 am (UTC)
(Link)
Well, it works in jboss.
[User Picture]
From:khitrenovich
Date:August 21st, 2007 11:43 am (UTC)
(Link)
Well, it doesn't in Tomcat :)))
[User Picture]
From:tarlog
Date:September 4th, 2007 01:24 pm (UTC)

Automatic testing of web service

(Link)
Was it you, who asked me about automatic testing of web services?
[User Picture]
From:limilom
Date:September 4th, 2007 03:29 pm (UTC)

Re: Automatic testing of web service

(Link)
Ага
[User Picture]
From:tarlog
Date:September 4th, 2007 06:10 pm (UTC)

Re: Automatic testing of web service

(Link)
Nashel chto nibud'?

[User Picture]
From:limilom
Date:September 4th, 2007 06:32 pm (UTC)

Re: Automatic testing of web service

(Link)
На днях покупаю http://www.itko.com/site/lisa/, позволяет тестировать SOAP/WEB etc.

Leonid
[User Picture]
From:tarlog
Date:September 4th, 2007 06:39 pm (UTC)

Re: Automatic testing of web service

(Link)
U nih est' evaluation version?

Ja tut nachal kovirjatsja s HP Service Test (bivshee Mercury Service Test)
Poka ono mne ne sloshkom nravitsja, no mne ee pokupat' ne nado, skoree naoborot.
[User Picture]
From:tarlog
Date:September 4th, 2007 06:49 pm (UTC)

Re: Automatic testing of web service

(Link)

> Go to Top
LiveJournal.com