Friday, December 31, 2010

Tomcat from within Eclipse

I just went through a very frustrating time trying to get a Tomcat instance configurable and usable from within Eclipse. As you will realize after going through this article, the Eclipse integration for Tomcat is a very confusing integration. The problem is that Eclipse has no single wizard or form to configure a Tomcat instance.

After an hour, I got my Eclipse and Tomcat integrated the way I wanted them to be. I am sharing some of the initial steps that I had to go through so that you can get a head-start with your Eclipse Tomcat integration.

Step 1. Download the binary distribution of tomcat from Tomcat 6.0.29

Step 2. Just unpack your download and you should be ready to go with Tomcat from the command-line.

Step 3. Launch Eclipse. ( I assume you are already using Eclipse. )

Step 4. Add the Tomcat server runtime environment to your Eclipse workbench.

(i) Click on Window -> Preferences


(ii) Select Server -> Runtime Environments from the preferences pane.

(iii) Click Add and select Apache Tomcat v6.0 from the list.


(iv) Click Next.

(v) Set the Tomcat installation path to the location in Step 2 above.


(vi) Click Finish.


Step 5. Create server instance.

(i) Click on Window -> Show View -> Other


(ii) Select Servers from the list of Views.


(iii) Click OK.

(iv) In the Server view, right-click and select New -> Server.


(v) Select Tomcat v6.0 Server from the list. Provide a name and select the runtime environment created in Step 4 above.


(vi) Click Finish

Step 6. Test your Tomcat instance.

(i) In the Server view, right-click the server that you just created in Step 5 above. Select Start.

(ii) Open the following URL in your browser: http://localhost:8080/.

(iii) You will get a "HTTP Status 404" error message and the title of the page will indicate the version of Tomcat. This should confirm that Tomcat has launched correctly.

Step 7. Configure the deploy path and ports for Tomcat.

(i) In the Server view, right-click the server that you just created in Step 5 above. Select Open.


(ii) In the server configuration view, you can modify the configuration as you wish.


HELP !!! 404 error when accessing http://localhost:8080/


The tomcat instance that we created in the steps above comes with a ROOT webapp that has no content. You may want to deploy a proper ROOT app with the default Tomcat welcome page that we are all familiar with. I like the welcome page because it gives me quick access to the release notes and Apache's online pages for Tomcat.

To get the welcome page, you must do the following :

(i) Click on the "Open launch configuration" link in the server configuration view (see Step 7 above). In the Edit Configuration window that appears, switch to the Arguments tab and view the location by examining the setting for catalina.base and wtp.deploy in the "VM arguments" field.


(ii) Start and stop your tomcat instance. Note: It is important that you do this step before proceeding to the next step because the wtp.deploy directory does not get created until you have started tomcat at least once.

(iii) From the command-line, you can copy the ROOT application from the tomcat installation (see Step 1 above) to the eclipse tomcat server instance (see Step 5 above).

$ cp -R ~/Apps/apache-tomcat-6.0.26/webapps/ROOT ~/workspace/personal_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/

(iv) You should now be able to access the root app from http://localhost:8080/

HELP !!! How do I configure tomcat-users.xml ?


In the Package Explorer view of Eclipse, you will find a new project called "Servers". Expand the project and you will see the tomcat-users.xml file.



I hope you have saved yourself some time with the help of the above article. Happy coding!

Ciao

No comments: