Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java...

85
Glassfish, JAVA EE, Glassfish, JAVA EE, Servlets, Servlets, JSP, EJB JSP, EJB

Transcript of Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java...

Page 1: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Glassfish, JAVA EE, Glassfish, JAVA EE, Servlets, JSP, EJB Servlets, JSP, EJB

Page 2: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Software or Resource Version RequiredNetBeans IDE, Java bundle 6.8 or 6.96.9Java Development Kit (JDK) 66MySQL database server 5.15.1MySQL Connector/J JDBC Driver version 5.x5.xGlassFish server v3 or Open Source Edition 3.0.13.0.1

necessary for communication between Java platformsJava platforms and the MySQLMySQL database protocol database protocol (already included

inside of NetBeans)

Page 3: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

The welcome page (index.jspindex.jsp) presents the user with a simple HTML form. When a browser requests index.jsp, the JSTL code JSTL code within the page initiates a query on MyNewDatabase. It retrieves data from the Subject database tableSubject database table, and inserts it into to the page before it is sent to the browser.

Table

TWO-TIER Architecture: a Java web application communicates directly with a MySQL database using the Java Database

Page 4: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

(Sample Run)(Sample Run) Final Project Final Project

Submit Submit Subject selectedSubject selected

Index.jsp

Response.jsp

Page 5: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

This example requires that the (username) root’s password is not empty.

Page 6: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

C:\Program Files (x86)\EasyPHP-5.3.3\mysql\bin>mysqld --init-filemysqld --init-file=c:\\mysql-init.txtmysql-init.txt

Resetting the Resetting the root root passwordpassword

Create a password file:

Start the MySQL server with the special --init-file --init-file option (notice that the backslash in the option value is doubled):

