martes, 31 de marzo de 2009

Developing and debugging web applications in eclipse and tomcat

Point tomcat to our web applications sources and debug them in eclipse


Ok this is my situation. I like to program my java web app applications inside some ide like eclipse or netbeans and test them in tomcat. The idea is that tomcat take its resources directly from the eclipse workspace so just saving a file (like a jsp, .java, etc) will instantly apply the changes. Also we will be able to debug our web application java code.

Personally, I like to have my tomcat clean and under control, so I am not a fan of "web development tools" that came with these IDEs.

So, the main idea is for one side, having one project in the IDE with the entire web application resources (java, jsp, xml) and for the other side we will work with a tomcat which have defined a web application that points directly to the eclipse project resources. When we finnish, we will be able to:
  • Have all our web application in one eclipse project.
  • Simply saving modified .java or .jsp resources will impact inmediately the changes in a running application.
  • Debug java code inside eclipse while the web application is running, both in .java and .jsp files.
I will explain how to configure the web applications project inside the eclipse ide.

1) Create an eclipse java project. I suppose you will have one or more java source folders with your application logic, persistence classes, etc.

2) Define a directory for your webapp. Inside the created eclipse project define a directory that will contain exactly the files needed by your webapp application (the files that will be stored in $TOMCAT/webapp/$YOUR_WEBAPP_NAME). This means that your .jsps, WEB-INF/web.xml, WEB-INF/classes, META-INF/ will be here. Later, we will point tomcat to this directory so it will read the resources stored in this.

In my little application I named it /html/ and need to create /html/WEB-INF/web.xml, /html/classes.

3) Output .class files to your webapp dir. We must tell eclipse to put all .class generated files to /html/WEB-INF/classes. For this you have to, in each of your source folders, right click the source folder -> Build bath -> Configure output folder, choose "Specific output folder..." and enter html/WEB-INF/classes.

4) Point tomcat to the project resources. All we need now is to create a tomcat web application that points to our $ECLIPSE_PROJECT/html directory. The best way I found in Tomcat (5.x and 6.x) for doing this is creating the file $TOMCAT/conf/$ENGINENAME/$HOSTNAME/$YOUR_WEBAPP_NAME.xml with the following content:

docBase="$ECLIPSE_PROJECT/html">

in my example I had to create the file C:\apache-tomcat-5.5.27\conf\Catalina\localhost\fileIndexer.xml
with the following content:


docBase="C:\dev\workspace\fileIndexer\html">

If your application needs it, more information can be added to this context file.

5) Libraries. You will have to put all yout java libraries (.jar) in /html/WEB-INF/lib directory for making them available to the tomcat web app.

6) java compiler. We rest only one thing to do and is to be sure that tomcat and eclipse are using the same jre, and that eclipse is generating the propert version .class files. Tomcat uses the jre defined by the environment variable JAVA_HOME and prints to the console which jre is using like:

Using JRE_HOME: C:\Java\jdk1.5.0_15

So one thing we can do is, in eclipse Window->preferences->java->Installed JREs make sure to select the same jre tomcat's using and in Window->preferences->java->Compiler choose a java compliance level supported by that jre.

7) debuggin java in eclipse. Basically we will configure eclipse so tomcat is launched as a eclipse application. In eclipse go to : Run->"Debug configurations..." (or "Open debug dialog..." in 3.3 and previous eclipse versions). There create a new java application launcher, name it appropiately, and in the Main tab select your eclipse project and specify "org.apache.catalina.startup.Bootstrap" as Main class:

Now, in "Arguments" write "start" in Program arguments and something like the following (change it according to your installation):

-Dcatalina.home="C:\apache-tomcat-5.5.27" -Djava.endorsed.dirs="C:\apache-tomcat-5.5.27\common\endorsed" -Dcatalina.base="C:\apache-tomcat-5.5.27"
-Djava.io.tmpdir="C:\apache-tomcat-5.5.27"

Now, "add the external Jar" $TOMCAT/bin/bootstrap.jar in User entries, like in:



and last, in "Source" tab add your java project. Click in "Apply" to save and click "debug" for launching tomcat in debug mode.

Breakpoints in your .java files, or even in you .jsp's java scriptlets will pause the web user requests and open the debug perspective. Try for example, adding a breakpoint in some java code of one of yours jsps, "debug as" your tomcat using the created eclipse java app launch configuration, point the browser to the later jsp and eclipse must pause the thread in debug perspective: