ece356 database systems
Skip this Video
Download Presentation
ECE356 – Database Systems

Loading in 2 Seconds...

play fullscreen
1 / 42

ECE356 – Database Systems - PowerPoint PPT Presentation

  • Uploaded on

ECE356 – Database Systems. Lab 1 – Building a Web Project with NetBeans Tiuley Alguindigue Lab I nstructor – University of Waterloo, E & CE Dept. Fall 2013. Lab 1 - Learning objectives. S tarting NetBeans C reating a Web project A dding JSPs, servlets, and beans to a project

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about ' ECE356 – Database Systems' - brand

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
ece356 database systems

ECE356 – Database Systems

Lab 1 – Building a Web Project with NetBeans


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

Fall 2013

lab 1 learning objectives
Lab 1 - Learning objectives
  • Starting NetBeans
  • Creating a Web project
  • Adding JSPs, servlets, and beans to a project
  • Model-View-Controller architecture
  • Using JSPs and beans to process HTML forms
  • Adding MySQL JDBC driver to project
  • Connecting to MySQL from a bean
starting netbeans
Starting NetBeans
  • Install Netbeans 7.2.1
    • Download the “Java EE option” from the available “NetBeans IDE Download Bundles”.
    • Select the “Apache Tomcat” option for your web server during the installation.
starting netbeans1
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.

starting netbeans2
Starting NetBeans
  • Create a Web Project
    • File/New Project
create a web project4
Create a Web Project
  • Run your first Web Project
  • Displays a web page with “Hello World!”
adding jsps servlets and beans to a project
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.
adding jsp
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.

adding jsp2
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.
adding a servlet
Adding a Servlet
  • Test your web project
  • You will now add the servlet which defines the action performed when the user clicks on the “submit” button of your form.
userdataservlet java

Uses 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 request

Hint: 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(..);

testing your jsps forms and servlet
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 class (provided here) to the source package “ece356”
  • Test your web project
model view controller architecture
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)
adding mysql jdbc driver to project
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
adding mysql jdbc driver to project1
Adding MySQL JDBC driver to project

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

mysql jdbc driver added to project
MySQL JDBC driver added to project

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

add option to connect to the mysql database in the server
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 (you will write this servlet in the next part of this lab)
    • Next slide shows index.jsp after these modifications
add a new servlet dbtestservlet java
Add a new Servlet
  • Add a new Servlet to package ece356 named 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()
userdbao java
  • Servlet DBTestServlet uses the services of a data access object(DAO) called to to interact with the database. The code for is provided here.
  • Modify the method TestConnection() in the so that the database, user id, and password corresponds to your course database in the eceweb server. Ie. If your uwuserid is pthomas, your parameters should look as follows:

public static final String url = "jdbc:mysql://";

public static final String user = “user_pthomas";

public static final String pwd = “your_password"; // specify your password here, initially same as userid

dbtestservlet processrequest
  • Modify method processRequest() to connect to the database.
    • Add call to TestConnection method in


    • The statement above may throw an exception when the attempt to connect is not successful. Your code should handle this exception.
dbtestservlet processrequest1
  • Handling exceptions – setting the value of variable with name of jsp to be displayed:

String url;

try {

// Call static method using class name


// 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";


dbtestservlet processrequest2

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

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

adding jsps displayed after connection
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

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.
further self study
Further self study
  • Java Programming
  • HTML
  • JSP Programming
  • Model View Controller(MVC) architectural pattern as it applies to JSP programming

Sample Code for this lab was written by Prof. WojciechGolab

Java, JSP and JDBC

The Java Tutorials

The Java Tutorial - Trail: JDBC(TM) Database Access

Introduction to Java Server Pages(JSP)

 Web Application Development

The Java EE 6 Tutorial

Tutorial - Creating a Simple Web Application Using a MySQL Database  (using NetBeans)

  • Reference for html

  • Reference for MVC and JSPs:

  • Ref for DAO :