2. Using the Tutorial Examples 3. Getting Started with Web Applications Mapping URLs to Web Components Setting Initialization Parameters Mapping Errors to Error Screens Declaring a Reference to a Resource Declaring a Reference to a Web Service Accessing Databases from Web Applications Populating the Example Database Creating a Data Source in the Application Server Further Information about Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API 36. The Coffee Break Application | Web ModulesIn the Java EE architecture, web components and static web content files suchas images are calledweb resources. Aweb module is the smallest deployable andusable unit of web resources. A Java EE web module corresponds to aweb application as defined in the Java Servlet specification. In addition to web components and web resources, a web module cancontain other files:
A web module has a specific structure. The top-level directory of a webmodule is thedocument root of the application. The document root is where JSPpages,client-side classes and archives, and static web resources, such as images, arestored. The document root contains a subdirectory namedWEB-INF, which contains the following filesand directories:
If your web module does not contain any servlets, filter, or listener componentsthen it does not need a web application deployment descriptor. In other words,if your web module only contains JSP pages and static files then youare not required to include aweb.xml file. Thehello1 example, first discussedinPackaging Web Modules, contains only JSP pages and images and therefore does not includea deployment descriptor. You can also create application-specific subdirectories (that is, package directories) in either thedocument root or theWEB-INF/classes/ directory. A web module can be deployed as an unpacked file structure orcan be packaged in a JAR file known as a web archive (WAR)file. Because the contents and use of WAR files differ from those ofJAR files, WAR file names use a.war extension. The web modulejust described is portable; you can deploy it into any web container thatconforms to the Java Servlet Specification. To deploy a WAR on the Application Server, the file must also containa runtime deployment descriptor. The runtime deployment descriptor is an XML file thatcontains information such as the context root of the web application and themapping of the portable names of an application’s resources to the Application Server’sresources. The Application Server web application runtime DD is namedsun-web.xml and islocated in theWEB-INF directory along with the web application DD. Thestructure of a web module that can be deployed on the Application Serveris shown inFigure 3-5. Figure 3-5 Web Module Structure ![]() Packaging Web ModulesA web module must be packaged into a WAR in certain deploymentscenarios and whenever you want to distribute the web module. You package aweb module into a WAR by executing thejar command in a directory laidout in the format of a web module, by using the Antutility, or by using the IDE tool of your choice. This tutorial showsyou how to use NetBeans IDE or Ant to build, package, and deploythe sample applications. To build thehello1 application with NetBeans IDE, follow these instructions:
To build thehello1 application using the Ant utility, follow these steps:
Deploying a WAR FileYou can deploy a WAR file to the Application Server in afew ways:
All these methods are described briefly in this chapter; however, throughout the tutorial,you will useant and NetBeans IDE for packaging and deploying. Setting the Context RootAcontext root identifies a web application in a Java EE server. Youspecify the context root when you deploy a web module. A context rootmust start with a forward slash (/) and end with a string. In a packaged web module for deployment on the Application Server, thecontext root is stored insun-web.xml. To edit the context root, do the following:
Deploying a Packaged Web ModuleIf you have deployed thehello1 application, before proceeding with this section,undeploy the application by following one of the procedures described inUndeploying Web Modules. Deploying with the Admin Console
Deploying withasadminTo deploy a WAR withasadmin, open a terminal window or command promptand execute asadmin deployfull-path-to-war-file Deploying with AntTo deploy a WAR with the Ant tool, open a terminal windowor command prompt in the directory where you built and packaged the WAR,and execute ant deploy Deploying with NetBeans IDETo deploy a WAR with NetBeans IDE, do the following:
Testing Deployed Web ModulesNow that the web module is deployed, you can view it byopening the application in a web browser. By default, the application is deployedto hostlocalhost on port 8080. The context root of the webapplication ishello1. To test the application, follow these steps:
The application should display the name you submitted as shown inFigure 3-3andFigure 3-4. Listing Deployed Web ModulesThe Application Server provides two ways to view the deployed web modules: theAdmin Console and theasadmin command. To use the Admin Console:
Use theasadmin command as follows: asadmin list-components Updating Web ModulesA typical iterative development cycle involves deploying a web module and then makingchanges to the application components. To update a deployed web module, you mustdo the following:
Updating a Packaged Web ModuleThis section describes how to update thehello1 web module that you packaged. First, change the greeting in the filetut-install/javaeetutorial5/examples/web/hello1/web/index.jsp to <h2>Hi, my name is Duke. What’s yours?</h2> To update the project in NetBeans IDE:
To update the project using the Ant build tool:
To view the modified module, reload the URL in the browser. You should see the screen inFigure 3-6 in the browser. Figure 3-6 New Greeting ![]() Dynamic ReloadingIf dynamic reloading is enabled, you do not have to redeploy an applicationor module when you change its code or deployment descriptors. All you haveto do is copy the changed JSP or class files into thedeployment directory for the application or module. The deployment directory for a web modulenamedcontext-root isdomain-dir/applications/j2ee-modules/context-root. The server checks for changes periodically and redeploys theapplication, automatically and dynamically, with the changes. This capability is useful in a development environment, because it allows code changesto be tested quickly. Dynamic reloading is not recommended for a production environment,however, because it may degrade performance. In addition, whenever a reload is done,the sessions at that time become invalid and the client must restart thesession. To enable dynamic reloading, use the Admin Console:
In addition, to load new servlet files or reload deployment descriptor changes, youmust do the following:
For JSP pages, changes are reloaded automatically at a frequency set in theReload Poll Interval field. To disable dynamic reloading of JSP pages, set theReload Poll Interval field value to –1. Undeploying Web ModulesYou can undeploy web modules in four ways: you can use NetBeansIDE, the Admin Console, theasadmin command, or the Ant tool. To use NetBeans IDE:
To use the Admin Console:
Use theasadmin command as follows: asadmin undeploycontext-root To use the Ant tool, execute the following command in the directory whereyou built and packaged the WAR: ant undeploy Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |