Chapter 3. Web Development

Create dynamic and static Web projects

Static Web projects

If you want to create a content-based Web application that does not contain any dynamic content (such as servlets, JSP files, filters, and associated metadata) you might prefer to create a static Web project, as opposed to a dynamic Web project.

Static Web projects have the following characteristics:

  • a Web content folder (called WebContent) for all publishable resources, You can change the name of this folder from the project's pop-up menu.

  • a theme folder, the suggested directory for storing cascading style sheets and other style-related objects.

  • the ability to define folders outside of the Web content folder, for storing intermediate files, such as MIF files

  • a conversion path from a static Web project to a dynamic Web project. If you decide to convert the project, it will be a fully-valid dynamic Web project.

In addition, your project will still have the following features (which are common to both static and dynamic Web projects):

  • HTML syntax validation

  • a broken link fix-up wizard

  • a Web site navigation tool

  • a new server type, the Static Web server, which makes it easy to publish static Web projects

    NOTE: A static web project won't run on the WebSphere Application Server V6 server that ships with the product, but it will run on a static Web server that you provide; additionally, a static Web project can be previewed within Page Designer directly from the workspace files.

The folder that a static Web project is published to is modifiable, so that when you set the publishing "root" value (called a context root), such as /web1, for a static project, everything in the Web content folder will be published to the web1 folder under the Web server's doc root. This enables you to group Web resources on a Web server in folders that correspond to Web projects in the workbench. When projects defined in this way are ready for production, you can publish specific projects directly to the doc root by changing the value to / and all publishing, link fixing, and browsing will update automatically.

Aliases can also be used to specify a context root value. For example, suppose that there is an alias that is defined on the target Web server, as follows:

Alias /scripts/ "/var/www/scripts"
					

In this example, in which the current static Web project will contain common JavaScript files, you can set the context root value to be "scripts". In order for the resources in the static Web project to be published to the correct location on the Web server, you must add this Alias mapping to the server tools instance of the static Web server, as follows:

  1. From the Server view, double-click on the static Web server configuration to open the server configuration editor.

    Note: This assumes that you've already defined the static Web server.

  2. Click the Configuration editor tab.

  3. Scroll down to the Alias Path Mapping section, and add the new Alias mapping.

Now that "scripts" is defined as an Alias, the Web content in the static Web project will be published to the mapped path, /var/www/scripts.

Creating a static Web project

In the workbench, you create and maintain resources for Web applications in Web projects. If you want to create a static, content-based Web application that contains no dynamic elements, such as JSP files or servlets, use the New Static Web Project wizard. If you later decide that you want to add dynamic elements to this project, you can convert the project to a dynamic Web project.

Note that static web projects will NOT run in the provided WebSphere test environments, but these projects can be previewed in a browser directly from the workspace.

To create a new static Web project, open the Web perspective and complete the following steps:

  1. Select File > New > Static Web Project.

    The New Static Web Project wizard appears.

  2. Provide a name for the Web project.

    New Static Web Project wizard

  3. Accept the default value in the Project location field (this is where your project is stored in your file system), or click Browse to choose a location from the file system.

  4. You have two options to proceed:

    • If you want to accept the defaults associated with a static Web project, select Finish.

    • If you want to specify or customize the project options, select Next. The following step assumes you have chosen this option.

  5. Select one or more of the Web Project features.

    Web Project features

    The default selects the Default style sheet (CSS) check box, which creates a default CSS file (called Master.css) for the project.

  6. Select Next, and then select Use a default Page Template for the Web Site if you want your entire Web site to share a common page template.

    Page Template

    If you want to use one of the sample templates provided, select Sample Page Template and then choose one of the templates shown in the Thumbnail box. If you want to use a template of your own, select User-defined Page Template and then click Browse to select the template from the file system.

  7. Click Finish. The wizard creates an empty project containing the appropriate folder structure, which you can see in the Project Explorer.

    Project Explorer

You can now begin creating or importing content for your Web project using Web Site Designer, Web resource editors (such as Page Designer), the New File wizards, or the Import wizards available from the File menu.

Dynamic Web projects and applications

In Rational Developer, you create and maintain resources for Web applications in Web projects. There are two types of Web projects: dynamic and static. Dynamic web projects can contain dynamic J2EE resources such as servlets, JSP files, filters, and associated metadata, in addition to static resources such as images and HTML files. Static web projects only contains static resources. When you create Web projects, you can include cascading style sheets and JSP tag libraries (for dynamic Web projects), so that you can begin development with a richer set of project resources.

