Showing posts with label tomcat. Show all posts
Showing posts with label tomcat. Show all posts

Monday, September 26, 2011

Simple Batch Script to Run Tomcat

Sometimes it's a good idea to separate the actual Tomcat installation from the running configuration. Actually Tomcat comes with this ability built-in:
catalina.base
system property defines the location of the configuration, while
catalina.home
system property defines the location of the Tomcat installation.
And here comes the basic script that runs the Tomcat:

@echo off
setlocal

SET TOMCAT_BASE=c:\work\tomcats\tomcat1
SET TOMCAT_HOME=C:\tps\apache-tomcat-7.0.16
SET TOMCAT_CLASSPATH=%TOMCAT_HOME%\bin\bootstrap.jar;%TOMCAT_HOME%\bin\tomcat-juli.jar;%JAVA_HOME%\lib\tools.jar
SET JAVA_ENDORSED_DIRS=%TOMCAT_HOME%\endorsed
SET JAVA_OPTS=
SET JAVA_OPTS=%JAVA_OPTS% -Djava.util.logging.config.file="%TOMCAT_BASE%\logging.properties" -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
SET JAVA_OPTS=%JAVA_OPTS% -Djava.io.tmpdir="%TOMCAT_BASE%\temp"
SET JAVA_OPTS=%JAVA_OPTS% -Dcatalina.base="%TOMCAT_BASE%" -Dcatalina.home="%TOMCAT_HOME%"
SET JAVA_OPTS=%JAVA_OPTS% -Djava.endorsed.dirs="%JAVA_ENDORSED_DIRS%"

REM SET JAVA_OPTS=%JAVA_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,address=7878,server=y,suspend=n

%JAVA_HOME%\bin\java %JAVA_OPTS% -classpath %TOMCAT_CLASSPATH% org.apache.catalina.startup.Bootstrap start

endlocal

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