<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN"
  "file:///bham/common/lib/docbook/docbookx.dtd">
<article>
  <articleinfo>    
    <title>Developing JSPs and Servlets with Netbeans <inlinegraphic fileref="logo.gif"/>
    </title>
    
    <author>
      <firstname>Nick</firstname><surname>Shrine</surname>
      <affiliation>
        <address><email>N.R.Shrine@cs.bham.ac.uk</email></address>
      </affiliation>
    </author>
    
    <copyright>
      <year>2004</year>
      <holder role="mailto:N.R.Shrine@cs.bham.ac.uk">Nick Shrine</holder>
    </copyright>
    
    <releaseinfo>
        <ulink url="netbeans-webapps.pdf">Printable (pdf) version</ulink>, <ulink url="netbeans-webapps.xml">XML source</ulink>
    </releaseinfo>
  
    <abstract>
      <para>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 delployment descriptor
  (<filename>web.xml</filename>) when you add new servlets. It is also possible to run web applications in the debugger.</para>
    </abstract>
    
  </articleinfo>
  
  <sect1><title>Creating a new Web Application</title>    
    <orderedlist>
        <listitem><para>Start a new project and create and mount a directory for your web application.
        <graphic fileref="Image2.jpg"/></para></listitem>
        <listitem><para>With the above directory selected, select: <filename>File &rarr; New &rarr; JSPs &amp; Servlets &rarr; Web Module.</filename>
        <graphic fileref="Image3.jpg"/></para></listitem>
        <listitem><para>Hit <command>Next</command>, the target directory in the subsequent dialog should be the one you mounted above.
	<graphic fileref="Image3a.jpg"/></para>
	<para>Now hit <command>Finish</command>.</para></listitem>
        <listitem><para>Now you should have a view in your Filesystem explorer like this:   
        <informaltable><tgroup cols="2"><tbody><row>        
        <entry><inlinegraphic fileref="Image4.jpg" align="left"/></entry>
        <entry><itemizedlist>
            <listitem><para>You put your <filename>.html</filename> and <filename>.jsp</filename> files in the top level directory, any file references in your JSP/servlets are relative to this directory.
            </para></listitem>
            <listitem><para><filename>WEB-INF/classes</filename> is where you put your servlet classes.</para></listitem>
            <listitem><para><filename>WEB-INF/lib</filename> is where you put any <filename>.jar</filename> files required by your application e.g. <filename>postgresql.jar</filename> for web applications that access a Postgresql database.</para></listitem>
            <listitem><para><filename>WEB-INF/web</filename> is your <filename>web.xml</filename> file (double-click to edit).</para></listitem>
	    <listitem><para>You can ignore the <filename>META-INF</filename> directory.</para></listitem>
	    <listitem><para>Netbeans also remounts <filename>WEB-INF/classes</filename> at the bottom to give you a shortcut to your servlet package hierarchy.</para></listitem>
            </itemizedlist></entry>        
        </row></tbody></tgroup></informaltable>
        </para></listitem>
    </orderedlist>        	    
  </sect1>

  <sect1><title>Creating a new JSP page</title>
  <orderedlist>
    <listitem><para>To create a JSP page select the top level folder in the explorer then do <filename>File &rarr; New &rarr; JSPs &amp; Servlets &rarr; JSP</filename>.
	<graphic fileref="Image9.jpg"/></para></listitem>
    <listitem><para>Hit <command>Next</command>.
	<graphic fileref="Image9a.jpg"/></para>
	<para>Enter a name for your JSP (without .jsp extension). You can select a subfolder to put it in if you want to organise your pages into subfolders. Then hit <command>Finish</command>:
    </para></listitem>
    <listitem><para>Netbeans creates a skeleton JSP page comprising of little more than the <filename>&lt;head&gt;</filename> and <filename>&lt;body&gt;</filename> tags and a couple of commented-out sample bean directives. I have added the <filename>&lt;h1&gt;</filename> and <filename>&lt;p&gt;</filename> lines in the screenshot below.
	<graphic fileref="Image9b.jpg"/></para></listitem>
    <listitem><para>To run your JSP page select it in the explorer or source editor and hit <command>F6</command> or the <inlinegraphic fileref="run.png"/> button. You will either see the page in Netbeans internal browser:
    <graphic fileref="Image10.jpg"/></para>
    <para>or you can point Mozilla at the relevent URL:
    <graphic fileref="Image10a.jpg"/></para>
    <para>Netbeans 3.6 uses port 8084 for Tomcat so the URL will be of the format <filename>http://localhost:8084/&lt;your web app&gt;/&lt;your jsp or servlet&gt;</filename></para></listitem>
  </orderedlist>
  </sect1>  
    
  <sect1><title>Creating a new Servlet</title>
  <orderedlist>
    <listitem><para>To create a servlet, select the <filename>WEB-INF/classes</filename> folder or the corresponding mount at the bottom, then do 
    <filename>File &rarr; New &rarr; JSPs &amp; Servlets &rarr; Servlet</filename>.
    <graphic fileref="Image5.jpg"/></para>
    <para>Hit <command>Next</command></para></listitem>
    <listitem><para>Enter a name for your servlet. You must specify a package for your servlet classes.
    <graphic fileref="Image5a.jpg"/></para>
    <para>Hit <command>Next</command>.</para></listitem>
    <listitem><para>Specify a URL mapping for your servlet.</para>
