Top Banner
ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013
42

ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Dec 23, 2015

Download

Documents

Ashlee Davidson
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

ECE356 – Database Systems

Lab 1 – Building a Web Project with NetBeansTiuley Alguindigue

Lab Instructor – University of Waterloo, E & CE Dept.Fall 2013

Page 2: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Lab 1 - Learning objectives

1. Starting NetBeans 2. Creating a Web project 3. Adding JSPs, servlets, and beans to a project 4. Model-View-Controller architecture 5. Using JSPs and beans to process HTML forms 6. Adding MySQL JDBC driver to project 7. Connecting to MySQL from a bean

Page 3: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Starting NetBeans

• Install Netbeans 7.2.1– http://netbeans.org/downloads/– Download the “Java EE option” from the available

“NetBeans IDE Download Bundles”.– Select the “Apache Tomcat” option for your web

server during the installation.

Page 4: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Starting NetBeans

• For lab computers– NetBeans is installed with the Glassfish option– Make sure the environment variable JAVA_HOME

is set to the directory where the JDK is installed (under C:\Program Files\Java\ )

Note: The procedure for setting JAVA_HOME on a Windows box is to right click on "My Computer", select Properties, Advanced (or Advanced System Settings), Environment Variables, and then either Edit or New.

Page 5: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Starting NetBeans

• Create a Web Project– File/New Project

Page 6: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Create a Web Project

Page 7: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Create a Web Project

Page 8: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Create a Web Project

Page 9: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Create a Web Project

Page 10: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Create a Web Project

• Run your first Web Project• Displays a web page with “Hello World!”

Page 11: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding JSPs, servlets, and beans to a project

• Modify this web project so that:– It will allow the user to enter some data in

another JSP.– It will execute a servlet that will connect to a

MySQL database running in the eceweb server.– It will use java classes to store data entered by the

user and also to store the status of the connection with the database as required by the Model-View-Controller architecture.

Page 12: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding JSP

• Add a JSP called user_data_form.jsp under the folder “Web Pages”

• Add html commands to create a form such as the one shown in the next slide.

• Use the following command when specifying your form html tag:<form method="post" action="UserDataServlet">Note: UserDataServlet is a servlet that you will build in the next step of this lab.

Page 13: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding JSP

Page 14: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding JSP

• Modify index.jsp to allow the user to access the page that you have just created. After your changes index.jsp must contain code to display the page shown in the next slide.

Page 15: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Your first JSPindex.jsp

Page 16: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding a Servlet

• Test your web project• You will now add the servlet

UserDataServlet.java which defines the action performed when the user clicks on the “submit” button of your form.

Page 17: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Add a new package

Page 18: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Add servlet under this package

Page 19: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Add servlet under this package

Page 20: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Add servlet under this package

Page 21: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

UserDataServlet.javaUses a class called UserData ( called a JavaBean) to store the values entered in the form. We have provided this class here.

The java code in this servlet will:

1. Get the parameters entered in the form and store them in the corresponding fields in object of type UserData.Hint: see documentation for class HttpServletRequest, method getParameter()

2. Save the object UserData to the session requestHint: see documentation for class HttpSession , method setAttribute()

3. Display the values entered in the form using a jsp called user_data_thanks.jsp (provided here)Hint: see documentation for getServletContext().getRequestDispatcher(..).forward(..);

Page 22: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Testing your jsps, forms and servlet

• Add the user_data_thanks.jsp (provided here) to the folder ‘Web Pages”

• Add the user_data_thanks2.jsp (provided here) to the folder ‘Web Pages”

• Add the UserData.java class (provided here) to the source package “ece356”

• Test your web project

Page 23: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Testing your jsps, forms and servletuser_data_thanks.jsp

Page 24: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Testing your jsps, forms and servletuser_data_thanks2.jsp

Page 25: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Model-View-Controller architecture

• Your web project uses the MVC software architecture model:– JSP to receive and present data to the user (View)– Servlet to implement navigation and business logic

(Controller)– JavaBean – use to encapsulate related properties

in a single java object (Model)– Data Access Object (DAO) - object that provides

an abstract interface to a database(Model)

Page 26: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding MySQL JDBC driver to project

• The next part of this lab is to add an option to connect to the MySQL database in the server

