[RSS]

Import an Eclipse WTP project into NetBeans

(0) Preface

Tested with latest builds of NetBeans 6.0, Eclipse Europa (3.3.1), Apache Tomcat 6.0.14, using JDK 1.6.0_03 on Ubuntu GNU/Linux 7.10. Though this procedure does work for "simple" projects, your mileage might vary if your Eclipse Web projects are more complex (including external libraries / jars and the like). NetBeans is supposed to be installed using the bundled Tomcat 6.0.14 container.

(1) Creating and testing an Eclipse Web project

Once in your Eclipse workspace, use the "New" -> "Project" wizard to create a "Dynamic Web Project" like this:

In our example, this project will be named foobar as the name obviously doesn't matter:

In most cases (if you do have a running web dev workspace), you are likely to have a servlet container / application server configuration already around and working, otherwise please click the "New" button right of "Target Runtime" combo box and follow the next steps to add a new Apache Tomcat runtime configuration:

By then, browse to the folder of your local Tomcat installation (CATALINA_HOME, if you're familiar with running Tomcat from the shell) and finish creating your server runtime as well as creating your project:

By then, you should see an empty Eclipse WTP project structure looking more or less like that:

To do our demonstration, we want to add an empty JSP as well as an empty servlet and make them generate some (pointless yet working) output. First, the JSP: Right-click the "foobar" project in your workspace, choose "New" and then "JSP":

The file will be located in the WebContent folder of your project and be named "foo.jsp", for the sake of consistency. By then, you'll see that Eclipse has already opened the newly created file in an editor for you to insert something meaningful (like the "h1" statement shown in the image):

Next, the servlet. Again, right-click the project and choose "New" -> "Servlet", to create an EmptyServlet class in the foobar package:

Again, you're provided with an editor to actually put code to the servlet, please modify the doGet(...) method as outlined in the next picture:

By now, we're ready to test-drive this (rather simple) application on our local Tomcat server. For that, again right-click the project, choose "Run" -> "Run on server"...,

... keep all settings at the default and proceed clicking "Finish". By then, a browser window will open inside the Eclipse workspace showing an error (which is because we haven't set an index.jsp and/or handled the welcome page call in another meaningful way. However by now we should be capable of both running our JSP file...

... and our servlet file by using the appropriate URL mappings in the browser window:

So far, so good. Now let's stop Tomcat using the "Big Red Button" in the lower right of your workspace...

and move forth.

(2) Importing to NetBeans

Usually, there is an Eclipse Importer plugin available for NetBeans, as outlined in EclipseToNetBeans. To make use of it, you can choose "File" -> "Import" -> "Eclipse Project", choose the Eclipse workspace to contain your 'foobar' project...,

... select the right project from there and "Finish"...

... just to see that, asides your Java (servlet) classes and the package of yours, not much of your old project environment is here. Miss..

This seems to be an issue about the Eclipse Project Importer plugin, but of course, no need to give up on that, as there's another way working well for projects like this (and virtually any "self-contained" web project not including a vast set of external library dependencies). First off, use the NetBeans "New" wizard to create a new "Web Application"..

... called "foobar", being placed in an appropriate folder (while keeping the other default settings in place).

By then you should see an empty Web Application project looking like that:

Now, let's get our content here... To do so, please first of all do clean your newly created application by removing

  • the "index.jsp" file to be found in "Web Pages" and
  • the "web.xml" file to be found in the "WEB-XML" subfolder of "Web Pages".

Then, you should make use of the "Favorites" window ("Window" -> "Favorites" or CTRL+3 hotkey). Once there, navigate to the folder containing your Eclipse project in your workspace. Remembering the project structure seen in Eclipse, you can easily copy-and-paste content from there to your newly created Web Application rather quickly:

  • Anything below "WebContent" in the Eclipse project structure has to be put to (the appropriate subfolder in) "Web Pages" in your NetBeans web application.
  • Anything below "src" in the Eclipse project structure goes to "Source Packages" in your NetBeans web application view.

Take care not to miss copying the "web.xml" from "WebContent/WEB-INF/" to "Web Pages" -> "WEB-INF" to keep your application an actual web application.

By then, you're merely done. Right-clicking the web project and choosing "Run" should make your Tomcat start up and serve the application for you to play with, once again specifying the URL mappings for the two resources in there ("/foobar/foo.jsp" and "/foobar/EmptyServlet").

(3) ToDo

  • Of course, having the Eclipse Project Importer actually cleanly importing an Eclipse WTP project would be the preferrable solution.
  • This short hands-on guide doesn't deal with adding external libraries / jars to either project - if you excessively do so, you might be forced to re-add them in NetBeans (actually that's what pushed me to moving maven2 for build and dependency management).