Java Tutorial - Java Script : NetBeans

Java Tutorial - Java Script :

NetBeans


Building a Hello World Servlet

Unlike Eclipse, you do not need to install any additional software to build a servlet in the NetBeans environment. To start, you need to create a new project called HelloServlet. When you create a new project, you need to mount a directory, as in the previous section. This time, create and mount the directory HelloServlet (for example, c:\mysrc\HelloServlet). After the directory is mounted, right-click on the directory in the Explorer pane and choose Tools Convert Filesystem into Web Module. This generates a message informing you of multiple ways to view the file system. Dismiss the message by clicking OK.

The Explorer pane now shows two views of the file system. Also note that expanding the mounted directory node reveals a WEB-INF subdirectory and the other associated subdirectories below WEB-INF. If the WEB-INF node is selected, the properties at the bottom of the Explorer pane reflect the web.xml file. Editing these properties changes the web.xml file that will be generated.

Choose File New to bring up the new file dialog box again. This time, scroll down if needed to find the node that is titled “JSPs and Servlets.” Open this node and select servlet. Enter helloServlet as the servlet name, and select
the classes directory as the target location. Clicking Next brings you to a deployment configuration panel. No changes are needed here, so click Finish.

The source editor pane now shows the source code for a fully functional servlet that doesn’t do anything. Modify the code in the processRequest() method by adding the following line after the line defining the PrintWriter:
out.println( “Hello World”);

The project can be built by choosing Build Build.

NetBeans comes with its own integrated version of Tomcat. To run the servlet, just choose Build Execute, and NetBeans will start the internal Tomcat and deploy the Web application to that instance. An instance of the browser
pointed to the URL where your Web application is deployed is also started for you. Messages sent to the Tomcat console are displayed in a pane below theEditor pane. When you are finished with testing, Tomcat can be stopped and started manually through the Runtime tab located below the Explorer pane. To do this, expand the Server Registry node, the Installed Servers Node, and the Tomcat node and then right-click on the Internal node. The menu provides options to start, stop, or restart the server as needed.

NetBeans can also export the .war file. To do so, right-click the project directory in the Explorer panel, and select Tools Export WAR file. NetBeans prompts for a filename and then generates the war file.