Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
CS 220 Spring 2007 Lab Exercise 11 Objectives: To gain initial experience with Enterprise JavaBeans (EJBs). 1 In this exercise, you will create an Enterprise Application, and implement its two major parts: the EJB Module and the Web Module. This exercise also includes JDBC capabilities using the MySQL Database. 2. Important: When you run your application program from within NetBeans, always Run Main Project. You can do this by selecting menu Run > Run Main Project, or by clicking the Run Main Project button on the toolbar, or by hitting the F6 button, or by right-clicking on the top Project node in the Project pane and selecting Run Project from the popup list. Do not run an individual file within your project. Lab 11 Instructions: 1. Create a new project using NetBeans. a. For this exercise, create an Enterprise Application project as a container for your Web module and EJB module. The Enterprise Application template lets you create the projects for your Web module and EJB module automatically. b. Choose File > New Project (Ctrl-Shift-N) and then select Enterprise Application from the Enterprise category. Click Next. c. Enter a name for the application and specify a location for the project. Set the Java EE Version to J2EE 1.4 from Java EE5 and leave the rest of the settings at their default values and click Finish. For this document, we will assume that the application is named SimpleEntrApp. d. Your Enterprise Application should have three major nodes: SimpleEntrApp, SimpleEntrApp-ejb and SimpleEntrApp-war. They should be listed in the Project pane. Instructions are provided below for the implementation of these modules. 2. Set configuration items for MySQL Database. a. Select menu Tools > Options. In the Options dialog, click the Miscellaneous icon then expand the Ant node,click Manage Classpath.. button and click the Add JAR/ZIP… button to browse to and select the mysql j-connector jar file if it not already selected. Click OK, OK b. In the left-side Runtime pane, expand the Databases > Drivers nodes. Add the MySQL driver if this has not already been done(i.e. jdbc:mysql://localhost/cascothing[root on Default schema]. c. Add the MySQL driver to the Project Properties. In the left-side Project pane, select the SimpleEntrApp Project node, right-click and select Properties in the popup list. In the Project Properties dialog, select Libraries in the left-side Categories tree structure. Click on the Add JAR/Folder button and add the path to the MySQL connector jar file. d. In the Project pane, right-click on the SimpleEntrApp-ejb node and select Properties from the popup list. In the Properties dialog, in the left-side Categories pane, select the Libraries node. Then add the MySQL connector jar and click OK. 3. Start the MySQL Database if necessary. a. In the K236 Lab, the database should be running when the computer is started up. EJB Module. In the following steps, you will implement the EJB Module 4. Create the Catalog EJB as a Session Bean. a. In the Projects window, right-click the SimpleEntrApp-ejb node and choose New > Session Bean. Name the session bean CatalogEJB, and place it in the ejb package. Set b. c. d. e. f. g. h. the bean to be stateless and to have only local interfaces. Then click Finish. The IDE creates the bean and opens its bean class in the Source Editor. In the Projects pane: The source code files for the new EJB are in the ejb package-folder under Source Files. There are four source code files for EJB. Now we will add a couple methods to the CatalogEJB to obtain information from the database. Create method getConnection in the next steps. In the Project pane, expand the Enterprise Beans node under SimpleEntApp-ejb, rightclick on the node CatalogEJBSB class and select Add > Add Business Method from the popup lists. Enter the following in the Add Business Method dialog: i. Name: getConnection ii. Return Type: java.sql.Connection iii. Parameters: (leave blank) iv. Exceptions: Exception Then click OK. The header line for the new getConnection method will be listed in the public interface CatalogEJBLocalBusiness. The method with an empty body will have been created in the CatalogEJBBean class. Copy the body of this method from your previous CasClothes project and paste it into the new method. At the top of the file below the package statement, add these import statements: import java.util.*; import java.sql.*; import eModels.*; i. j. i. Create method getItems as you did for getConnection ii. However, for the Return Type, enter ArrayList. The header line for the new getItems method will also be listed in the public interface CatalogEJBLocalBusiness class. The method with an empty body will have been created in the CatalogEJBBean class. Copy the body of this method from your previous CasClothes project and paste it into the new method. a. Create the eModels package. Locate your ItemList java file in your eModels package. b. Comment out or delete the line import dbInterface in the ItemList java file Add the line import ejb; Change: Catalog catalog = new Catalog(); To CatalogEJBBean catalog = new CatalogEJBBean(); c. Copy the eModels package-folder from your CasClothes project. Paste the copied folder into the folder: d. ...\SimpleEntrApp\SimpleEntrApp-ejb\src\java e. Wait a moment, and the folder (with contents) should appear in the NetBeans Project pane. f. Web Module. In the following steps, you will implement the Web Module. g. Create a Custom Service Locator. k. h. We will create a custom service locator that the IDE will use when generating calls to the enterprise bean. The service locator doesn't do anything special, but you could customize the way your Enterprise application locates enterprise beans. i. In the Projects window, right-click the SimpleEntrApp-war node and choose New > File/Folder. In the wizard, in the Categories pane select Enterprise, and then select Service Locator in the right-side FileTypes pane. Click Next. j. Type ServiceLocator as the Class Name and web as the Package, and click Finish. Create eModels package. a. Copy the eModels package-folder that you modified earlier. Paste the copied folder into the folder: b. ...\SimpleEntrApp\SimpleEntrApp-war\src\java c. Wait a moment, and the folder (with contents) should appear in the NetBeans Project pane. d. Create a Servlet. e. For the final step, you will add a servlet to the Web module that displays the list of items from the Item table of our CasClothes database. f. In the Projects window, right-click the SimpleEntrApp-war node and choose New > Servlet. Name the servlet ItemsDisplay and put it in the web package. g. In the Source Editor, right-click anywhere in the servlet class's body and choose Enterprise Bean Resources > Call Enterprise Bean. Select the CatalogEJBSB session bean, and click OK. The IDE inserts the lookup method. h. At the top of the file below the package statement, add these import statements: import java.util.*; import java.sql.*; import eModels.*; i. Edit the processRequest method to look like the following: protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet ItemDetail</title>"); out.println("</head>"); out.println("<body>");out.println("<h1>Servlet ItemDetail at " + request.getContextPath() + "</h1>"); ArrayList itemList = null; ejb.CatalogEJBLocal catalogLocal = null; String name = null; try { catalogLocal = lookupCatalogEJBBean(); if (catalogLocal == null) out.println("<h3>catalogLocal is null</h3>"); else out.println("<h3>catalogLocal is not null</h3>"); }catch (Exception ex){ ex.printStackTrace(); } try {itemList = catalogLocal.getItems(); out.println("<h3>Got the catalogLocal object </h3>"); } catch (Exception ex){ if (catalogLocal == null) out.println("<h3>Could not get item list, catalogLocal is null </h3>"); ex.printStackTrace(System.out);} if (itemList == null) out.println("<h3>itemList is null</h3>"); else { out.println("<h3>"); out.println("itemList is not null</h3>"); for (int i=0; i < itemList.size(); i++) { out.println("<h3> Item #" + i + " " + ((Item)(itemList.get(i))).getName() + "<br>");}out.println("</h3>"); } out.println("</body>"); out.println("</html>"); out.close(); } l. Set the default Web Page for the Project program. a. By default, a Web application displays its index.jsp page when it is run. Since our index.jsp is blank, we want the SimpleEntrApp project to display the ItemsDisplay servlet instead. b. In the Projects window, right-click the SimpleEntrApp project node and choose Properties. Then click Run in the left panel. c. Type “/ItemsDisplay” (without the quotes) in the Relative URL field. d. Click OK m. Run the Application. a. Run the Project by using one of the methods listed earlier at the top of this Lab Exercise document (e.g., click the Run Main Project button on the toolbar). The IDE does all of the following: b. Builds the Enterprise application project and all of its subprojects (the Web module project and EJB module project). c. Starts the server if it is not already running. d. Un-deploys the Enterprise application if it is already deployed to the application server. e. Deploys the Enterprise application to the application server. f. Opens the Web module at the specified relative URL. g. See the figure below for a window capture of the page that should be presented in your external browser. n. Run, test, and debug your program. a. If the Items list, comprised of the 16 items from the database, is not displayed on your web page, the problem is most likely that the program could not find and load the MySQL driver. b. Be sure that you have added the path to the MySQL connector JAR file in all the appropriate places. c. Make a window capture of the web browser to demonstrate that your program works correctly. d. Make the modifications and enhancements to the program as described in the next steps. e. Add JSP pages from your CasClothes project to the Web Module of this program. f. Copy the first “entry” JSP page, named index.jsp, into the Web Pages folder of the Web Module. g. Copy the image required for the above page into the Web Pages folder. h. Copy the ItemsJSP page into the Web Pages folder of the Web Module. i. Modify the ItemsJSP page. Add the following to the ItemsJSP page: j. At the top of the file, right under the import statements, add the declaration of the web.ServiceLocator variable. Copy the declaration from the servlet file. k. At the very bottom of the file, paste a copy of the two methods from the servlet file, namely getServiceLocator() and lookupCatalogEJB() l. Set the default Web Page for the Project program. m. In Project Properties, set the start page to be the first CasClothes JSP page that you added above. n. Run, test, and debug your modified and enhanced program. o. The two JSP pages of this program should work just as they did in your existing CasClothes project program. p. The ItemJSP page should display a table of all the items in the database. For each item, it should display the available sizes, colors, and price, as in your existing CasClothes project program. q. Make window captures of the web browser to demonstrate that your program works correctly. TO RECEIVE CREDIT FOR YOUR ASSIGNMENT: Send in: a. Your entire NetBeans project folder with all its subfolders and files(this only includes the latest changes). b. Your Microsoft Word document file, as described above, named Lab.11.doc. The Word document file must be in the top level folder. Email your complete lab files as one zipped file to [email protected] with the subject heading: “CS220 Lab. 11” by 5/5/08 WINDOW CAPTURE OF INITIAL WORKING PROGRAM: