GlassFish Bootcamp NetBeans Lab
GlassFish Bootcamp NetBeans Lab
0 Hands-on Lab
Table of Contents
1.0 Overview..............................................................................................................................................3 2.0 Hands-on Lab Instructions...................................................................................................................3 3.0 Optional Component Instructions........................................................................................................7 3.1Verify the prerequisites.....................................................................................................................7 3.2 Creating a set of entity classes........................................................................................................9 3.3 Create the RESTful web services with Jersey...............................................................................12 4.0 Troubleshooting.................................................................................................................................15
1.0 Overview
The goal of this lab is to introduce folks to some of the development optimizations that are incorporated into the NetBeans IDE plugin for GlassFish. These optimizations help users focus on coding. This hands-on lab will: Demonstrate Java EE 6 simplified packaging Demonstrate deployment integration Demonstrate 'Deploy on Save' functionality Demonstrate cross deployment session preservation The optional component of the lab demonstrates how to expose the content of a database RESTfully and as a JSR-109 web service with no coding: Demonstrate code generation wizard for JPA Demonstrate code generation wizard for JAX-RS (Jersey)
2.2 Use 'F6' to run the main project. You can also select the 'Run Main Project' item from the Run menu. This will generate a lot of activity. Three output tabs will open on the lower right of the NetBeans window.
The browser opens and the initial page of the web application is displayed. You can enter a name to see how the application responds.
2.3 Take a look at the server's log in the output window titled 'GlassFish Server 3.1'
2.4 Open the file StatelessSessionBean.java, by expanding the project's tree and double clicking on the node. 2.5 Add a new Business method to this EJB. public String sayWelcomeBack(String name) { return "Welcome back, " + name + "!\n"; } When you save the change, pay attention to the server's log window. The Deploy on save feature of NetBeans will activate and a redeploy will be triggered. 2.6 Open the file Servlet2Stateless.java and replace
String val = req.getParameter("name"); if ((val != null) && (val.trim().length() > 0)) { out .println("<FONT size=+1 color=red> Greeting from StatelessSessionBean: </FONT> " + sless.sayHello(val) + "<br>"); }
With
String val = req.getParameter("name"); if ((val != null) && (val.trim().length() > 0)) { String prevName = (String) req.getSession().getAttribute("prevName"); req.getSession().setAttribute("prevName", val); out.println("<FONT size=+1 color=red> Greeting from StatelessSessionBean: </FONT> "); if (val.equals(prevName)) { out.println(sless.sayWelcomeBack(val) + "<br>"); } else { out.println(sless.sayHello(val) + "<br>"); } }
Save this change with Control-S/Clover-S and switch to the browser. 2.7 Enter a name in the text field. Enter the same name a second time and you will notice that the reply changes to be something like: Greeting from StatelessSessionBean: Welcome back, name you entered! 5
2.8 Edit the sayWelcomeBack(String) method in the file StatelessSessionBean.java, so it reads: public String sayWelcomeBack(String name) { return "Welcome back, " + name + ", again!\n"; } and save this change. You will see that the project gets redeployed. 2.9 Go back to the browser and re-enter the name that you entered in 2.7. You will see the following output: Greeting from StatelessSessionBean: Welcome back, name you entered, again! This means that the session data was preserved across the redeployment operations.
C) Expand the connection to verify that you see the following tree:
D) Right-click on one of the DISCOUNT_CODE table and select 'View Data...' from the menu. An 'SQL Command' window will appear in the Editor area.
You may need to resize the lower pane (titled 'select * from APP.DISCOUN...') to see the actual data rows.
B) Select 'New Data Source' as the value of the 'Data Source' field in the dialog that appears. This will make a secondary dialog appear. Enter a name of your choosing in the 'JNDI Name' field. Select the connection that we used to 'View Data...'. Press the OK button to dismiss the secondary dialog.
C) The primary dialog will update to include some 'Available Tables'. Press the 'Add All' button to transfer them to the 'Selected Tables' list and press 'Next>' to proceed.
10
D) Press the 'Finish' button to accept the defaults on this page and the next page. Update the default value for the 'Package' field when using this wizard for 'real' code.
11
B) Move all the Entity classes that appear in the 'Available Entity Classes' list to the 'Selected Entity Classes' list with the 'Add All' button. Press 'Next>' to continue. C)
12
D) You can accept the defaults on the page that appear by pressing the 'Finish' button. You would probably want to provide a different value in the 'Resource Package' field when this wizard is used for 'real' code.
E) Uncheck the item labelled 'Add Jersey library' and use the OK button to dismiss the secondary dialog that appears.
13
F) NetBeans generates a number of classes and updates the Projects explorer view of the project with an additional node: RESTful Web Services.
You can see how these services respond to an HTTP GET using the 'Test Resorce Uri' item from the right-click menu of any of the service nodes, like DiscountCodeFacadeREST.
14
4.0 Troubleshooting
4.1 If no sample database is shown during JDBC connection pool creation, then expand the Databases, JavaDB node in the Services pane and connect to one of the existing databases.
15