Dynamic Web projects are always imbedded in Enterprise Application projects. The wizard that you use to create a dynamic Web project will also create an Enterprise Application (EAR) project if it does not already exist. The wizard will also update the application.xml deployment descriptor of the specified Enterprise Application project to define the Web project as a module element. If you are importing a WAR file rather than creating a dynamic Web project new, the WAR Import wizard requires that you specify a Web project, which already requires an EAR project.

J2EE conventions may represent extra overhead if you only want to create a static, content-based Web application, which contains no dynamic files, such as JSP files or servlets. In this case, when you need only the most basic Web project, you might want to use the static Web project type. Note that static Web projects can be converted to dynamic Web projects by selecting Convert to a Dynamic Web Project, from the Project menu.

The J2EE model, and more specifically, the Sun Microsystems Java Servlet 2.3 Specification, defines a Web application directory structure that specifies the location of Web content files, class files, class paths, deployment descriptors, and supporting metadata. The Web project hierarchy mirrors that of the Web application created from a project. In the workbench, you can use the New Web Project wizard to create a new Web project.

The main project folder contains all development objects related to a Web application. The WebContent folder contains the elements of the project necessary to create a Web application. This folder structure maps to the Web application archive (WAR) structure defined by Sun Microsystems. The following default elements are located in the Web project folder hierarchy:

Dynamic Web project

NOTE: In the Project Explorer view, Web projects are filtered into folder nodes to customize the display of Web resources for easy management during development.

  • Web Deployment Descriptor

    The standard Web application deployment descriptor (the WEB-INF/web.xml file).

  • JavaSource folder

    Contains the project's Java source code for classes, beans, and servlets. When these resources are added to a Web project, they are automatically compiled and the generated files are added to the WEB-INF/classes directory. The contents of the source directory are not packaged in WAR files unless an option is specified when a WAR file is created.

    NOTE: Though the default name given to the folder is JavaSources, you can change the name through the Project Explorer view or through the Preferences page available by selecting Window > Preferences > Web Tools > New J2EE Project.

  • imported_classes folder

    This folder may be created during a WAR import, and contains class files that do not have accompanying source. The imported_classes folder is a Java classes folder; Java classes folders can also be created using the Web project Java Build Path properties page.

  • WebContent folder

    The mandatory location of all Web resources, including HTML, JSP, graphic files, and so on. If the files are not placed in this directory (or in a subdirectory structure under this directory), the files will NOT be available when the application is executed on a server. The Web content folder represents the contents of the WAR file that will be deployed to the server. Any files not under the Web content folder are considered development-time resources (for example, .java files, .sql files, and .mif files), and are not deployed when the project is unit tested or published.

    NOTE: Though the default name given to the folder is WebContent, you can change the name in the Project Explorer by right-clicking the folder and selecting Rename or from the Web page of the project's Properties dialog. In a dynamic Web project, changing the folder name will update the Java build output directory. You can change the preference for the default folder name to be applied when creating new Web projects by selecting Window > Preferences > Web Tools > New J2EE Project.

  • META-INF folder

    This directory contains the MANIFEST.MF file, which is used to map class paths for dependent JAR files that exist in other projects in the same Enterprise Application project. An entry in this file will update the run-time project class path and Java build settings to include the referenced JAR files.

  • theme folder

    The suggested directory for cascading style sheets and other style-related objects.

  • WEB-INF folder

    Based on the Sun Microsystems Java Servlet 2.3 Specification, this directory contains the supporting Web resources for a Web application, including the web.xml file and the classes and lib directories.

  • WEB-INF/classes folder

    This directory is for servlets, utility classes, and the Java compiler output directory. The classes in this directory are used by the application class loader to load the classes. Folders in this directory will map package and class names, as in: /WEB-INF/classes/by/boot/java/servlets/MyServlet.class.

    Do NOT place any .class files directly into this directory. The .class files are placed in this directory automatically when the Java compiler compiles Java source files that are in the Java Resources directory. Any files placed directly in this directory will be deleted by the Java compiler when it runs.

  • WEB-INF/lib folder

    The supporting JAR files that your Web application references. Any classes in .jar files placed in this directory will be available for your Web application

  • Libraries folder

    The supporting JAR files that your Web application references. This folder mirrors the content of the WEB-INF/lib folder. In addition, Web Library Projects, which are "virtual" JAR files that do not physically reside in the Web project, but are associated with Java projects elsewhere in your workspace, are included in this folder. They are packaged with your project when you export the application's WAR file.

    NOTE: A library entry on the Java build path will remain there unless the actual JAR file is deleted from the WEB-INF/lib folder. If you remove a library path entry but not the JAR file, the library entry will be re-added to the path automatically.

