Chapter 12. Developing Web Applications

Why the IDE Supports Web Application Development

Most basic IDEs associated with Java programming provide developers with the tools necessary to create, execute, and debug Java programs. These features have met requirements over the years because Java was traditionally associated with .java files and applications were traditionally developed as independent entities with local scope. With the emergence of the Java 2 Enterprise Edition platform, however, the Java language has been transformed, and a myriad of new standards and methodologies have been adopted. These changes have consequently upped the ante for development environments. The plain vanilla “write a Java applet or class file” IDE simply won’t suffice in the modern Java world. Developers need an IDE that provides support for the new and emerging J2EE standards. This typically means supporting Web Application (JSPs and servlets) and Enterprise JavaBean development.

At the time of this writing, the NetBeans IDE has support for developing J2EE Web Applications with the JSP/Servlet module. EJB development is not currently supported in NetBeans. Sun ONE Studio 4, Enterprise Edition for Java (formerly Forte for Java), which is based on NetBeans, provides support for advanced EJB development.

How the IDE Provides Web Application Support

NetBeans provides support for web applications with the JSP/Servlet module. This module can be installed using the IDE’s Auto Update feature, or you can download the associated JAR and manually install it. To see if the module is already installed, you can check the list of modules by expanding the Modules node found in Tools Options. The module is active if it appears in this list and its Enabled property is set to true.

The JSP/Servlet module allows you to develop J2EE web application components using the IDE and provides the Tomcat Application Server as a container for your components to be executed and debugged in. Table 12-1 shows a listing of some of the important features, configuration options, actions, and services that are added to the IDE by the module. These features will be described in detail throughout this chapter.

Table 12-1. Features added by the JSP/Servlet module

Feature

Location

Description

Templates

IDE Menu: File New

A collection of templates that provide skeleton code for developing complete web application components such as JSPs, Servlets, HTML pages, and other web resources

JSP/Servlet Executor

IDE Menu: Tools Options, Server Execution Options node.

Executes JSPs, Servlets, and web applications in the Tomcat Application Server

WAR Packager

IDE Menu: Tools Export WAR File (while a Web Application node is selected in the project or filesystem view).

Provides a wizard for packaging a web application into a Web Archive (WAR) that can be deployed to another J2EE compliant application server.

Creating a Web Application

A web application is a collection of servlets, JSPs, HTML files, and other web-related resources (for example, image and audio files). Web applications are identified by their directory structure defined in the Java Servlet Specification version 2.2. The specification states that a valid directory structure must contain a subdirectory called WEB-INF, which may optionally contain two other directories called lib and classes. Furthermore, the WEB-INF directory must contain an XML file called web.xml. This file is known as the deployment descriptor and can be used to configure the web application.

In the IDE, a filesystem is identified as a web application if it conforms to the aforementioned directory structure. Therefore, any existing filesystem with a WEB-INF subdirectory containing a valid web.xml will be viewed as a web application in the IDE. If a filesystem does not conform to the specification, it can be easily converted to a web application and the necessary subdirectories will be created. The process of converting an existing filesystem into a web application is also known as creating a web module.

To create a web module, right-click on the root directory of a filesystem and select Tools. Select Convert Filesystem into Web Module from the submenu. You should then see a message box with the prompt Convert this Filesystem into a J2EE Web Module?. Click the OK button to continue. You will be presented with another message box with the message Alternate view on web module installed in Project. This message is informing you about the two views that the IDE maintains for web applications. More on these two views will be covered later in this section; for now click the OK button to continue.

If you expand the filesystem, you should see the WEB-INF directory that was created by the IDE. Expand the WEB-INF directory and you will see the classes and lib directories. Figure 12-1 shows this directory structure. These directories should be empty. Finally you should see the web.xml file in the WEB-INF directory. If you have the XML support module installed, you should be able to expand this file and view the elements of the deployment descriptor. These elements will be explained in more detail later in this section and used throughout this chapter.

A web application directory structure in Filesystems view

Figure 12-1. A web application directory structure in Filesystems view

The IDE maintains two views of your web application—a Filesystems view and a Web Project view.

The Filesystems View

The Filesystems view allows you to see the files in your web application as you would if you were browsing them in a file browser such as Windows Explorer or Midnight Commander. JSPs, servlets, and other web-related files can be added in the same way that they are added to other mounted filesystems. In fact, web applications have no special distinction from other mounted filesystems in the Filesystems view. For J2EE web module-related features such as configuring the deployment descriptor, deploying your web application, and executing it in the Tomcat application server, you will need to work in the Web Project view, as shown in Figure 12-2.

