Developing JSPs and Servlets with Netbeans 4.1


Netbeans comes with a built-in Tomcat server for development of JSPs and Servlets. It also has templates for Web Applications, JSP pages and Servlet classes and automatically updates your web application deployment descriptor (web.xml) when you add new servlets. It is also possible to run web applications in the debugger.

Table of Contents
  1. Creating a new Web Application
  2. Creating a new JSP page
  3. Creating a new Servlet
  4. The web.xml file
  5. Adding .jar files to your application
  6. Restarting Tomcat
  7. Debugging JSPs and Servlets
  8. Exporting your Web Application

1.Creating a new Web Application

  1. Start a new project and create and mount a directory for your web application. WebApp 1
  2. With the above directory selected, select: File → New File→ JSP WebApp 2
  3. Complete any file location details and hit Finish.

2.Creating a new JSP page

  1. To create a JSP page select the top level folder in the explorer then do File → New File → JSP. JSP 1
  2. Hit Next. Enter a name for your JSP (without .jsp extension). Hit Finish:
  3. Netbeans creates a skeleton JSP page comprising of little more than the <head> and <body> tags and a couple of commented-out sample bean directives. I have added the <h1> and <p> lines in the screenshot below. JSP 2
  4. To run your JSP page select it in the explorer or source editor and hit F6 or the run button. You should see the output in your external web browser. Netbeans uses port 8084 for Tomcat so the URL will be of the format http://localhost:8084/<your web app>/<your jsp or servlet>

3.Creating a new Servlet

  1. To create a servlet, select File → New File→ Servlet. Servlet dialogHit Next
  2. Enter a name for your servlet. You must specify a package for your servlet classes. Servlet dialog 2Hit Next.
  3. Specify a URL mapping for your servlet.It can be useful to prefix servlet URLs with /servlet/ for later deployment on web servers such as Apache where this prefix can be used for deciding which pages to forward to web container such as Tomcat for processing.You can also specify any servlet initialisation parameters that you can then access from the servlet's init() method. Servlet dialog 3Hit Finish
  4. Netbeans creates a skeleton servlet with init(), destroy(), doGet(), doPost() and getServletInfo() methods. Servlet dialog 4By default the familiar doGet() and doPost() methods are both forwarded to a single common processRequest() method as shown above. But you can delete this if you want and code the appropriate doGet() and doPost() method bodies as appropriate.
  5. To run your servlet select it in the explorer or the source editor and hit F6 or the button and Netbeans should start Tomcat. Fire up your browser and point it at the relevant URL thus: Browser JSP

4.The web.xml file

The web.xml file is the Web Application Deployment Descriptor, which defines which servlets should be run for certain URLs and some other parameters of your web application.Netbeans creates one for you when you create a new web application and it looks something like this: web.xml

The web.xml file lives in the web/WEB-INF directory of your application and describes the mapping between URLs and servlets i.e. it tells tomcat which servlet to run when you click on a certain link. You can see that Netbeans has automatically added <servlet> and <servlet-mapping> entries for MyServlet we just created above.The <session-config> contains a definition for the time in minutes before a user's session times out (in this case 30 minutes) and a <welcome-file-list> section describing which files will be loaded as the default home page for the web application. In this case it will try index.jsp. When you create a new servlet you can set up URL mappings in the New-Servlet dialogs and Netbeans will add the appropriate entries to your web.xml, but you are free to edit these by hand.

5.Adding .jar files to your application

If your web applications needs additional libraries such as for database access, right click the project name, select properties -> sources -> libraries -> Add JAR/Folder then select the required jar file.

6.Restarting Tomcat

  1. If you edit your code, hitting run again will recompile and restart Tomcat.
  2. To restart the whole web application select WEB-INF in the explorer and hit run. Any altered JSP/servlets will be recompiled and redeployed.
  3. Occasionally not all changes will be registered, such as if you manually edit web.xml or edit tag libraries. In which case I manually restart Tomcat by going to the Runtime tab and right-click on the node Server Registry → Tomcat 5 Servers → http://localhost:8084 and select Start / Stop Server Start - stop dialog

7.Debugging JSPs and Servlets

  1. Set any breakpoints where you would like execution to halt in your JSP or Servlet code by moving the cursor to the appropriate line in the editor and either clicking in the left margin, hitting Ctrl-F8 or right-click → Toggle Breakpoint. breakpoint
  2. To debug the current JSP or Servlet that you are editing hit the debug button whilst the cursor is in the editor pane. To debug the whole web application right click the project name, and select Debug Project. The application will run until a breakpoint is encountered.
  3. To see the servlet code generated for a JSP page (remember JSPs are just templates for servlets created automatically by the container), right click anywhere in the source of the JSP and select View Servlet.

8.Exporting your Web Application

To create a web application .war file to deploy on external JSP/Servlet containers, right-click on the project name, select properties -> build ->packaging and complete the filename details.

Exporting .war file