UPDATE mysql.user SET Password=PASSWORD(‘nbusernbuser') WHERE User='rootroot';

FLUSH PRIVILEGES;

Save the file. For this example, the file will be named C:\mysql-init.txtmysql-init.txt.

New password

Page 7: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

mysql> selectselect user, host, password FROMFROM mysql.user;

Displaying existing accountsDisplaying existing accounts

Encrypted passwordsEncrypted passwords

You can verify if you succeeded in changing the root password by issuing the Following mysql command:

Page 8: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 9: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

From the menu, select File/New Project/Java Web/Web ApplicationFile/New Project/Java Web/Web Application

Create a New ProjectCreate a New Project

International Former Professional Wrestlers’ Association for Counseling and Development

Page 10: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Create a New ProjectCreate a New Project

We want to see how to create a deployment descriptor (web.xmlweb.xml), that’s why we want to select Java EE 5Java EE 5

Page 11: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Result: Result: Welcome Page (Welcome Page (index.jspindex.jsp) )

We can easily add a table here using a Palette.

Add Table and Form here

comment

JSP Page directive

Page 12: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 13: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Invoke the Invoke the Palette WindowPalette Window

From the menu, select Window/Palette

You can also configure the display of the Palette by right-clicking the Palette title bar, and selecting Show Big Icons.

This is the result of changing the display into using Big icons

This toggles between Show Big and Small icons

Page 14: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add a Add a TableTable using the using the PalettePalette

Position the cursor at your target coordinates (after the </h1> tag</h1> tag), thenInsert a TableTable by double-clicking the icon for Table

Page 15: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add a Add a FormForm using the Palette using the Palette

Position the cursor at your target coordinates (in between the <td> and </td> tags<td> and </td> tags), then Insert a Form by double-clicking the icon for Forms

Page 16: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

<body> <h1>Welcome to IFPWAFCAD, the International Former Professional Wrestlers' Association for Counseling and Development!</h1> <table border="0"> <thead> <tr> <th>IFPWAFCAD offers expert counseling in a wide range of fields.</th> </tr> </thead> <tbody> <tr> <td>To view the contact details of an IFPWAFCAD certified former professional wrestler in your area, select a subject below:</td> </tr> <tr> <td><form action="response.jspresponse.jsp">

<strong>Select a subject:Select a subject:</strong> </form>

</td> </tr> </tbody> </table> </body>

Edit Edit Index.jspIndex.jsp

Page 17: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add a Add a Drop-Down ListDrop-Down List using the using the Auto-completion Auto-completion

support support (Ctrl+Space)(Ctrl+Space)

Press Ctrl+SpaceCtrl+Space to invoke the Auto-completion support

Page 18: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add a Add a submitsubmit button button

Add a submit button item to a point just after the drop-down list you just added.

Page 19: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

RunRun your index.jsp file your index.jsp file

• To view this page in a browser, right-click in the 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. 

At this stage the web page doesn’t look nice yet.. We can format this later using CSS formatting.CSS formatting.

Page 20: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 21: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

CreatingCreating the the responseresponse pagepage

Right-click the projectproject, then select New / JSPNew / JSP.

Page 22: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

<table border="0"> <thead> <tr> <th colspan="2">{placeholder}{placeholder}</th> </tr> </thead> <tbody> <tr> <td><strong>Description:Description: </strong></td> <td><span style="font-size:smaller; font-style:italic;">{placeholder}{placeholder}</span></td> </tr> <tr> <td><strong>Counselor: Counselor: </strong></td> <td>{placeholder}{placeholder} <br> <span style="font-size:smaller; font-style:italic;"> member since:member since: {placeholder}{placeholder}</span> </td> </tr> <tr> <td><strong>Contact Details: Contact Details: </strong></td> <td><strong>email:email: </strong> <a href="mailto:mailto:{placeholder}{placeholder}">{placeholder}{placeholder}</a> <br><strong>phone:phone: </strong>{placeholder}{placeholder} </td> </tr> </tbody></table>

Insert the following Insert the following table table into the into the response pageresponse page

Page 23: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Execute the Execute the response pageresponse page

• To view this page in a browser, right-click in the editor and choose RunRun FileFile (Shift-F6).

Page 24: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 25: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add CSSAdd CSS

Page 26: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add CSSAdd CSS

Page 27: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

body { font-family: Verdana, Arial, sans-serif; font-size: smaller; padding: 50px; color: #555;}

h1 { text-align: left; letter-spacing: 6px; font-size: 1.4em; color: #be7429; font-weight: normal; width: 450px;}

table { width: 580px; padding: 10px; background-color: #c5e7e0;}

th { text-align: left; border-bottom: 1px solid;}

td { padding: 10px;}

a:link { color: #be7429; font-weight: normal; text-decoration: none;}

a:link:hover { color: #be7429; font-weight: normal; text-decoration: underline;}

Add CSSAdd CSS

Page 28: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="style.cssstyle.css">...</head>

Add CSSAdd CSS

• Add a link to your style.css file in both index.jsp index.jsp and response.jspresponse.jsp..

• put the link in the <head> section<head> section

Page 29: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Display Result: Display Result: index.jsp index.jsp (with CSS)(with CSS)Look at the effect of applying the CSS-styleCSS-style to index.jsp

Page 30: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Display Result: Display Result: response.jsp response.jsp (with CSS)(with CSS)

Page 31: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 32: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

This tutorial uses nbusernbuser as an example password. To set your password to nbuser, navigate to your MySQL installation's bin directory in the command-line prompt and enter the following:

shell> mysql -u root

mysql> UPDATE mysql.user SET Password = PASSWORD('nbusernbuser') -> WHERE User = 'rootroot';

mysql> FLUSH PRIVILEGES;

Changing the root passwordChanging the root password

Page 33: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

ConnectingConnecting to a MySQL Database to a MySQL Database

Default port number of MySQL server

In the Services windowServices window, expand the Databases nodeDatabases node, right-click the MySQL MySQL Server node Server node and choose Properties.

Enter password: nbusernbuser

Page 34: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

ConnectingConnecting to a MySQL Database to a MySQL Database

Page 35: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Creating and Connecting to a Creating and Connecting to a MySQL Database MySQL Database

InstanceInstance

Page 36: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Creating a Table using the Database ExplorerCreating a Table using the Database Explorer

This is optional if you have an SQL file SQL file containing the entire database structure. You can simply load it later.

Page 37: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add an Existing SQL ScriptAdd an Existing SQL Script

File/Open/Load SQL file

Select the database to connect to

Page 38: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Our newly added database

Page 39: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Database StructureDatabase Structure

Page 40: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 41: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 42: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Setting up a Setting up a JDBC data source JDBC data source and and connection connection

poolpool

Page 43: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Create a new one

Java Naming and Directory Interface

Java Naming and Directory Interface (JNDI)Java Naming and Directory Interface (JNDI) – a naming system naming system is required to manage the lookup between names and resources (e.g. Database).

Page 44: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

The database that we want to connect to

Page 45: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Sun-resources.xmlSun-resources.xml will be created automatically. This will contain entries for the data source and connection pool that you have specified.

The new password should reflect here

Page 46: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Right-click the IFPWAFCADIFPWAFCAD project node, then select DEPLOYDEPLOY

Open the Services window, and expand the Servers/Glassfish/Resources/JDBC/JDBC Resources and Connection Pools nodes

Page 47: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

You need to referencereference the JDBC resource JDBC resource you just configured from the web application

Page 48: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Deployment descriptors Deployment descriptors are XML-basedXML-based text files that contain information describing how an how an application is to be deployed to a specific application is to be deployed to a specific environment.environment.

For example, they are normally used to specify:• application context parameters and behavioural patterns• security settings• mappings for servlets, filters and listeners.

Deployment Descriptor: Deployment Descriptor: Web.xmlWeb.xml

Page 49: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Projects WindowProjects Window/ Configuration Files folder/ open web.xml

Web.xmlWeb.xml

Page 50: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Select the Select the ReferencesReferences tab, click on the tab, click on the Add button, then enter detailsAdd button, then enter details

We need to referencereference the data source data source in the application’s deployment descriptor.

Web.xmlWeb.xml

Page 51: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Verifying the ResultsVerifying the Results of adding the Resource Referenceof adding the Resource Reference

Web.xmlWeb.xml

Page 52: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

We need to add the database driver’s JAR file We need to add the database driver’s JAR file to enable to enable the server to communicate with our databasethe server to communicate with our database..

Page 53: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

From the menu, select the From the menu, select the ToolsTools, Servers., Servers.

Page 54: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Select Glassfish, then enable the JDBC driver deployment optionSelect Glassfish, then enable the JDBC driver deployment option

You are actually connecting to an instanceinstance of the Glassfish application server.

Each instance runs in a unique domain, and the driver should be located within the domain (e.g. domain1)

Page 55: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

We could verify that our We could verify that our database driver database driver has been installed properly has been installed properly by inspecting the actual directory of the domain.by inspecting the actual directory of the domain.

Page 56: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

We can generate content for the We can generate content for the JSP placeholders JSP placeholders that that we’ve created by implementing JSTL codes.we’ve created by implementing JSTL codes.

JSTL technology JSTL technology performs the logic that directly queries performs the logic that directly queries the database and inserts the retrieved data into the two the database and inserts the retrieved data into the two pagespages

Page 57: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

JSTLJSTL allows us to allows us to accessaccess and and displaydisplay datadata taken from the taken from the database.database.

Glassfish serverGlassfish server includes the includes the JSTL JSTL librarylibrary by default. by default.

JavaServer Pages Standard Tag LibraryJavaServer Pages Standard Tag Library(JSTL)(JSTL)

Page 58: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

We need to populate the form inside We need to populate the form inside index.jspindex.jsp with names coming from the with names coming from the SubjectSubject database tabledatabase table..

From the From the PalettePalette, select the , select the DB Report DB Report icon.icon.

Insert here

Page 59: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Specify the data source and variable name for the Specify the data source and variable name for the DB ReportDB Report

<sql:query><sql:query> to create query to create query<c:forEach> <c:forEach> to loop through the query’s results set and output to loop through the query’s results set and output the retrieved datathe retrieved data

JSTL TagsJSTL Tags

jdbc/IFPWAFCAD

Page 60: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

AutomaticallyAutomatically Generated Codes ( Generated Codes (index.jspindex.jsp))

<%@taglib directives required for the sql and forEach tags.

Custom tags

Page 61: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%><%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%><sql:query var="subjects" dataSource="jdbc/IFPWAFCAD"> SELECT subject_id, name FROM Subject</sql:query>

<table border="1"> <!-- column headers --> <tr> <c:forEach var="columnName" items="${subjects.columnNames}"> <th><c:out value="${columnName}"/></th> </c:forEach></tr><!-- column data --><c:forEach var="row" items="${subjects.rowsByIndex}"> <tr> <c:forEach var="column" items="${row}"> <td><c:out value="${column}"/></td> </c:forEach> </tr></c:forEach></table><%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="style.css"> <title>IFPWAFCAD Homepage</title> </head> <body> <h1>Welcome to IFPWAFCAD, the International Former Professional Wrestlers' Association for Counseling and Development!</h1> <table border="0"> <thead> <tr> <th>IFPWAFCAD offers expert counseling in a wide range of fields.</th> </tr> </thead> <tbody> <tr> <td>To view the contact details of an IFPWAFCAD certified former professional wrestler in your area, select a subject below:</td> </tr> <tr> <td><form action="response.jsp"> <strong>Select a subject:</strong> <select name="subject_id"> <option></option> <option></option> <option></option> </select> <input type="submit" value="submit" name="submit" /> </form></td> </tr> </tbody> </table>

</body></html>

((index.jspindex.jsp))

Form

SQL Query

Table

Table

CSS

html

Custom tags

Page 62: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

The file is:• deployeddeployed to the GlassFish server, • compiled into a servletcompiled into a servlet, and then• rendersrenders in your default browser.

The code generated from the DB Report DB Report item creates the following table in the welcome page.

RunRun the welcome page (index.jspindex.jsp) to see how it displays in a browser.

Page 63: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

(Sample Run)(Sample Run) The Project, so far... The Project, so far...

1. Change root password to nbuser

2. Start EasyPHP to start phpMyAdmin and MySQL server.

3. Stop Apache. Remember, we are using Glassfish this time.

4. Run NetBeans, run the web application

How to run your How to run your web application?web application?

• Netbeans• Glassfish Java EE application server

• EasyPHP• MySQL server• phpMyAdmin • Apache web server

Page 64: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

(Sample Run)(Sample Run) The Project, so far... The Project, so far...

Next, we would like Next, we would like to put these to put these generated HTML generated HTML codes into the codes into the drop-down listdrop-down list

Page 65: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.
Page 66: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

<td> <form action="response.jsp"> <strong>Select a subject:</strong> <select name="subject_id"> <c:forEach var="row" items="${subjects.rows}"> <option value="${row.subject_id}">${row.name}</option> </c:forEach> </select> <input type="submit" value="submit" name="submit" /> </form></td>

the <c:forEach><c:forEach> tags loop through all subject_idsubject_id and name values from the SQL query, and insert each pair into the HTML <option> tagsHTML <option> tags. In this manner, the form's drop-down list is populated with data.

Integrate the <c:forEach> tags into the HTML form as follows.

((index.jspindex.jsp))

Page 67: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

(Sample Run)(Sample Run) The Project, so far... The Project, so far...

Subject chosenSubject chosen

Page 68: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Insert a DB Query component DB Query component on top of <%@page tag

Page 69: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Insert a DB Query componentDB Query component on top of <%@page tag

Customise the query as indicated below.

Page 70: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Automatically generated code

((response.jspresponse.jsp))

The IDE automatically added the <%@taglib> directive needed for the <sql:query> tag

Extracts the user’s choice from the drop-down list drop-down list of index.jspindex.jsp

Page 71: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Add a <c:set variable tag <c:set variable tag to store the first record returned by the query.

((response.jspresponse.jsp))

Added lines

Add a <%@taglib directive <%@taglib directive to allow us to use the <c:set tag.

Page 72: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" type="text/css" href="style.css"> <title>${counselorDetails.name} </title> </head> <body> <table border="0"> <thead> <tr> <th colspan="2">${counselorDetails.name}</th> </tr> </thead> <tbody> <tr> <td><strong>Description: </strong></td> <td><span style="font-size:smaller; font-style:italic;">${counselorDetails.description}</span></td> </tr> <tr> <td><strong>Counselor: </strong></td> <td><strong>${counselorDetails.first_name} ${counselorDetails.nick_name} ${counselorDetails.last_name}</strong> <br> <span style="font-size:smaller; font-style:italic;"> member since: ${counselorDetails.member_since}${counselorDetails.member_since}</span> </td> </tr> <tr> <td><strong>Contact Details: </strong></td> <td><strong>email: </strong> <a href="mailto:${counselorDetails.email}">${counselorDetails.email}${counselorDetails.email}</a> <br><strong>phone: </strong>${counselorDetails.telephone}${counselorDetails.telephone} </td> </tr> </tbody> </table> </body></html>

Replace the {placeholders} {placeholders} with the following Expression Expression LanguageLanguage (EL) statements (blue highlights)(blue highlights)

((response.jspresponse.jsp))

Run the application!Run the application!

Page 73: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

(Sample Run)(Sample Run) Final Project Final Project

Submit Submit Subject selectedSubject selected

Index.jsp

Response.jsp

Page 74: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

JSTL – for dynamic logicJsf-impl.jar – required library to utilise JSTL codesJdbc/IFPWAFCAD – name of data resource

JSP documentsMySQL server, database with 2 tablesGlassfish Java EE application server

Page 75: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Web browserWeb browser

------------------------------------

submit

Glassfish Glassfish Java EE application serverJava EE application server

------------------------------------

JSTL codesJSTL codes

Index.jspIndex.jsp

Jsf-impl.jarJsf-impl.jar

Server-Client InteractionServer-Client Interaction

Project NameProject Name::IFPWAFCADIFPWAFCAD

Name: subject_idsubject_id

Name: submitsubmit

0. JSP is deployed to the server and compiled into a servlet

Page 76: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Web browserWeb browser

------------------------------------

submit

Glassfish Glassfish Java EE application serverJava EE application server

------------------------------------

JSTL codesJSTL codes

Index.jspIndex.jsp

Jsf-impl.jarJsf-impl.jar

MySQL ServerMySQL Server

Subject (Table)

Counselor(Table)

MyNewDatabaseMyNewDatabase

Server-Client InteractionServer-Client Interaction

Data resource:Data resource:

Name:Name: jdbc/IFPWAFCAD jdbc/IFPWAFCAD

Project NameProject Name::IFPWAFCADIFPWAFCAD

Name: subject_idsubject_id

Name: submitsubmit

We would like to populate the drop-down list with records coming from MyNewDatabase, Subject table.

Page 77: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Web browserWeb browser

------------------------------------

submit

Glassfish Glassfish Java EE application serverJava EE application server

------------------------------------

JSTL codesJSTL codes

Index.jspIndex.jsp

Jsf-impl.jarJsf-impl.jar

MySQL ServerMySQL Server

Subject (Table)

Counselor(Table)

MyNewDatabaseMyNewDatabase

Server-Client InteractionServer-Client Interaction

Project NameProject Name::IFPWAFCADIFPWAFCAD

1. Request for index.jsp

Name: subject_idsubject_id

Name: submitsubmit

Page 78: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Web browserWeb browser

------------------------------------

submit

Glassfish Glassfish Java EE application serverJava EE application server

------------------------------------

JSTL codesJSTL codes

Index.jspIndex.jsp

Jsf-impl.jarJsf-impl.jar

MySQL ServerMySQL Server

Subject (Table)

Counselor(Table)

MyNewDatabaseMyNewDatabase

Server-Client InteractionServer-Client Interaction

1. Request for index.jsp

2. Initiate query on

subject table

Data resource:Data resource:

Name:Name: jdbc/IFPWAFCAD jdbc/IFPWAFCAD

Page 79: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Web browserWeb browser

------------------------------------

submit

Glassfish Glassfish Java EE application serverJava EE application server

------------------------------------

JSTL codesJSTL codes

Index.jspIndex.jsp

Jsf-impl.jarJsf-impl.jar

MySQL ServerMySQL Server

Subject (Table)

Counselor(Table)

MyNewDatabaseMyNewDatabase

Server-Client InteractionServer-Client Interaction

Data resource:Data resource:

Name:Name: jdbc/IFPWAFCAD jdbc/IFPWAFCAD

3. Send records based

on query result

------------------------------------4. Insert

records into page by

referring to the name of data source

2. Initiate query on

subject table

Page 80: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Web browserWeb browser

------------------------------------

submit

Glassfish Glassfish Java EE application serverJava EE application server

------------------------------------

JSTL codesJSTL codes

Index.jspIndex.jsp

Jsf-impl.jarJsf-impl.jar

MySQL ServerMySQL Server

Subject (Table)

Counselor(Table)

MyNewDatabaseMyNewDatabase

Server-Client InteractionServer-Client Interaction

Data resource:Data resource:

Name:Name: jdbc/IFPWAFCAD jdbc/IFPWAFCAD

------------------------------------4. Insert

records into page by

referring to the name of data source

5. Send generated

page back to browser via

HTTP

Page 81: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Database Connection

Page 82: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Project

JSP files: index.jsp, response.jspConfiguration files: web.xml

Services

Jdbc:mysql://localhost:3306

Databases

mynewdatabase

Server: Glassfish Server 3

Mysql-connector-java.5.1.6-bin.jar

Domain1

Enable JDBC driver deployment

DATA resource

New jdbc connection pool: IFpwafcadPool

Jdbc resource: jdbc/IFPWAFCAD

Jndi name: jdbc/IFPWAFCAD

Sun-resources.xml

<resource-ref> ------ ------</resource-ref>

Web.xml

Deployment Descriptor

Database Driver

------------------------------------

Must reside inside the server’s directory

Page 83: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Project

JSP files: index.jsp, response.jspConfiguration files: web.xml

Services

Jdbc:mysql://localhost:3306

Databases

mynewdatabase

Server: Glassfish Server 3

Mysql-connector-java.5.1.6-bin.jar

Domain1

Enable JDBC driver deployment

DATA resource

New jdbc connection pool: IFpwafcadPool

Jdbc resource: jdbc/IFPWAFCAD

Jndi name: jdbc/IFPWAFCAD

Sun-resources.xml

<resource-ref> ------ ------</resource-ref>

Web.xml

Deployment Descriptor

Database Driver

------------------------------------

Both data source and connection pool need to be registered with Glassfish server

Must reside inside the server’s directory

Page 84: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Project

JSP files: index.jsp, response.jspConfiguration files: web.xml

Services

Jdbc:mysql://localhost:3306

Databases

mynewdatabase

Server: Glassfish Server 3

Mysql-connector-java.5.1.6-bin.jar

Domain1

Enable JDBC driver deployment

DATA resource

New jdbc connection pool: IFpwafcadPool

Jdbc resource: jdbc/IFPWAFCAD

Jndi name: jdbc/IFPWAFCAD

Sun-resources.xml

<resource-ref> ------ ------</resource-ref>

Web.xml

Deployment Descriptor

Database Driver

Contains a reference to the data source

------------------------------------

Both data source and connection pool need to be registered with Glassfish server

Must reside inside the server’s directory

Page 85: Glassfish, JAVA EE, Servlets, JSP, EJB. Software or ResourceVersion Required NetBeans IDE, Java bundle 6.9 6.8 or 6.9 Java Development Kit (JDK) 6 MySQL.

Project

JSP files: index.jsp, response.jspConfiguration files: web.xml

Services

Jdbc:mysql://localhost:3306

Databases

mynewdatabase

Server: Glassfish Server 3

Mysql-connector-java.5.1.6-bin.jar

Domain1

Enable JDBC driver deployment

DATA resource

New jdbc connection pool: IFpwafcadPool

Jdbc resource: jdbc/IFPWAFCAD

Jndi name: jdbc/IFPWAFCAD

Sun-resources.xml

<resource-ref> ------ ------</resource-ref>

Web.xml

Deployment Descriptor

Database Driver

Contains a reference to the data source

------------------------------------

Both data source and connection pool need to be registered with Glassfish server

Must reside inside the server’s directory