• MySQL provides connectivity for client applications developed in the Java programming language through a JDBC driver, which is called MySQL Connector/J.

• You will need to add to your web project the library for the MySQL Connector/J

Page 27: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding MySQL JDBC driver to project

Add a new Library, select the “MySQL JDBC Driver” from the list of available libraries

Page 28: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

MySQL JDBC driver added to project

Your “Projects” tab should now show the MySQL JDBC Driver jar file.

Page 29: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Add option to connect to the MySQL database in the server

• Modify index.jsp:– Add a link that allows the user to connect to the

database– When this link is clicked, your web application will

execute the servlet DBTestServlet.java (you will write this servlet in the next part of this lab)

– Next slide shows index.jsp after these modifications

Page 30: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Your first pageindex.jsp

Page 31: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Add a new Servlet DBTestServlet.java

• Add a new Servlet to package ece356 named DBTestServlet.java. Make sure to choose the option “Add information to deployment descriptor (web.xml)” on the second panel.

• You will later edit this servlet and add the java code needed to connect to the DB as part of the method processRequest()

Page 32: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

UserDBAO.java

• Servlet DBTestServlet uses the services of a data access object(DAO) called to UserDBAO.java to interact with the database. The code for UserDBAO.java is provided here.

• Modify the method TestConnection() in the UserDBAO.java so that the database, user id, and password corresponds to your course database in the eceweb server. Ie. If your uw userid is pthomas, your parameters should look as follows:

public static final String url = "jdbc:mysql://eceweb.uwaterloo.ca:3306/"; public static final String user = “user_pthomas"; public static final String pwd = “your_password"; // specify your password here, initially same as userid

Page 33: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

DBTestServlet processRequest()

• Modify method processRequest() inDBTestServlet.java to connect to the database.

– Add call to TestConnection method in UserDBAO.java:

UserDBAO.testConnection();

– The statement above may throw an exception when the attempt to connect is not successful. Your code should handle this exception.

Page 34: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

DBTestServlet processRequest()

• Handling exceptions – setting the value of variable with name of jsp to be displayed:

String url;

try { // Call static method using class name UserDBAO.testConnection(); // Set the name of jsp to be displayed if // connection succeds url = "/dbtest.jsp"; } catch (Exception e) { // Save the exception info in th e request object so it can be displayed. request.setAttribute("exception", e);

// Set the name of jsp to be displayed if // connection fails

url = "/error.jsp"; }

Page 35: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

DBTestServlet processRequest()

Displaying the suitable jsp from the servlet (name of jsp is now stored in variable “url”):

getServletContext().getRequestDispatcher(url).forward(request, response);

Page 36: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding jsps displayed after connectionwhen connection is successful

• Add jsp name dbtest.jsp

Page 37: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding jsp displayed after connectionwhen connection is not successful

• Add jsp name error.jsp

Page 38: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Adding jsps displayed after connection

• Add jsp name error.jsp (provided here)– Use request.getAttribute() to get the value of

attribute “exception” (this attribute was saved to the response object in DBTestServlet)

– Prints error message and stack trace in this attribute

Page 39: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Summary

What we have learned:• Created a Web project with NetBeans • The project, using Model-View-Controller

architecture, demonstrates how to:– Use JSPs and a HTML form to collect data from the user.– Use a JavaBean class to represent user data.– Store and Access data from JavaBean in JSPs and servlets.– Connect to a database from a servlet, using a DAO object

to interface with the database.

Page 40: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

Further self study

• Java Programming• HTML • JSP Programming• Model View Controller(MVC) architectural

pattern as it applies to JSP programming

Page 41: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

References

Sample Code for this lab was written by Prof. Wojciech Golab

Java, JSP and JDBC The Java TutorialsThe Java Tutorial - Trail: JDBC(TM) Database AccessIntroduction to Java Server Pages(JSP)

Web Application Development

The Java EE 6 TutorialTutorial - Creating a Simple Web Application Using a MySQL Database (using NetBeans)

Page 42: ECE356 – Database Systems Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab Instructor – University of Waterloo, E & CE Dept. Fall 2013.

References

• Reference for htmlwww.w3schools.com

• Reference for MVC and JSPs:http://netbeans.org/kb/docs/javaee/ecommerce/design.html?print=yes

• Ref for DAO : http://en.wikipedia.org/wiki/Data_access_object