Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.

Slides:



Advertisements
Similar presentations
CE203 - Application Programming Autumn 2013CE203 Part 51 Part 5.
Advertisements

Murach’s Java SE 6, C21© 2007, Mike Murach & Associates, Inc.Slide 1.
JDBC. Java Database Connectivity (JDBC) Use the java.sql package to query and update the database. JDBC is an API that allows java to communicate with.
1 JDBC Java Database Connectivity. 2 c.pdf
18-Jun-15 JSP Java Server Pages Reference: Tutorial/Servlet-Tutorial-JSP.html.
DT228/3 Web Development Databases. Database Almost all web application on the net access a database e.g. shopping sites, message boards, search engines.
ASP.NET Programming with C# and SQL Server First Edition Chapter 8 Manipulating SQL Server Databases with ASP.NET.
Three-Tier Architecture Oracle DB Server Apache Tomcat App Server Microsoft Internet Explorer HTML Tuples HTTP Requests JDBC Requests Java Server Pages.
Form Handling, Validation and Functions. Form Handling Forms are a graphical user interfaces (GUIs) that enables the interaction between users and servers.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
1 Java Database Connection (JDBC) There are many industrial-strength DBMS's commercially available in the market. Oracle, DB2, and Sybase are just a few.
CSCI 6962: Server-side Design and Programming JDBC Database Programming.
Jaeki Song JAVA Lecture 11 Java Database Connectivity.
Beginning Databases with JDBC Mike Bradley Adapted from and notes by Kevin Parker, Ph.D.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Database Programming in Java Corresponds with Chapter 32, 33.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use.
JDBC (Java Database Connectivity) SNU OOPSLA Lab. October 2005.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
CONTROLPANEL Java.sql package This package provides the APIs for accessing and processing data which is stored in the database especially relational.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 32.1 Reviewing the Bookstore Application 32.2.
Introduction.  The scripting language most often used for client-side web development.  Influenced by many programming languages, easier for nonprogrammers.
JDBC Enterprise Systems Programming. JDBC  Java Database Connectivity  Database Access Interface provides access to a relational database (by allowing.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 31.1 Reviewing the Bookstore Application 31.2.
Chapter 17 Accessing Databases with JDBC. JDBC JDBC provides a standard library for accessing relational databases. By using the JDBC API, you can access.
JDBC. Java.sql.package The java.sql package contains various interfaces and classes used by the JDBC API. This collection of interfaces and classes enable.
JDBC CS 124. JDBC Java Database Connectivity Database Access Interface provides access to a relational database (by allowing SQL statements to be sent.
Li Tak Sing COMPS311F. Database programming JDBC (Java Database Connectivity) Java version of ODBC (Open Database Connectivity) ODBC provides a standard.
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
CS562 Advanced Java and Internet Application Introduction to the Computer Warehouse Web Application. Java Server Pages (JSP) Technology. By Team Alpha.
Database Access Using JDBC BCIS 3680 Enterprise Programming.
8 Chapter Eight Server-side Scripts. 8 Chapter Objectives Create dynamic Web pages that retrieve and display database data using Active Server Pages Process.
Access Databases from Java Programs via JDBC Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale
Database Processing with JSP ISYS 350. Example: Enter CID in a box and retrieve the customer record.
Introduction to JDBC Instructor: Mohamed Eltabakh 1.
1 JDBC – Java Database Connectivity CS , Spring 2010.
1 Download current version of Tomcat from: g/tomcat/ g/tomcat/ Install it in: C:\Program Files\Apache.
Intro to JDBC Joseph Sant Applied Computing and Engineering Sciences Sheridan ITAL.
Java and database. 3 Relational Databases A relational Database consists of a set of simple rectangular tables or relations The column headings are.
Database Processing with JSP ISYS 350. Database Applications Applications Database Server Queries/Updates Results.
CS320 Web and Internet Programming Database Access with JDBC Chengyu Sun California State University, Los Angeles.
JSP java server pages.
ASP.NET Programming with C# and SQL Server First Edition
CS3220 Web and Internet Programming Database Access with JDBC
Note: To complete the examples in this section you need access to a database!! Most of the examples work for any database with JDBC drivers. However, connecting.
Running a Forms Developer Application
Introduction to Dynamic Web Programming
JDBC Database Management Database connectivity
JDBC – Java Database Connectivity
Java Server Pages.
CS320 Web and Internet Programming Database Access with JDBC
JDBC.
ISC440: Web Programming 2 Server-side Scripting PHP 3
Database Processing with JSP
Introduction to Server-Side Web Development using JSP and Databases
Objectives In this lesson, you will learn about:
Java Server Pages.
Interacting with Database
Bolat Azamat, Kim Dongmin
JAVA DATABaSE CONNECTIVITY
JDBC Example.
Lecture 11 Database Connection
CS3220 Web and Internet Programming Database Access with JDBC
CS3220 Web and Internet Programming Database Access with JDBC
Presentation transcript:

Active Server Pages ASP is Microsoft’s server-side script engine for dynamically-generated web pages. Most common language used is VBScript. If you use JavaScript, then you have your JSP.

A summary of the code for the JSP Although a JSP looks much like an HTML page, a JSP contains embedded Java code. To code a scriptlet that contains one or more Java statements, you use the <% and %> tags. To display any expression that can be converted to a string, you use the <%= and %> tags. When you code a JSP, you can use the implicit request object. This object is named request. You can use the getParameter method of the request object to get the values of the parameters that are passed to the JSP.

The syntax for a JSP scriptlet <% Java statements %> The syntax for a JSP expression <%= any Java expression that can be converted to a string %> The syntax for getting a parameter from the implicit request object request.getParameter(parameterName);

Two scriptlets and an expression that display an HTML line 5 times <% int numOfTimes = 1; while (numOfTimes <= 5){ %> <h1> This line is shown <%= numOfTimes %> of 5 times. </h1> numOfTimes++; }

How to code scriptlets and expressions Within a scriptlet, you can code one or more complete Java statements. Because these statements are Java statements, you must end each one with a semicolon. Within a JSP expression, you can code any Java expression that evaluates to a string. This includes Java expressions that evaluate to any of the primitive types, and it includes any object that has a toString method. Because a JSP expression is an expression, not a statement, you don’t end it with a semicolon.

A scriptlet that determines if a checkbox is checked <% String rockCheckBox = request.getParameter("Rock"); // returns the value "on" if checked, null otherwise. if (rockCheckBox != null){ %> You checked Rock music! <% }

A scriptlet that reads and displays multiple values from a list box <% String[] selectedCountries = request.getParameterValues("country"); // returns the values of items selected in list box. for (int i = 0; i < selectedCountries.length; i++){ %> <%= selectedCountries[i] %> <br> }

Where and how to save a JSP JSPs are normally saved in the same directory as the HTML pages. This directory should be a subdirectory of the web applications directory for your server. If you’re running Tomcat on your PC, that directory is usually c:\tomcat\webapps or c:\jakarta-tomcat\webapps. If you’re using Tomcat on your local system, you can also use webapps\ROOT as the root directory for your applications. To make sure that the filename for a JSP is saved with the jsp extension when you’re using an HTML or text editor, you can enter the filename within quotes.

How to request a JSP When you use the Get method to request a JSP from an HTML form, the parameters are automatically appended to the URL. When you code or enter a URL that requests a JSP, you can add a parameter list to it starting with a question mark and with no intervening spaces. Then, each parameter consists of its name, an equals sign, and its value. To code multiple parameters, use ampersands (&) to separate the parameters.

A Form tag that requests a JSP <form action="show_email_entry.jsp" method="get"> Two URLs that request a JSP http://localhost:8080/murach/email4/show_email_entry.jsp http://www.murach.com/email4/show_email_entry.jsp How to include parameters show_email_entry.jsp?firstName=John show_email_entry.jsp?firstName=John&lastName=Smith

Some JSP tags Tag Name Purpose <% %> JSP scriptlet To insert a block of Java statements. <%= %> JSP expression To display the string value of an expression. <%@ %> JSP directive To set conditions that apply to the entire JSP. <%-- --%>JSP comment To tell the JSP engine to ignore code.

A JSP comment <%-- Today’s date is <%= new java.util.Date() %>. --%>

A JSP that displays the date and an instance variable

The code for the JSP <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Chapter 4 - Email List application</title> </head> <body> <%@ page import= "business.User, data.UserIO, java.util.Date, java.io.*" %> <%! int accessCount = 0; %> <%! String file = "../webapps/murach/WEB-INF/etc/UserEmail.txt"; %> <% String firstName = request.getParameter("firstName"); String lastName = request.getParameter("lastName"); String emailAddress = request.getParameter("emailAddress"); User user = new User(firstName, lastName, emailAddress); UserIO.addRecord(user, file);

The code for the JSP (continued) int localCount = 0; synchronized (this) { accessCount++; localCount = accessCount; } %> <!-- missing code --> Today’s date is <%= new Date() %>. <br> <i>This page has been accessed <%= localCount %> times.</i> </body> </html>

How to use Java to work with a database

How to connect to a database Before you can get or modify the data in a database, you need to connect to it. To do that, you use the forName method of the Class class to load the driver. Then, you use the getConnection method of the DriverManager class to return a Connection object. When you use the forName method of the Class class, you must supply the driver name. This method throws a ClassNotFoundException. When you use the getConnection method of the DriverManager class, you must supply a URL for the database, a username, and a password. This method throws a SQLException. Although the connection string for each driver is different, the documentation for the driver should explain how to write a connection string for that driver.

How to connect to a MySQL db named murach Database URL syntax jdbc:subprotocolName:databaseURL How to connect to a MySQL db named murach Connection connection = null; try{ Class.forName("org.gjt.mm.mysql.Driver"); String dbURL = "jdbc:mysql://localhost/murach"; String username = "root"; String password = ""; connection = DriverManager.getConnection( dbURL, username, password); } catch(ClassNotFoundException e){message = "Database driver not found.";} catch(SQLException e){ message = "Error loading database driver: "+ e.getMessage();

How to create a result set that contains 1 row and 1 column Statement statement = connection.createStatement(); ResultSet userIDResult = statement.executeQuery( "SELECT UserID FROM User " + "WHERE EmailAddress = 'jsmith@hotmail.com'"); How to create a result set that contains multiple columns and rows ResultSet products = statement.executeQuery( "SELECT * FROM Product ");

ResultSet methods for forward-only, read-only result sets Method Description next() Moves the cursor to the next row in the result set. last() Moves the cursor to the last row in the result set. close() Releases the result set’s JDBC and database resources. getRow() Returns an int value that identifies the current row of the result set.

Description To return a result set to a class, you use the createStatement method of a Connection object to create a Statement object. Then, you use the executeQuery method of the Statement object to execute a SELECT statement that returns a ResultSet object. By default, the createStatement method creates a forward-only, read-only result set. This means that you can only move the cursor through it from the first record to the last and you can’t update it. When a result set is created, the cursor is positioned before the first row. Then, you can use the methods of the ResultSet object to move the cursor. The createStatement, executeQuery, and next methods throw an SQLException. As a result, any code that uses these methods needs to catch or throw this exception.

How to retrieve data from a result set The getXXX methods can be used to return all eight primitive types. For example, the getInt method returns the int type and the getLong method returns the long type. The getXXX methods can also be used to return strings, dates, and times. For example, the getString method returns any object of the String class, and the getDate, getTime, and getTimestamp methods return objects of the Date, Time, and Timestamp classes of the java.sql package.

How to use the executeUpdate method to add a record String query = "INSERT INTO Product (ProductCode, ProductDescription, ProductPrice) " + "VALUES (‘ " + product.getCode() + "‘ , " + "‘ " + product.getDescription() + "‘ , " + "‘ " + product.getPrice() + "‘ )"; Statement statement = connection.createStatement(); int rowCount = statement.executeUpdate(query);

How to use the executeUpdate method to update a record String query = "UPDATE Product SET " + "ProductCode = ‘" + product.getCode() + "‘, " + "ProductDescription = ‘" + product.getDescription() + "‘, " + "ProductPrice = ‘ " + product.getPrice() + "‘ " + "WHERE ProductCode = ‘ " + product.getCode() + "‘"; Statement statement = connection.createStatement(); int rowCount = statement.executeUpdate(query);

How to use the executeUpdate method to delete a record String query = "DELETE FROM Product " + "WHERE ProductCode = ‘ " + productCode + "‘ "; Statement statement = connection.createStatement(); int rowCount = statement.executeUpdate(query);

The SQL Gateway application executing an INSERT statement

The SQL Gateway application after executing a SELECT statement

The JSP code (sql_gateway.jsp) <!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN"> <% String sqlStatement = (String) session.getAttribute("sqlStatement"); if (sqlStatement == null) sqlStatement = ""; String message = (String) session.getAttribute("message"); if (message == null) message = ""; %> <html> <head> <title>Chapter 11 - The SQL Gateway application</title> </head> <body> <h1>The SQL Gateway</h1> <p>Enter an SQL statement and click the Execute button. Then, information about the <br> statement will appear at the bottom of this page.</p>

The JSP code (continued) <form action="../servlet/sql11.SQLGatewayServlet" method="post"> <b>SQL statement:</b><br> <textarea name="sqlStatement" cols=60 rows=8> <%= sqlStatement %> </textarea><br> <br> <input type="submit" value="Execute"> </form> <p> <b>SQL result:</b><br> <table cellpadding="5" border="1"> <%= message %> </table> </p> </body> </html>