1、o Adding JSP and JSTL Code Deploying and Running the Project See Also To follow this tutorial, you need the following software and resources.Software or ResourceVersion RequiredNetBeans IDEWeb and Java EE installationversion 6.0Java Developer Kit (JDK)version 6 or version 5MySQL database serverversi
2、on 5.xMySQL Connector/J JDBC DriverGlassFish application server or Tomcat servlet containerV2 version 6.xNotes: The NetBeans IDE 6.1 with Glassfish and MySQL Bundle Download provides you with an all-in-one package of software required for this tutorial. The Web and Java EE installation enables you t
3、o optionally install the GlassFish V2 application server and the Apache Tomcat servlet container 6.0.x. You must install one of these to work through this tutorial. The MySQL Connector/J JDBC Driver, necessary for communication between Java platforms and the MySQL database protocol, is included in N
4、etBeans IDE 6.x. If you need to compare your project with a working solution, you can download the sample application. Planning the StructureSimple web applications can be designed using a two-tier architecture, in which the application communicates directly with a data source using the Java Databas
5、e Connectivity API. A users requests are sent to a database, and the results are sent directly back to the user. Two-tier architectures can be easily mapped to a client-server configuration, where a users browser serves as the client, and a remote database reachable over the Internet corresponds to
6、the server.The application you build in this tutorial involves the creation of two JavaServer Pages. In each of these pages, you add basic HTML to implement a simple interface, followed by SQL tags provided by JSTL technology in order to query the database. Consider the following client-server scena
7、rio:The welcome page (index.jsp) presents the user with a simple HTML form. When a client requests the index.jsp page, the JSP code contained therein is parsed, and data from the Subject database table is gathered, added to the page, and sent to the client. The user makes a selection in the provided
8、 HTML form and submits, which causes the client to make a request for response.jsp. When response.jsp is parsed, data from both the Subject and Counselor tables is gathered and inserted into the page. Finally, the page is returned to the client and the user views data based upon his or her selection
9、.Creating a New ProjectIn order to implement the scenario described above, you develop a simple application for a fictitious organization named IFPWAFCAD, or The International Former Professional Wrestlers Association for Counseling and Development. The application enables a user to choose a counsel
10、ing subject from a drop-down list (index.jsp), then retrieves data from the MySQL database and returns the information to the user (response.jsp):index.jspresponse.jspCreate a new project in the IDE:1. Start NetBeans IDE and choose File New Project (Ctrl-Shift-N) from the main menu. Under Categories
11、 select Web; under Projects select Web Application. Click Next. 2. In Project Name, enter IFPWAFCAD. From the Server drop-down list, select the server you plan work with. Leave all other settings at their defaults and click Finish. The IDE creates a project template for the entire application, and o
12、pens an empty JSP page (index.jsp) in the Source Editor. index.jsp serves as the entry point for the application. The new project is structured according to Sun Java BluePrints guidelines. Preparing the InterfaceBegin by preparing a simple interface for the two pages. Both index.jsp and response.jsp
13、 implement an HTML table to display data in a structured fashion. index.jsp also requires an HTML form that includes a drop-down list.Make sure index.jsp is opened in the Source Editor. If it is not already open, double-click index.jsp from IFPWAFCAD Web Pages in the Projects window.1. In the Source
14、 Editor, change the text between the tags to IFPWAFCAD Homepage. Also change the text between the tags to Welcome to IFPWAFCAD, the International Former Professional Wrestlers Association for Counseling and Development!. Note: For further content, you can either add HTML elements to the page using t
15、he IDEs palette, as demonstrated below, or just copy and paste the code provided in step 8 directly into your index.jsp page. 2. Open the IDEs Palette by choosing Window Palette (Ctrl-Shift-8) from the main menu. Hover your pointer over the Table icon from the HTML category and note that the default
16、 code for the item displays:Click the icon, and drag and drop a table into the page in the Source Editor, to a point just after the tags. In the Insert Table dialog that displays, specify the following criteria, then click OK:o Rows: 2 o Columns: 1 o Border Size: 0 The HTML table code is generated a
17、nd added to your page. 3. Add the following content to the table heading and the cell of the first table row (new content in bold):4. 5. 6. 7. IFPWAFCAD offers expert counseling in a wide range of fields.8. 9. 10. 11. 12. To view the contact details of an IFPWAFCAD certified former13. professional w
18、restler in your area, select a subject below:14. 15. For the bottom row of the table, drag and drop an HTML form from the Palette into the page, directly between the second pair of tags. In the Action text box, type in response.jsp, then click OK. 16. Type in the following text between the tags of t
19、he form you just created:Select a subject:/strong17. Drag and drop a drop-down list from the Palette to a point just beneath the text you just added. In the Insert Drop-down dialog that displays, type in subject_id for the Name text field, and click OK. The number of options for the drop-down is cur
20、rently not important. Later in the tutorial you will add JSTL tags that dynamically generate options based on the data gathered from the Subject database table. Click OK, and note that the code for the drop-down list is added to the form. 18. Add a submit button to the form by dragging a button from
21、 the Palette to a point just after the drop-down list you just added. Enter submit for both the Label and Name text fields, then click OK. 19. To format your code, right-click in the Source Editor, and choose Format (Alt-Shift-F). Your code is automatically formatted, and should now look similar to
22、the following:20. 21. Welcome to IFPWAFCAD, the International Former22. Professional Wrestlers23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. professional wrestler in your area, select a subject below:35. 36. 37. 38. 39. form action=response.jsp40. 41. select name=subject_id42. /option43. 44. 45. 46
23、. 47. 48. 49. /bodyTo view this page in a browser, right-click in the Source Editor and choose Run File (Shift-F6). When you do this, the JSP page is automatically compiled and deployed to your server. The IDE opens your default browser to display the page from its deployed location:In order to prep
24、are the interface for response.jsp you must first create the file in your application. Note that most of the content that displays in this page is generated dynamically using JSP technology. Therefore, in the following steps you add placeholders which you will later substitute for the JSP code.1. Right-click the IFPWAFCAD project node in the Projects window and choose New JSP. The New JSP File dialog opens. 2. In the JSP File Name fiel
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1