Tuesday, March 07, 2006

From zero to a running servlet in one minute

Many learners to programming servlets find it difficult to get started working with Tomcat - installing, configuring, deploying with the Tomcat manager or admin, reloading a modified servlet, etc. etc. Using Netbeans can greatly reduce the frustration in initial studying, as we can create a working servlet easily and quickly. Here are the steps:

  1. Start Netbeans
  2. Create a project by clicking the menu File -> New Project...
  3. In the New Project dialog, select Category of Web, and Projects type Web Application, then click Next >
  4. In the New Web Application dialog, enter the Project Name (e.g., WebExample1), then click Next >
  5. In the next step, we don't use any framework, so just click Finish.
  6. Netbeans is creating the project.
  7. The project is created. We can just ignore the default JSP page that is automatically generated.
  8. Now, let's create a servlet. Click the menu File -> New File...
  9. In the New File dialog, select the Category of Web, and the File Type of Servlet, then click Next >
  10. In the New Servlet dialog, enter the Class Name (e.g., ServletExample1), then click Next >
  11. In the next step, we use all default deployment values, so just click Finish.
  12. A complete servlet is generated.
  13. Edit the servlet do show something. E.g., remove the line "/* TODO output your page here." and the line "*/".
  14. After editing the servlet, save it by clicking the menu File -> Save.
  15. We can now run the servlet. Click the menu Run -> Run File -> Run "ServletExample1.java"
  16. Accept the default Execution URL by clicking OK.
  17. Netbeans will compile the servlet, start the Tomcat server, deploy the web app.
  18. And then open a browser to access the URL for your servlet. The servlet is executed.

In this way, we can create and run a servlet, without the installation and configuration of Tomcat. Tomcat 5.5.9 is bundled and pre-configured in Netbeans 5.0.