glassfish bootcamp netbeans lab

Upload: sirfaraz

Post on 07-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    1/15

    GlassFish Development with NetBeans 7.0Hands-on Lab

    Vince Kraemer, Oracle Corporationblogs.oracle.com/vkraemer

    1

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    2/15

    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........................................................................................................93.3 Create the RESTful web services with Jersey...............................................................................12

    4.0 Troubleshooting.................................................................................................................................15

    2

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    3/15

    1.0 Overview

    The goal of this lab is to introduce folks to some of the development optimizations that areincorporated into the NetBeans IDE plugin for GlassFish. These optimizations help usersfocus 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 databaseRESTfully 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.1Create a Java EE 6 Webproject by selecting the'New Project...' item fromthe File menu. Expandthe Samples Categoryand select the Servlet

    Stateless project from theJava Web category. ClickNext> and Finish toaccept the defaults.

    Verify that the project thatis created is the MainProject: The text for thename of the projectshould be bold.

    3

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    4/15

    2.2Use 'F6' to run the main project. You can also select the 'RunMain 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 aname tosee howtheapplicationresponds.

    2.3Take a look at the server's log in the output window titled 'GlassFish Server 3.1'

    4

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    5/15

    2.4Open the file StatelessSessionBean.java, byexpanding the project's tree and double clickingon the node.

    2.5Add 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 theserver's log window. The Deploy on save featureof NetBeans will activate and a redeploy will betriggered.

    2.6 Open the file Servlet2Stateless.java and replace

    String val = req.getParameter("name");

    if ((val != null) && (val.trim().length() > 0)) {out

    .println(" Greeting from StatelessSessionBean: "+ sless.sayHello(val) + "
    ");

    }

    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(" Greeting from StatelessSessionBean: ");if (val.equals(prevName)) {

    out.println(sless.sayWelcomeBack(val) + "
    ");} else {

    out.println(sless.sayHello(val) + "
    ");}

    }

    Save this change with Control-S/Clover-S and switch to the browser.

    2.7Enter a name in the text field. Enter the same name a second time and you willnotice that the reply changes to be something like:

    Greeting from StatelessSessionBean: Welcome back, name you entered!

    5

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    6/15

    2.8Edit the sayWelcomeBack(String) method in the file StatelessSessionBean.java, soit 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.9Go back to the browser and re-enter the name that you entered in 2.7. You will seethe following output:

    Greeting from StatelessSessionBean: Welcome back, name you entered, again!

    This means that the session data was preserved across the redeploymentoperations.

    6

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    7/15

    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 prerequisitesThe 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 apopulated 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

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    8/15

    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 theactual data rows.

    8

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    9/15

    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

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    10/15

    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

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    11/15

    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 generatedclasses.

    11

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    12/15

    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 EntityClasses' file type.

    Press 'Next>'

    B) Move all the Entity classesthat appear in the 'Available

    Entity Classes' list to the

    'Selected Entity Classes' listwith the 'Add All' button.

    Press 'Next>' to continue.

    C)

    12

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    13/15

    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

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    14/15

    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 theright-click menu of any of the service nodes, like DiscountCodeFacadeREST.

    14

  • 8/6/2019 GlassFish Bootcamp NetBeans Lab

    15/15

    4.0 Troubleshooting

    4.1 If no sample database is shown during JDBC connection pool creation, then expand theDatabases, JavaDB node in the Services pane and connect to one of the existingdatabases.

    15