GlassFish Development with NetBeans 7.0 Hands-on Lab

hundredhonkΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 4 μήνες)

353 εμφανίσεις

GlassFish Development with NetBeans 7.0

Hands-on Lab
Vince Kraemer, Oracle Corporation
blogs.oracle.com/vkraemer
1
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
2
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.0 Hands-on Lab Instructions
2.1
Create a Java EE 6 Web

project by selecting the

'New Project...' item from

the File menu. Expand

the Samples Category

and select the Servlet

Stateless project from the

Java Web category. Click

“Next>” and “Finish” to

accept the defaults.
Verify that the project that

is created is the “Main

Project”: The text for the

name of the project

should be bold.
3
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'
4
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.
6
3.0 Optional Component Instructions
In this second optional section of the lab you will use some of the code generation wizards that are

available in NetBeans to access database entities RESTfully and via a JSR 109 Web Service.
3.1Verify the prerequisites
The sample database must be registered in NetBeans and populated with data to continue.
A)
Switch to the Services explorer and verify that

you see the following
This shows us that a database connection has

been registered in NetBeans.
If that appears, we need to verify that there is a

populated database available for that registered

database connection.
B)
Right-click on the JDBC connection URL and select the 'Connect...' item from the menu that

appears.
If an authentication dialog appear, enter the password

'app'.
7
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.
8
3.2 Creating a set of entity classes
A)
Use Control-N/Clover-N to open the New File Wizard.
Select the 'Persistence' Category and the 'Entity Classes from Database' File Type and press the

'Next>' button.
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.
9
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.
E)
Switch back to the Projects explorer to see the generated

classes.
11
3.3 Create the RESTful web services with Jersey
A)
Use Control-
N/Clover-N to open

the 'New File...'

wizard again. Select

the 'Web Services'

category and

'RESTful Web

Services from Entity

Classes' file type.

Press 'Next>'
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