The Web Project view

Figure 12-2. The Web Project view

The Web Project View

In the Web Project view, a web module is identified by a special icon and the name of it’s root folder in the Filesystems view. A similar file listing is used except for the addition of a special folder called “Document Base.” This folder represents the root folder for adding your application files and is simply a pointer to the file structure you saw in the Filesystems view. When you need to add files to your application from the Web Project view, you will right-click on this folder and proceed with adding the file as if you were using the Filesystems view. The folders lib and classes are also identified in this view. They are both pointers to the same directories found in the Filesystems view and also in the Document Base directory. The Web Project view allows you to do more with your application than merely add files. It provides you with the functionality to develop, deploy, debug, and manage a J2EE web application. It is recommended that you use this view as the primary Explorer view when developing J2EE web-related applications. To access the Web Project view, select the Project tab in the Explorer.

While developing in this view, you will have access to special actions specific to web application development. These actions are located in the Tools context menu when you right-click on the web module root (identified by the special icon). Throughout this chapter we will be working in the Web Project view to develop web applications.

Working with JSP and HTML Files

You are now ready to begin adding JSP and HTML files to your web application. New files are added using templates. The IDE provides several templates for creating new web application files. Table 12-2 shows the current set of web application templates available and a description of their intended purpose.

Table 12-2. Templates for creating web application files

Template name

Description

JSP

Allows you to create JSP files with JSP and HTML syntax coloring and code completion.

Servlet

Provides a template java file for servlets.

Web Module

A template for creating new web applications. Your project can contain several web applications that can be added to a server configuration module or run independently.

To add a new JSP page to the application click on the Document Base folder in the Project view to select it; then select File New from the IDE menubar. You should see a dialog box similar to the one shown in Figure 12-3.

Adding a new JSP

Figure 12-3. Adding a new JSP

This is the template chooser dialog. The template chooser contains all the templates that are available in the IDE and allows you to create new files.

Expand the JSP & Servlet node to view the templates available for common web application files.

Select JSP from the Template Chooser and click OK. Name the file index (.jsp will be added automatically) and click Finish in the New JSP file Wizard. The Source Editor should open, showing you the source of the index.jsp file. Because the file was created from the JSP template, it will contain the minimum source code for a valid HTML file. Modify your newly created JSP to match Example 12-1.

Example 12-1. Source listing for index.jsp

      <%@page import="java.util.Date" contentType=
      "text/html"%>
      <html>
           <head>
                <title>Developed By NetBeans</title>
           </head>
           <body>
                <center><h1>A Trivial Welcome
                Page</h1></center>
                <%="Today's date is " + new Date( ) %>
           </body>
      </html>

You can preview JSP files with an external web browser configured, to be launched by the IDE. Right-click on the file in either the Project or Filesystems view and select Execute from the context menu. If you entered the source code shown in Example 12-1, you should see output similar to that shown in Figure 12-4. To add HTML files, follow the same steps for adding JSP files. HTML templates, however, are found in the Other node of the Template Chooser.

JSP page in browser

Figure 12-4. JSP page in browser

Advanced Web Applications Features

In the previous section we added a simple JSP page to a web application and previewed it with an external browser. Creating and previewing JSPs are the most common tasks for web authors, and NetBeans is well suited for them. However, we have only scratched the surface in terms of the IDE’s J2EE web development capabilities. This section includes some of the more advanced features of the IDE’s web application development support.

Executing Web Applications

Previously you saw how to execute JSP files individually and preview the HTML with a web browser in the IDE. A web application consists of one or more JSP files (or servlets) and other web resources. The JSP files can be executed directly, or the application can be executed as an entity. The application’s deployment descriptor determines what file to run when the web application is executed as an entity. For example, a JSP may be configured as a welcome file, or a servlet mapping may exist that maps the root folder / to a specific servlet that should be executed. To execute the web application go to the Project view; right-click on the web module (root folder) and select Tools Execute from the context menu. You should see output similar to that shown in your IDE’s configured web browser.

Working with Servlets

Servlets are an integral part of the J2EE web application specification. A servlet is basically a Java class that is loaded by an application server and is allowed to handle HTTP requests the same way traditional Common Gateway Interface scripts work. JSP technology is hinged on servlets because JSP files are translated to servlets at runtime. The IDE has a tight integration with the Tomcat Servlet Engine that provides support for servlets, the compilation of JSP pages to servlets, and JSP debugging.

Viewing JSPs as servlets

