Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 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 tags. To display any expression that can be converted to a string, you use the 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.

3 The syntax for a JSP scriptlet The syntax for a JSP expression The syntax for getting a parameter from the implicit request object request.getParameter(parameterName);

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

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

6 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! <% } %>

7 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++){ %> <% } %>

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

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

10 A Form tag that requests a JSP 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

11 Some JSP tags TagNamePurpose JSP scriptletTo insert a block of Java statements. JSP expressionTo display the string value of an expression. JSP directiveTo set conditions that apply to the entire JSP. JSP commentTo tell the JSP engine to ignore code.

12 A JSP comment <%-- Today’s date is. --%>

13 A JSP that displays the date and an instance variable

14 The code for the JSP Chapter 4 - Email List application <% 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);

15 The code for the JSP (continued) int localCount = 0; synchronized (this) { accessCount++; localCount = accessCount; } %> Today’s date is. This page has been accessed times.

16 How to use Java to work with a database

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

18 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(); }

19 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 Statement statement = connection.createStatement(); ResultSet products = statement.executeQuery( "SELECT * FROM Product ");

20 ResultSet methods for forward-only, read- only result sets MethodDescription 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.

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

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

23 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);

24 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);

25 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);

26 The SQL Gateway application executing an INSERT statement

27 The SQL Gateway application after executing a SELECT statement

28 The JSP code (sql_gateway.jsp) <% String sqlStatement = (String) session.getAttribute("sqlStatement"); if (sqlStatement == null) sqlStatement = ""; String message = (String) session.getAttribute("message"); if (message == null) message = ""; %> Chapter 11 - The SQL Gateway application The SQL Gateway Enter an SQL statement and click the Execute button. Then, information about the statement will appear at the bottom of this page.

29 The JSP code (continued) SQL statement: SQL result:


Download ppt "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."

Similar presentations


Ads by Google