Active Server Pages - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Active Server Pages

Description:

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 ... – PowerPoint PPT presentation

Number of Views:100
Avg rating:3.0/5.0
Slides: 30
Provided by: Roberto313
Category:
Tags: active | java | jdbc | pages | server

less

Transcript and Presenter's Notes

Title: Active Server Pages


1
Active Server Pages
  • ASP is Microsofts 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 lt and gt tags.
  • To display any expression that can be converted
    to a string, you use the lt and gt 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
  • lt Java statements gt
  • The syntax for a JSP expression
  • lt any Java expression that can be converted to
    a string gt
  • 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
  • lt
  • int numOfTimes 1
  • while (numOfTimes lt 5)
  • gt
  • lth1gt This line is shown lt numOfTimes gt of
    5 times.
  • lt/h1gt
  • lt
  • numOfTimes
  • gt

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 dont end it with a semicolon.

6
A scriptlet that determines if a checkbox is
checked
  • lt
  • String rockCheckBox request.getParameter("Rock")
  • // returns the value "on" if checked, null
    otherwise.
  • if (rockCheckBox ! null)
  • gt
  • You checked Rock music!
  • lt
  • gt

7
A scriptlet that reads and displays multiple
values from a list box
  • lt
  • String selectedCountries
    request.getParameterValues("country")
  • // returns the values of items selected in
    list box.
  • for (int i 0 i lt selectedCountries.length
    i)
  • gt
  • lt selectedCountriesi gt ltbrgt
  • lt
  • gt

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 youre running Tomcat on your PC, that
    directory is usually c\tomcat\webapps or
    c\jakarta-tomcat\webapps.
  • If youre 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 youre 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
  • ltform action"show_email_entry.jsp" method"get"gt
  • Two URLs that request a JSP
  • http//localhost8080/murach/email4/show_email_ent
    ry.jsp
  • http//www.murach.com/email4/show_email_entry.jsp
  • How to include parameters
  • show_email_entry.jsp?firstNameJohn
  • show_email_entry.jsp?firstNameJohnlastNameSmith

11
Some JSP tags
  • Tag Name Purpose
  • lt gt JSP scriptlet To insert a block of Java
    statements.
  • lt gt JSP expression To display the string
    value of an expression.
  • lt_at_ gt JSP directive To set conditions that
    apply to the entire JSP.
  • lt-- --gtJSP comment To tell the JSP engine to
    ignore code.

12
A JSP comment
  • lt--
  • Todays date is lt new java.util.Date() gt.
  • --gt

13
A JSP that displays the date and an instance
variable
14
The code for the JSP
  • lt!doctype html public "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtChapter 4 - Email List
    applicationlt/titlegt
  • lt/headgt
  • ltbodygt
  • lt_at_ page import "business.User, data.UserIO,
    java.util.Date, java.io." gt
  • lt! int accessCount 0 gt
  • lt! String file "../webapps/murach/WEB-INF/etc/
    UserEmail.txt" gt
  • lt String firstName request.getParameter("first
    Name")
  • String lastName request.getParameter("lastNa
    me")
  • String emailAddress request.getParameter("em
    ailAddress")
  • 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
  • gt
  • lt!-- missing code --gt
  • Todays date is lt new Date() gt. ltbrgt
  • ltigtThis page has been accessed lt localCount gt
  • times.lt/igt
  • lt/bodygt
  • lt/htmlgt

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
  • jdbcsubprotocolNamedatabaseURL
  • How to connect to a MySQL db named murach
  • Connection connection null
  • try
  • Class.forName("org.gjt.mm.mysql.Driver")
  • String dbURL "jdbcmysql//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_at_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
  • 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 sets 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 cant 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)
  • lt!doctype html public "-//W3C//DTD HTML 4.0
    Transitional//EN"gt
  • lt
  • String sqlStatement (String)
    session.getAttribute("sqlStatement")
  • if (sqlStatement null) sqlStatement ""
  • String message (String) session.getAttribute(
    "message")
  • if (message null) message ""
  • gt
  • lthtmlgt
  • ltheadgt
  • lttitlegtChapter 11 - The SQL Gateway
    applicationlt/titlegt
  • lt/headgt
  • ltbodygt
  • lth1gtThe SQL Gatewaylt/h1gt
  • ltpgtEnter an SQL statement and click the Execute
    button. Then, information about the ltbrgt
  • statement will appear at the bottom of this
    page.lt/pgt

29
The JSP code (continued)
  • ltform action"../servlet/sql11.SQLGatewayServlet"
    method"post"gt
  • ltbgtSQL statementlt/bgtltbrgt
  • lttextarea name"sqlStatement" cols60 rows8gt
  • lt sqlStatement gt
  • lt/textareagtltbrgt
  • ltbrgt
  • ltinput type"submit" value"Execute"gt
  • lt/formgt
  • ltpgt
  • ltbgtSQL resultlt/bgtltbrgt
  • lttable cellpadding"5" border"1"gt
  • lt message gt
  • lt/tablegt
  • lt/pgt
  • lt/bodygt
  • lt/htmlgt
Write a Comment
User Comments (0)
About PowerShow.com