<para>It can be useful to prefix servlet URLs with <filename>/servlet/</filename> 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.</para>
<para>You can also specify any servlet initialisation parameters that you can then access from the servlet's <filename>init()</filename> method.
    <graphic fileref="Image5b.jpg"/></para>
    <para>Hit <command>Finish</command></para></listitem>
    <listitem><para>Netbeans creates a skeleton servlet with <filename>init(), destroy(), doGet(), doPost() and getServletInfo()</filename> methods.
    <graphic fileref="Image5c.jpg"/></para>
    <para>By default the familiar <filename>doGet() and doPost()</filename> methods are both forwarded to a single common <filename>processRequest()</filename> method as shown above. But you can delete this if you want and code the appropriate <filename>doGet()</filename> and <filename>doPost()</filename> method bodies as appropriate.</para></listitem>
    <listitem><para>To run your servlet select it in the explorer or the source
editor and hit <command>F6</command> or the <inlinegraphic fileref="run.png"/> button and Netbeans should start Tomcat. Fire up your browser and point it at the relevant URL thus:
    <graphic fileref="Image7.jpg"/></para></listitem>
<!--
    <filename>&lt;servlet&gt;</filename> and <filename>&lt;servlet-mapping&gt;</filename>
    entries for the new servlet to your <filename>web.xml</filename>.</para></listitem> -->
  </orderedlist>
  </sect1>  

  <sect1><title>The <filename>web.xml</filename> file</title>
  <para>The <filename>web.xml</filename> file is the <emphasis>Web Application Deployment Descriptor</emphasis>, which defines which servlets should be run for certain URLs and some other parameters of your web application.</para>
  <para>Netbeans creates one for you when you create a new web application and it looks something like this:
  <graphic fileref="Image8.jpg"/></para>
  <para>The format of the URL-to-servlet mappings is described 
  <ulink url="http://supportweb.cs.bham.ac.uk/documentation/java/servlets/socs-tomcat/#id213976">here</ulink>.</para>
  <para>You can see that Netbeans has automatically added <filename>&lt;servlet&gt;</filename> and <filename>&lt;servlet-mapping&gt;</filename> entries for <filename>MyServlet</filename> we just created above.</para>
  <para>The <filename>&lt;session-config&gt;</filename> contains a definition for the time in minutes before a user's session
  times out (in this case 30 minutes) and a &lt;welcome-file-list&gt; section describing which files will be loaded
  as the default home page for the web application. In this case it will try <filename>index.jsp</filename>, 
  <filename>index.html</filename> and finally if these don't exist <filename>index.htm</filename>.</para>
  <para>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 <filename>web.xml</filename>, but you are free to edit these by hand.</para>
  </sect1>
  
  <sect1><title>Adding <filename>.jar</filename> files to your application</title>
  <para>If your web applications needs additional libraries such as for database access then copy the corresponding <filename>.jar</filename> file to your project's <filename>WEB-INF/lib</filename> directory (e.g. <filename>postgresql.jar</filename>) and then right-click on <filename>WEB-INF/lib</filename> and select <command>Refresh Folder</command> and the file will be added to your web application's classpath.</para>
  <para>I have not found an easy way to copy files from within Netbeans. If you copy and paste a jar file between mounts it tends to unpack the jar file, so I usually copy them in by hand e.g.:</para>
  <screen>cp /bham/common/java/lib/postgresql.jar ~/work/mywebapp/WEB-INF/lib/</screen>
  <para>Then subsequently, right-click on <filename>WEB-INF/lib</filename> and select <command>Refresh Folder</command></para>
  </sect1>
  
  <sect1><title>Restarting Tomcat</title>
  <orderedlist>
   <listitem><para>If you edit your code, hitting <inlinegraphic fileref="run.png"/> (run) again will recompile and restart Tomcat.</para></listitem>
   <listitem><para>To restart the whole web application select <filename>WEB-INF</filename> in the explorer and hit <inlinegraphic fileref="run.png"/> (run). Any altered JSP/servlets will be recompiled and redeployed.</para></listitem>
   <listitem><para>Occasionally not all changes will be registered, such as if you manually edit <filename>web.xml</filename> or edit tag libraries. In which case I manually restart Tomcat by going to the Runtime tab and right-click on the node <filename>Server Registry &rarr; Tomcat 5 Servers &rarr; http://localhost:8084</filename> and select <command>Start / Stop Server</command>
   <graphic fileref="reload.jpg"/></para></listitem>
  </orderedlist>
  </sect1>
  
  <sect1><title>Debugging JSPs and Servlets</title>
  <orderedlist>
    <listitem><para>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 hit <command>Shift-F8</command> or 
    right-click &rarr; <filename>Toggle Breakpoint</filename>.
    <graphic fileref="breakpoint.jpg"/></para></listitem>
    <listitem><para>To debug the current JSP or Servlet that you are editing hit the <inlinegraphic fileref="debug-btn.png"/>
    button whilst the cursor is in the editor pane. To debug the whole web application hit the 
    <inlinegraphic fileref="debug-btn.png"/> button whilst <filename>WEB-INF</filename> is selected in the Explorer.</para>
    <para>The application will run until a breakpoint is encountered. 
    <graphic fileref="debug.jpg"/></para>
    <para>The green line shows the current line to be executed and the execution can be controlled by the buttons at the top:
    <graphic fileref="debug-buttonbar.jpg" align="center"/>
    The buttons left to right are <filename>Stop, Pause, Continue, Step Over, Step Into, Step Out and Run to Cursor</filename>.
    (<filename>Pause</filename> is greyed out as the program is already paused at the breakpoint).</para>
    <para>In the panes at the bottom you can see the Call Stack, the values of Local Variables 
    (you can expand objects to look at member variables) and the values of any Watches you have set. You can set a new
    watch on a variable by right-clicking on it and selecting <filename>New Watch</filename>.</para>
    <para>For debugging web applications Netbeans also provides a HTTP Monitor so that you can look at the values of
    parameters in the actual HTTP requests.</para></listitem>
    <listitem><para>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 <command>View Servlet</command>.</para></listitem>
  </orderedlist>  
  </sect1>
  
  <sect1><title>Exporting your Web Application</title>
  <para>To create a web application <filename>.war</filename> file to deploy on external
    JSP/Servlet containers, right-click on <filename>WEB-INF</filename> and select <command>Export WAR file...</command> then give it a name in the file dialog.</para>
  </sect1>

</article>
    