Creating a dynamic Web project

You create and maintain the resources for your Web applications in Web projects. Unlike with static Web projects, dynamic Web projects enable you to create resources such as JavaServer Pages and servlets, and use technologies such as Struts and JavaServer Faces.

To create a new dynamic Web project, complete the following steps:

  1. Open the Web perspective and select File > New > Dynamic Web Project. The New Dynamic Web Project wizard starts.

  2. Type a name for your Web project.

  3. Accept the default value in the Project location field (this is where your project is stored in your file system), or click Browse to choose a location from the file system. Then click the Show Advanced button if the advanced options are not already displayed.

    Project location

  4. Select the servlet version number. The servlet version determines the server level on which you can run your applications. For example, servlet version 2.4 is ONLY supported on WebSphere Application Server V6 and NOT on V5. Any new servlets and JSP files that you expect to create should adhere to the latest servlet specification level available. (Previous specification levels are offered to accommodate any legacy dynamic elements that you expect to import into the project).

    NOTES:

    • The servlet version determines the choice of target servers that appear in the drop-down list.

    • The following servlet versions correspond to the following J2EE levels:

      • version 2.2 - J2EE 1.2

      • version 2.3 - J2EE 1.3

      • version 2.4 - J2EE 1.4

    • Note that you can upgrade your servlet version using the J2EE Migration Wizard but you can't go revert to an earlier level. For example, if you choose version 2.4, you will only be able to deploy to servers that support version 2.4, such as WebSphere Application Server V6.

  5. Select a target server from the drop-down list or create a new target server by selecting the New button.

  6. A new or existing Enterprise Application project (EAR Project) must be associated with your new Web project to facilitate deployment. If you want to override the default settings for the Enterprise Application project, click New, and make your changes. When your Web project is created at the end of the wizard, the new Enterprise Application project is also created with the name specified in the EAR project field. Note that the default is the name of the web project appended with EAR (unless the ear project was selected when you opened the wizard.) If you want to use an existing Enterprise Application project, select the project from the EAR project drop down list.

    If you want to add a Web project as a module to another Enterprise Application project in the future, you can open the application deployment descriptor editor for the Enterprise Application project and select the Add option in the Module page.

  7. Specify a context root. (The context root is the Web application root, which is the top-level directory of your application when it is deployed to the Web server). You CAN change the context root after you create a project using the project Properties dialog, which you access from the project's pop-up menu. The context root can also be used by the links builder to ensure that your links remain ready to publish as you move and rename files inside your project.

  8. To add support for annotated Java classes, select the appropriate check box.

  9. To specify or customize the project options, such as adding Struts or EGL or Tag library support, select the Next button.

  10. Select one or more of the Web Project features. The box on the right displays a description of each feature as you select it on the left. The default features are, Default style sheet (CSS file) and Web Diagram. Accepting the default style sheet creates a default CSS file called Master.css, which is used for any HTML and JSP files included in the project.

    If you want to create a project that uses Struts technology, select Struts and click Next. Later in the wizard, the Struts Settings page appears in which you can specify the version of Struts technology you want to use in your project, the package prefix, and whether or not you want to bundle the resources.

    Note that although you can select WDO Relational database run-time and other options, you are not required to select these features when you create your Web project. If you add any Faces resources to your Web project, the Faces tools will automatically be enabled.

    Web Project features

  11. Click Next.

  12. Select Use a default Page Template for the Web Site if you want your entire Web site to share a common page template. If you want to use one of the sample templates provided, select Sample page template and then choose one of the templates shown in the Thumbnail box. If you want to use a template of your own, select User-defined page template and then click Browse to select the template from the file system. The selected template is copied into your project for use when creating web pages.

    Page Template

  13. Click Finish (or click Next to adjust your Struts settings). The wizard creates a new project that reflects the J2EE folder structure that specifies the location of web content files, class files, class paths, the deployment descriptor, and supporting metadata.

    New Dynamic Web Project

You can now begin creating or importing content for your Web project using Web Site Designer, Web resource editors (such as Page Designer), the New File wizards, or the Import wizards available from the File menu.

Professional hosting     Belorussian informational portal         Free SCBCD 1.3 Study Guide     Free SCDJWS 1.4 Study Guide     SCDJWS 1.4 Quiz     Free IBM Certified Associate Developer Study Guide     Free SCJP 5.0 (Tiger) Study Guide     Free Mock Exam Engine     Free SCWCD 1.4 Study Guide     IBM Test 000-287. Enterprise Application Development with IBM WebSphere Studio, V5.0 Study Guide