ECLIPSE WEB TOOLS PLATFORM developing java web applications PHẦN 2 doc

75 478 0
ECLIPSE WEB TOOLS PLATFORM developing java web applications PHẦN 2 doc

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Iteration 1: J2EE Web Applications 49 3. Click the Next button to display the Tomcat Server page. Click the Browse button to select the directory where you installed Tomcat (see the Getting Tomcat sidebar), for example, E:\jakarta-tomcat-5.0.28 The Tomcat installation directory field should now show the selected directory (see Figure 3.4). Figure 3.4 Tomcat Server 4. The JRE field is initialized to the Workbench default JRE, which is the JVM you used to launch Eclipse. JDK Required for JSP Development Part of the server runtime environment setup process involves specifying the JRE to use for launching Tomcat. Be sure to specify a full JDK instead of just a JRE because you’ll be doing JSP development. If you do not currently have a JDK defined to Eclipse, add the one you previously installed (see the Getting a JDK sidebar). JSP development requires a Java compiler, which is included in the JDK but not the JRE. JSP compilation will fail if you specify a JRE. Note that you can precompile your JSPs in order to use a JRE in your production server environment. Click the Installed JREs button to open the Installed JREs wizard. This wizard lets you define additional JREs to Eclipse. Click the Add button and select the directory where you installed the JDK (see the Getting a JDK sidebar), for example, E:\ibm-java2-142 The JDK is added to the Installed JREs page. Select its checkbox to make it the default JRE (see Figure 3.5), and click the OK button to return to the Tomcat Server page. 50 CHAPTER 3 • Quick Tour Figure 3.5 Installed JREs 5. Select the JDK from the drop-down list of the JRE field (see Figure 3.4). When WTP launches Tomcat later, it will use the specified JDK. Click the Finish button to complete the definition. Tomcat is now listed on the Installed Server Runtime Environments page (see Figure 3.6). You have now extended Eclipse with a J2EE servlet container and are ready to create your first Java Web application development project. Create a Dynamic Web Project We assume you are familiar with the Eclipse concepts of workspace, project, and builder. Recall that an Eclipse workspace contains a set of projects. You typically put projects that are related to each other in the same workspace. For example, in J2EE development, you would put related Web, EJB, and utility projects in the same workspace. Each project has a set of builders associated with it. Builders are what give the project its intelligence. Builders know how to process the artifacts in a project. The most common example of a builder is the Eclipse incremental Java compiler, which knows how to compile your Java source files into class files. WTP provides builders for Java Web applications. These builders know, for example, how to package the artifacts in J2EE modules so that they can be deployed to J2EE application servers. In general, a J2EE application will contain several modules. For example, you might want to use a Web module for the presentation logic and an EJB module for the business logic. Here you’ll just develop a Web module. Do the following to add a Web module to your workspace: Iteration 1: J2EE Web Applications 51 Figure 3.6 Installed Server Runtime Environments with Tomcat Added 1. To begin development, create a new dynamic Web project by invoking the File ᭤ New ᭤ Project menu command to open the New Project wizard. Select Web ᭤ Dynamic Web Project as the project type (see Figure 3.7). 52 CHAPTER 3 • Quick Tour Figure 3.7 New Project 2. Click the Next button. The Dynamic Web Project page is displayed (see Figure 3.8). Enter Web1 in the Project Name field. Note that Apache Tomcat is selected as the Target Runtime since it is the only server runtime environ- ment you have defined. The Configurations field lets you select a predefined configuration of project facets. Leave the setting as <custom> for now. You can also ignore the checkbox to add the Web module to an EAR project since Tomcat is just a servlet container and doesn’t support EARs. 3. Click the Next button to advance to the Project Facets page (see Figure 3.9). A facet is part of the runtime configuration of a project, such as the version of Java or J2EE. Facets will be discussed in more detail in Chapter 6. For now, simply accept the selections. 4. Click the Next button to advance to the Web Module page (see Figure 3.10). You can change some of the project settings here. For example, the Context Root of the Web application defaults to its project name, Web1. Accept the defaults for now. Iteration 1: J2EE Web Applications 53 Figure 3.8 Dynamic Web Project Figure 3.9 Project Facets 54 CHAPTER 3 • Quick Tour 5. Click the Finish button to create the Web project. Since this is the first time you have created a Web project, you are prompted to accept the J2EE license from Sun (see Figure 3.11). WTP will next attempt to down- load the J2EE schemas from the Sun Web site, so you must accept the license before WTP proceeds. Products built on top of WTP may have obtained the right to redistribute the J2EE schemas, in which case you won’t see this dialog. 6. Click the I Agree button to accept the license. Note that if you do not accept the license, then WTP will be unable to validate the J2EE XML artifacts, such as the deployment descriptors (e.g., web.xml), you create. WTP has a special J2EE perspective and will attempt to switch to it when you create any J2EE project, such as a dynamic Web project or an EJB project. One of the user interface design guidelines of Eclipse is to not switch perspectives without asking the user if they want to. You are therefore prompted to switch perspectives (see Figure 3.12). Figure 3.10 Web Module 7. Click the Ye s button to agree to the switch. The wizard opens the J2EE perspective for you so you can see the logical structure of your modules. The J2EE Project Explorer view (see Figure 3.13) now shows the project structure after Web1 has been created. Note that the wizard created some folders and files under the Web1 module. These items are defined by the J2EE specification. For example, the WebContent folder is the root of the Web application and is where the normal Web content, such as HTML Iteration 1: J2EE Web Applications 55 Figure 3.12 Open Associated Perspective Figure 3.11 License Agreement 56 CHAPTER 3 • Quick Tour Figure 3.13 Project Explorer View Create and Edit a JSP Add a JSP file to your project as follows: 1. Add a new JSP file to the WebContent folder of Web1 as follows. In the Project Explorer view, expand Web1, right click on WebContent, and invoke the New ᭤ JSP command to open the New JavaServer Page wizard. Give the new file the name hello-world.jsp. The wizard lets you pick a template for the new JSP. Select the template for JSP with HTML markup, and click the Finish button. 2. The wizard creates the JSP file with the content filled in from the template and opens it in the JSP editor. The JSP editor provides full content assist on HTML tags, JSP tags, and Java code scriptlets. Edit the JSP to say “Hello, world” using HTML tags (see Figure 3.14). The Web application is now ready to run. pages, JSPs, and images, go. The WebContent folder contains a special folder named WEB-INF, which contains items that are not accessible by a Web browser. The WEB-INF folder is where compiled Java code goes. It also contains a special file, web.xml, which is the J2EE Web deployment descriptor; more on that later (see Chapter 7). Now you are ready to start creating the content of the Web application. Run the JSP on the Server The Eclipse Java Development Tools (JDT) lets you run a Java main program (i.e., a Java class that has a standard main method) by selecting its source file and invoking the Run As ᭤ Java Application command. WTP extends the Run As com- mand to Web artifacts such as HTML and JSP files. Simply select the file and invoke the Run As ᭤ Run on Server command from the context menu. In WTP, a server runtime environment plays the role that a Java runtime environment plays in JDT. Another difference between JDT and WTP is that in JDT, the input and output of the Java application is displayed in the Console view, but in WTP, the user interface of a Web application is hosted in a Web browser. Run your JSP file as follows: 1. Select hello-world . jsp and invoke the Run As ᭤ Run on Server command from the context menu. Since this is the first time you have tried to run any artifact from the Web1 project, WTP will prompt you to define a new server (see Figure 3.15). WTP defaults the server runtime environment to Apache Tomcat, which you previously associated with the project. However, in Iteration 1: J2EE Web Applications 57 Figure 3.14 JSP Editor 58 CHAPTER 3 • Quick Tour WTP a server consists of both a server runtime environment and configura- tion information such as the port numbers to use and the set of projects to deploy or publish on it. Note that a project may be deployed on several servers, which is handy when you are testing a Web application for porta- bility to different vendors. Figure 3.15 Define a New Server 2. Click the Next button to advance to the Add and Remove Projects page (see Figure 3.16). 3. Click the Finish button to confirm that you want WTP to add the Web1 module to the server configuration. WTP then starts the server and opens a Web browser with the Uniform Resource Locator (URL) of the JSP file (see Figure 3.17). [...]... to deploy your Java class as a Web service: 1 Deploying your Java class as a Web service is simple Just select Database .java and invoke the Web Services ᭤ Create Web service command from the context menu This opens the Web Service wizard (see Figure 3. 32) The Web Service wizard is very powerful and extensible It brings together many of the tasks you normally perform in the course of Web service development... JPA, in your Web application WTP 1.5 includes the Dali JPA Tools incubator subproject, which will become a component of JST in WTP 2. 0 Iteration 4: Web Services Web services are a way to let your Web application become integrated into other applications The topic of Web services is covered in much more detail in Chapter 10 A brief discussion is given here Web services are like ordinary Web pages except... code as an Eclipse plug-in, so download that and install it in your Eclipse directory We use version 10.1 .2. 1 here You can download the zip file 72 CHAPTER 3 • Quick Tour derby_core_plugin_10.1 .2. zip from http://apache.mirror99.com/db/derby/db-derby-10.1 .2. 1/ To install Derby, exit from Eclipse, unzip the plug-in into your eclipse installation directory (see Figure 3 .25 ), and then restart Eclipse Note... the following to add Derby access to your Web application: 1 The simplest way to add Derby access to a Web application is to copy derby.jar into the WEB- INF/lib directory The WEB- INF/lib directory of a J2EE Web application is precisely intended to hold any JAR files that the application needs Copy derby.jar into WEB- INF/lib now Using Derby with Multiple Web Applications Although this technique for adding... Iteration 3: Database Access 71 common Web application programming tasks, such as parameter passing and error handling Although Struts enjoys widespread popularity among Java Web application developers, it is not part of J2EE The ideas behind Struts have been standardized in JSR 127 : Java ServerFaces, which is now part of Java EE 5 WTP 1.5 includes the JavaServer Faces Tools incubator subproject, which... instead of HTML, Web services return pure data in XML and other formats so other applications can use Iteration 4: Web Services 83 it Web services often use SOAP to format and process messages The capabilities of a Web service are described by a WSDL document that software toolkits can process, typically to generate client access code J2EE contains Web services specifications such as JSR 101: Java APIs for... HelloServlet .java class that will process the request login-user.jsp 2 Create the HelloServlet .java servlet as follows: Select the Web1 project and invoke the New ᭤ Servlet command from the context menu This opens the New Servlet wizard Create the servlet in the src directory of the Web1 project and give it the package name org.example.ch03 and the class name HelloServlet (see Figure 3 .22 ) Figure 3 .22 Create... org.example.ch03; import java. io.IOException; import java. sql.SQLException; import import import import import javax.servlet.RequestDispatcher; javax.servlet.ServletContext; javax.servlet.ServletException; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; /** * Servlet implementation class for Servlet: HelloServlet * */ public class HelloServlet extends javax.servlet.http.HttpServlet... JSR 109: Implementing Enterprise Web Services [JSR109] You’ll conclude the development of your Web application by deploying the Database class as a Web service This will let other applications programmatically get the full name of a user, given their user id WTP contains a very complete set of extensible Web service tools You’ll use the Web Service wizard to deploy the Web service, generate a client... Add a Java Scriptlet to a JSP 2 Debug a JSP 3 Create a Servlet 4 Debug a Servlet Refer to Chapter 7 for more detail Add a Java Scriptlet to a JSP The simplest way to add dynamic behavior to a JSP is to insert a Java scriptlet A Java scriptlet is a block of Java code that gets executed when the JSP is requested Java scriptlets are placed inside of delimiters Within a scriptlet, the Java code . extended Eclipse with a J2EE servlet container and are ready to create your first Java Web application development project. Create a Dynamic Web Project We assume you are familiar with the Eclipse. example of a builder is the Eclipse incremental Java compiler, which knows how to compile your Java source files into class files. WTP provides builders for Java Web applications. These builders. example, the Context Root of the Web application defaults to its project name, Web1 . Accept the defaults for now. Iteration 1: J2EE Web Applications 53 Figure 3.8 Dynamic Web Project Figure 3.9 Project

Ngày đăng: 07/08/2014, 00:22

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan