Flex Data Services and Tomcat Authentication - Part 2: Simple webapp and Eclipse Tomcat Debugging
It’s warming up in Los Angeles. Yay! So, before I take off for a nice walk along the beach I bring to you the second part in my series of flex+tomcat posts.
In this post I’ll create an Eclipse project for a simple web app and then hookup the Eclipse debugger to a possibly remote instance of Tomcat. The files are somewhat long so I’ve created a zip file that you can download and use. You’ll have to modify the paths to match your system paths.
I’m using Eclipse 3.1.2 on Windows XP with Java JDK 1.5.0.10 and the Tomcat from the previous post. My tomcat installation resides at “c:\java\tomcat\”
Startup Eclipse and create a new Java Project and call it “flex-auth-webapp”. Accept the default options.
Eclipse projects will output .class files either to the source directories or to a “bin” directory that is created during the project creation phase. When you modify a .java file in Eclipse it will recompile the file into the destination build directory. I want the class files to be generated in my webapp directory so that Tomcat can see the class file changes and then reload the webapp. This cuts down my write-test cycle because I don’t have to perform any manual or extra steps when I make a source modification. That’s the motivation for the following step: Create a webapp directory in the tomcat/webapps directory and then have Eclipse output class files into this folder.
In Eclipse, right-click on your new project’s folder and select New > Folder.
Click on the “Advanced >>” at the bottom of the dialog.
Check the checkbox that says “Link to folder in the file system”
Click the “Browse …” button.
Navigate to the tomcat webapps directory. Should should end up in c:\java\tomcat\webapps\ or whatever your path is.
Create a new directory called “flex-auth-webapp”. Then navigate into this directory, and create another directory, call it “WEB-INF”. Navigate into this directory and call it “classes”. You should end up with the following path:
c:\java\tomcat\webapps\flex-auth-webapp\WEB-INF\classes
If you don’t like clicking all these buttons it’s easier to just use the command line window and navigate to the path and then mkdir the path.
In the “Folder Name” text field enter “tomcat-classes”.
When you’re done, click “Finish”
You’ll see the folder inside the path with a little arrow that means this is a linked folder, outside of the project’s workspace directory.
Now we have to tell Eclipse to use this new folder, tomcat-classes, as the output folder. So, right click on the project’s folder and select “Java Build Path”. Select the “Source” tab. At the bottom, in the “Default output folder:” section click on “Browse…”. Pick “tomcat-classes” from the list and click OK. Click “OK” again on the project properties window.
Copy the files in the archive into your project. You should end up with the following project structure:

Let’s test that the project can be accessed over the web. I use ant for making my build process easier. It’s a great tool and if you don’t use it then first get familiar with it: http://ant.apache.org/
Open up the ant.properties file and change the path as appropriate for your system. You’ll probably only have to change the tomcat.home property. Be careful of extra spaces at the end of these paths because ant won’t take off trailing spaces.
After you’ve properly setup your paths, right click on “build.xml”, select “Run As > Ant Build …”. From the dialog, make sure that the target “deploy” is checked. It probably is because it’s the default target. Hit “Run”. Watch the Console for any errors. If you see errors, it’s very easy to click on the error message and jump to the line in the build.xml that threw the error. If everything went well there should be a success message at the end, and if you go to the c:\java\tomcat\webapps\ directory you should see a directory called flex-auth-webapps with the following directory structure:

If you haven’t already, startup tomcat and the go to the following location:
http://localhost:8080/flex-auth-webapp/
You should see the following page:

This is just a simple jsp page that creates a new class DebutTest and calls a method in it. I did this just so we could test the debugger in the next section.
Debugging With Eclipse.
There are several plugins available for eclipse that let you star and stop an instance of Tomcat from inside eclipse. One of these lets you debug jsps. You can find them on google. It’s not that hard to hook up Eclipse, however, so, why not do it manually. So, here goes:
Shut down tomcat if it’s still running. Then open up setenv.bat (or setenv.sh if you’re on a unix machine). Add the following lines:
SET JPDA_TRANSPORT=dt_socket
SET JPDA_ADDRESS=localhost:8000
This tells the JVM that will run Tomcat to use the socket transport during debugging and to listen to connections on port 8000 at the localhost address.
Now startup tomcat with the following command line:
catalina.bat jpda run
This tells tomcat to start in debug mode and to run on the same window. The first line of the console should say something like this:
“Listening for transport dt_socket at address: 8000″
This means Tomcat is now waiting for any debugger connections on port 8000.
Go over to Eclipse and select: “Run > Debug …”
In the dialog, look for the Configuration titled “Remote Java Application. Select it and click on the ‘New’ button.
Select the Project to “flex-auth-webapp”, select the Connection Type to “Standard (Socket Attach)”. In the connection properties make sure the Host is “localhost” and the Port is “8000″.
Click Debug. It will look like nothing will happen but if you go over to the Debug perspective you should see an instance of the Java HostSpot hooked to localhost:8000.
Let’s go put a breakpoint in a file and have Tomcat stop at that breakpoint. Open up the file test/DebugTest.java. Add a breakpoint on line 8. In a browser, navigate to http://localhost:8080/flex-auth-webapp/. Go back to Eclipse and the debugger should now be stopped at line 8 of DebugTest.java. You can now step through the lines and do whatever you’re used to doing while debugging java apps, including inspecting the variables, changing their values, adding watches.
Next Steps
In the next post I’m going to create a service and a flex app that accesses this service. This will be the app we’ll use to explore authentication and security in a Tomcat+flex application.
Ok, I’m off for a walk.
Code
September 3rd, 2007 at 7:35 am
hello, the link of flex-auth-webapp.zip is wrong,
can you please fix it!
thanks!