Entries tagged [maven]
by budi kurniawan
Posted on Saturday Jan 09, 2016 at 02:52PM in Java
The most productive way of developing a servlet/JSP app is to structure your app to comply with the Servlet specification. This means creating a WEB-INF directory under your app directory, adding all JAR's to WEB-INF/lib and instructing Eclipse/NetBeans to compile Java sources to WEB-INF/classes. This way, as long as Tomcat's Context reloadable setting is set to true, your app will be reloaded automatically whenever a Java class is updated. No need to rebuild the project and restart Tomcat.
Unfortunately, you are not always free to structure your app. For instance, if your team is using Maven, chances are you will be working with a Maven directory structure. You'll probably have a structure like this:
web-app/ src/ main/ java/ resources/ webapp/ WEB-INF/ classes/ target/ classes/
Not too worry. You can still be productive if you are using Tomcat 7. Simply use its VirtualDirContext implementation of Resource in your Tomcat Context declaration.
Here is an example (you need to declare this in your server.xml file in $TOMCAT_HOME/conf or a Context file)
<Context path="/myapp" docBase="/home/temp/shopping/web" reloadable="true"> <WatchedResource>WEB-INF/web.xml</WatchedResource> <Resources className="org.apache.naming.resources.VirtualDirContext" extraResourcePaths="/WEB-INF/classes=/path1/classes,/WEBINF/classes=/path2,/css=/path3"/> <Loader className="org.apache.catalina.loader.VirtualWebappLoader" virtualClasspath="/home/temp/target/lib/homegrown.jar" /> <JarScanner scanAllDirectories="true" /> </Context>
You can find more info here.
This will not work with Tomcat 8, though, because Tomcat 8 removed VirtualDirContext. You can, however, use symlinks in Tomcat 8 to enjoy the same benefit.
More on servlet/JSP programming can be found in my book "Servlet & JSP: A Tutorial, 2nd Edition" (ISBN 9781771970273)