As previously mentioned, JSPs are compiled into servlets at runtime. You may, however, wish to see the servlet code generated for a JSP before running it. To view a JSP’s servlet code, select the JSP file in the Explorer, right-click, and select View as Servlet from the context menu. The last compiled Java code for the JSP should appear in the current editor. Note that to view the servlet code your JSP must be compiled at least once. For every update of the JSP code you will need to recompile for the changes to be reflected in the corresponding servlet code. Compile the JSP by right-clicking and selecting Compile from the context menu.

Adding servlets

Because servlets are class files, they should be added to the classes subfolder of your web application. At runtime, the Servlet Engine will find these classes as part of the classpath for the application. Servlets may also be contained in JAR files, in which case the entire JAR file should be copied to the lib subfolder of the web application.

Adding a new servlet

To add a new servlet to your web application, right-click on the classes folder in the Project view and select New Servlet & JSP Servlet from the context menu. You should be see a dialog box similar to the one shown in Figure 12-5. The source window should then open, showing you the source code for your new servlet. The IDE will create a basic servlet with the necessary inheritance and methods.

Creating a new servlet

Figure 12-5. Creating a new servlet

Adding an existing servlet

For existing servlets, the task of adding them to your web application in the IDE is simply a matter of copying the class file or JAR file to the appropriate subfolder. Class files should be copied to the classes folder, whereas JAR files should be copied to the lib folder. Keep in mind that all dependencies of the servlet class, including runtime dependencies in the form of other JAR files and classes, must also be copied to appropriate folders in your web application project.

Executing servlets

Earlier in this chapter you learned how to execute web application and JSP files. Servlets can also be executed directly. The simplest way to run a servlet is to right-click on the servlet file in the Explorer window and select Execute from the context menu. The servlet output should be displayed in a browser window.

Passing request parameters

It is common in JSP and servlet development to pass request parameters in a URL. These parameters are often used by the JSP or servlet referenced by the URL to fill in values for variables. The list of request parameters begins after the filename in a URL and is indicated by a question mark followed by a set of name-value pairs, each separated by ampersands. An example URL with request parameters looks like http://www.someserver.com/index.jsp?name=myName&age=28. To set request parameters on a servlet or JSP, right-click on the file’s node in the Explorer and select Tools Set Request Parameters. In the dialog box that opens enter the list of parameters, omitting the question mark. When you execute the servlet or JSP file, the request parameters will be shown in the browser’s address bar.

Packaging and Deploying Web Applications

After developing your web application, you’ll want to package the files and deploy them to an application server of your choice. Web applications are packaged into WAR files. The IDE can export your web application files as a WAR file. To do so, select the Web Application Module or the root filesystem from the Explorer, right-click, and select Tools Export WAR File.... Enter a new filename for the WAR file and select OK.

Deploying Web Applications

The IDE can deploy web applications to a list of applicable servers in the Server Registry. As the NetBeans community grows, more and more application server vendors will be offering modules that will incorporate their servers into the Server Registry. To deploy to a server supplied by a vendor as a module you will have to set it as the default application server for web modules. To do this click on the Runtime tab of the explorer; expand Server Registry Default Servers; right-click on the Web Module/Web Module Groups node and select Set Default Server from the context menu. From the dialog box shown in Figure 12-6, select the application server you want to use as a default for web applications.

Once you have selected a default application server, you can deploy your web applications to it by right-clicking on a web module in the project view and selecting Deploy from the context menu.

Configuring Tomcat

The IDE contains a list of application servers in the Server Registry. The registry contains an area for default servers and a listing for all the servers currently installed. At the time of this writing the IDE only contains Tomcat as an installed application server.

Tip

The ability to deploy directly to an application server is only available in NetBeans versions 3.3 and higher. If you do not see the windows and options mentioned in this section, you may need to upgrade your NetBeans distribution.

Selecting a default application server

Figure 12-6. Selecting a default application server

To configure Tomcat from the IDE, go to the runtime tab of the explorer, expand Server Registry Installed Servers, and select the Tomcat 3.2 node (or whichever version of Tomcat is displayed). You should see a Properties pane appear; if not, right-click on the node and select Properties from the context menu. The settings allow you to set the various ports for HTTP and debugging. Figure 12-7 shows the configuration properties for the Tomcat Servlet Engine.

Configuring the Tomcat server

Figure 12-7. Configuring the Tomcat server

Tomcat maintains a configuration file (server.xml) that is also accessible via the IDE. To access and modify the configuration file, right-click on the Tomcat Server node and select Open from the context menu. You can then make whatever changes are needed to configure your Tomcat installation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.106.9