MIS 415 Module 3 3Tier Webapps using JSPJDBC - PowerPoint PPT Presentation

1 / 41
About This Presentation
Title:

MIS 415 Module 3 3Tier Webapps using JSPJDBC

Description:

Designers. MIS415. 2. Querying. Developers. ISOM. Today's Buzzwords. 3 ... Parameters passed in this fashion may be accessed by the forwarded JSP (catalog. ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 42
Provided by: wri79
Category:
Tags: 3tier | jspjdbc | mis | module | using | webapps

less

Transcript and Presenter's Notes

Title: MIS 415 Module 3 3Tier Webapps using JSPJDBC


1
MIS 415 Module 33-Tier Webapps using JSP/JDBC
  • Arijit Sengupta

2
Structure of this semester
MIS415
1. Design
4. Advanced Topics
2. Querying
0. Intro
3. Applications
Database Fundamentals
Conceptual Modeling
Query Languages
Java DB Applications JDBC/JSP
XML Databases
Relational Model
Advanced SQL
Data Mining
Normalization
Newbie
Users
Professionals
Designers
Developers
3
Todays Buzzwords
  • 3-Tier applications
  • Client WebServer ApplicationServer
  • Basics of JDBC
  • Basics of JSP Containers - Tomcat
  • Web Applications using JSP

4
JDBC
  • A platform-independent library of classes
    allowing database access from any Java
    application
  • Take advantages of Polymorphism
  • JDBC is a set of interfaces
  • Driver, Connection, Statement, ResultSet, etc.
  • Database vendors (not programmers) will implement
    these interfaces.
  • If we switch from one database to another, we
    just need to load different driver (plug and
    play)!
  • YOU DON'T NEED TO MODIFY THE REST OF YOUR PROGRAM!

5
JDBC
6
JDBC
DriverManager
Connection
Statement
ResultSet
Driver
Database
7
JDBC (Contd.)
  • Register a JDBC driver
  • Driver d new oracle.jdbc.driver.OracleDriver()
  • DriverManager.registerDriver (d)
  • or
  • DriverManager.registerDriver (new
    oracle.jdbc.driver.OracleDriver())
  • Or, use the Java reflection abilities
  • Class.forName(
  • "oracle.jdbc.driver.OracleDriver")
  • calling Class.forName() will create an instance
    of a driver and register it with the
    DriverManager automatically
  • This is better since we can use a constant
  • String DRIVER "oracle.jdbc.driver.OracleDriver"
  • Class.forName(DRIVER)

8
JDBC
DriverManager
Connection
Statement
ResultSet
Driver
Database
9
JDBC (Contd.)
  • Make a connection
  • String URL "jdbcoraclethin_at_unixapps1.wright.e
    du1521ORA2"
  • Connection conn
  • DriverManager.getConnection(URL, USERNAME,
    PASSWORD)

10
JDBC
DriverManager
Connection
Statement
ResultSet
Driver
Database
11
JDBC (Contd.)
  • Create a statement
  • Statement st conn.createStatement()
  • //defaultTYPE_FORWARD_ONLY and CONCUR_READ_ONLY
  • or
  • Statement st conn.createStatement(
  • ResultSet.TYPE_SCROLL_SENSITIVE,
  • ResultSet.CONCUR_UPDATABLE)
  • //the resultset will be scrollable and sensitive
    to changes made by others
  • //we can update the resultset

12
JDBC
DriverManager
Connection
Statement
ResultSet
Driver
Database
13
JDBC (Contd.)
  • Execute a query
  • String SQL "INSERT INTO s"
  • " VALUES ('222-22-2222')"
  • int result st.executeUpdate(SQL)
  • //either the row count for INSERT, UPDATE or
    DELETE or 0 for SQL statements that return
    nothing
  • Execute a query and create a resultset
  • String SQL "SELECT FROM Student"
  • ResultSet rec st.executeQuery(SQL)

14
JDBC (Contd.)
  • Process the resultset
  • while(rec.next()) System.out.println(rec.getStri
    ng("snum"))
  • or
  • while(rec.next()) System.out.println(rec.getStri
    ng(1))
  • // first column of the resultset
  • There are methods like getString(), getInt(),
    etc. that take either a column name or column
    position
  • See http//java.sun.com/j2se/1.3/docs/guide/jdbc/i
    ndex.html for all JDBC Class documentation

15
Tomcat a J2EE Container
  • Open Source integrated as an Apache.org project
  • Can be obtained from http//tomcat.apache.org
  • Provides full JSP 2.0/Servlet 2.4 functionality
  • I have created one container environment for each
    team

16
Logging in
  • Log into my server busdb2.wright.edu using any
    ssh client (putty/ssh.com/)
  • Start your server
  • From the home directory, type
  • webstart
  • Stop your server
  • webstop
  • Test your server
  • http//busdb2.wright.edultyourportgt/
  • http//busdb2.wright.edultyourportgt/numguess.jsp
  • Your jsp files go under
  • tomcat/webapps/ROOT
  • Your class files go under
  • Tomcat/webapps/ROOT/WEB-INF/classes/ltyourpackagegt

17
Elements of a Java Server Page
  • Directives lt_at_ gt
  • Provide global information to the page
  • Import statements
  • Scripting language
  • u Declarations lt! gt
  • For page-wide variable and method declarations

18
Elements of a Java Server Page
  • Scriptlets lt gt
  • This is the Java code embedded in the web pages
  • Expressions lt gt
  • Formats the expression as a string to be included
    in the output of the web page
  • Comments lt-- --gt
  • User readable comments, contents ignored and
    removed by the JSP Compiler

19
JSP Directives
  • General syntax
  • lt_at_ directive attribute "value" gt
  • Possible values for directives are
  • Page - Information for the page
  • Include - Specifies the files whose contents are
    to be included in the output
  • e.g., lt_at_ include file"header.html" gt
  • Taglib
  • The URI for a library of custom tags that may be
    used in the page

20
JSP Page Directive
  • The page directive may take the following values
  • lt_at_ page language "java" gt
  • This variable tells the JSP engine what language
    will be used in the file
  • "java" is the only language supported by JSP in
    the current specification
  • lt_at_ page import "java.util., ItemValue" gt
  • Comma separated list of classes and packages that
    are used in the JSP page
  • Should appear at the top of the file

21
JSP Page Directive (Contd.)
  • lt_at_ page session "true false" gt
  • true indicates that session data is available to
    the page
  • By default, this is set to true
  • lt_at_ page buffer "none 16kb sizekb" gt
  • Determines the size of the output stream buffer
  • Defaults to 8kb
  • Use with autoFlush
  • lt_at_ page autoFlush "true false" gt
  • When set to true, flushes the output buffer when
    it is full, rather than raising an exception

22
JSP Page Directive (contd.)
  • lt_at_ page errorPage "mypage/error_handler.jsp"
    gt
  • Specifies the relative path of the page, where
    control would be transferred if any exceptions
    are thrown from this page
  • The error handling JSP page should have its
    isErrorPage directive set to true
  • lt_at_ page isErrorPage "true false" gt
  • Marks the page as an error handler

23
JSP Declarations
  • Class and instance variables (of the generated
    servlet class) may be specified using the JSP
    Declaration tag
  • lt! String name Web Applications"
  • int index 10
  • int count 0 gt
  • Methods may also be specified
  • lt!
  • private int getNextIndex()
  • return index
  • gt

24
JSP Scriptlets
  • JSP scriptlets are defined as block of Java code
    embedded between a pair of
  • tags, lt and gt.
  • Example
  • lt
  • java.util.Date d new java.util.Date()
  • out.println(d)
  • gt

25
JSP Expressions
  • Useful for embedding the result of a Java
    expression in a HTML page
  • The expression is encoded between the tags lt
    and gt
  • The value of the expression is converted to a
    string and then displayed
  • Conversion of primitive types to string happens
    automatically
  • Example
  • The date is lt new java.util.Date() gt

26
JSP Implicit Objects
  • When writing scriptlets and expressions, the
    following objects (called implicit objects) are
    available by default
  • request javax.servlet.http.HttpServletRequest
  • response javax.servlet.http.HttpServletResponse
  • out javax.servlet.jsp.JspWriter
  • session javax.servlet.http.HttpSession
  • application javax.servlet.ServletContext
  • exception java.lang.Throwable

27
Reading inputs from Forms/URLS
  • Remember that parameters are passed to Web
    applications via one of two methods
  • GET method parameters are passed directly
    through the URL, encoded using the urlencoding
    method
  • Quick to create and test can be created without
    a form
  • Can be bookmarked
  • URL shows in plaintext not secure
  • POST method parameters are encoded and sent to
    the server separately from the URL
  • Can only be created via forms (or advanced
    applications)
  • Secure parameters cannot be seen
  • Reading a single parameter via name
  • String value request.getParameter(paramname)
  • String values request.getParameterValues(para
    mname)
  • / for multivalued parameters like
    checkboxes/multiple selectable lists /
  • Better way using Beans - shortly

28
JSP Session Example
  • lthtmlgtltheadgtlttitlegtVisitor Count -- JSP
    Sessionlt/titlegt
  • lt/headgt ltbody bgcolor"FFFFFF"gt
  • lth2gtVisitor Countlt/h2gt
  • ltpgtThis JSP page demonstrates session management
    by incrementing a counter each time a user
    accesses a page.lt/pgt
  • lt!
  • private int totalHits 0
  • gt
  • lt session request.getSession(true)
  • Integer ival
  • (Integer)session.getValue("jspsession.counte
    r")
  • if (ival null) ival new Integer(1)
  • else ival new Integer(ival.intValue() 1)
  • session.putValue("jspsession.counter", ival)
  • gt
  • ltp align"center"gt
  • You have hit this page lt ival gt
  • timelt (ival.intValue() 1) ? "" "s" gt,
  • out of a total of lt totalHits gt page
  • hitlt (totalHits 1) ? "" "s" gt!lt/pgt

29
JSP Actions
  • Actions are tags that may affect the runtime
    behavior of the JSP or affect the current out
    stream they may also use, modify and/or create
    objects.
  • JSP specification defines the following standard
    actions
  • ltjspuseBeangt
  • ltjspsetPropertygt
  • ltjspgetPropertygt

30
JSP Actions
  • ltjspincludegt
  • ltjspforwardgt
  • ltjspparamgt
  • ltjspplugingt
  • New action types are introduced by means of
    custom tags

31
What is a JavaBean?
  • A Java class that (at a minimum)
  • has an empty constructor
  • has getters and setters for each of the
    properties in the class
  • implements the serializable interface

32
JSP Actions and Attributes
  • JSP actions can define named attributes and
    associated values
  • lt_at_ page import"People" errorPage"exception.jsp
    gt
  • ltjspuseBean id"myperson" class"People" /gt
  • lt myperson.setName("Ramesh") gt
  • lth1gtMy name is ltmyperson.getName()gtlt/h1gt
  • lt-- Let's now use the bean syntax --gt
  • Name using Bean is
  • ltjspgetProperty name"myperson" property"name"
    /gt

33
Beans and HTML forms
  • You may connect HTML form parameters to Bean
    properties
  • ltjspsetProperty name"id" property"accountHolder
    "
  • value "lt request.getParameter("accountHolder
    ")gt/gt
  • There is a shorthand for the above
  • ltjspsetProperty name"id" property"accountHold
    er" /gt
  • This works if the property name was exactly the
    same as the form parametername

34
Beans and HTML forms
  • If the form parameter name and the property name
    do not match, then use the following variant of
    "jspsetProperty"
  • ltjspsetProperty namemyperson" propertyage
    parammyage" /gt
  • Another powerful variation of "jspsetProperty"
    examines all the parameter names from the request
    object and if some of them match with property
    names, it sets the appropriate properties
  • ltjspsetProperty namemyperson" property"" /gt

35
Beans and HTML (contd.)
  • lt-- Trying the all powerful syntax --gt
  • ltjspsetProperty name"myperson" property"" /gt
  • Name is ltmyperson.getName()gt
  • Age is ltmyperson.getAge()gt
  • Weight is ltmyperson.getWeight()gt

36
Including Files
  • JSP supports two kinds of file inclusion
  • Static (using the directive "include")
  • Dynamic (or Request-Time)
  • Static inclusion is specified using the "include"
    directive
  • e.g., lt_at_ include file"header.html" gt
  • In static inclusion, the contents of
    "header.html" are included in the output of the
    containing JSP file during the JSP page
    compilation
  • If the contents of the "header.html" file change,
    these changes are not visible to the user of the
    containing JSP file
  • Static inclusion is fast (because inclusion is
    done at compile time)

37
Including Files (contd.)
  • Dynamic inclusion is supported by the use of the
    tag "jspinclude"
  • e.g., ltjspinclude page "news/headlines.jsp"
    flush "true" /gt
  • Each time the containing JSP file is accessed,
    the JSP engine includes the contents of the
    latest copy of the file "news/headlines.jsp
  • You may also pass parameters to the included
    file, e.g
  • ltjspinclude page "news/headlines.jsp"
    flush"true"gt
  • ltjspparam name"prefers" value"international"/gt
  • ...
  • lt/jspincludegt

38
Forwarding Files
  • A request to a JSP file may be forwarded,
    transparently to the user, to another JSP file
  • ltjspforward page "target" /gt
  • This is used to redirect request from one page to
    another page
  • The calling JSP file relinquishes control to the
    forwarded page
  • The calling JSP cannot send any data back to the
    browser before invoking this tag
  • If data had already been sent by the calling JSP,
    invoking the "jspforward" tag causes an
    exception to be thrown

39
Forwarding Requests
  • You may also pass parameters to the forwarded
    page
  • ltjspforward page"catalog.jsp" /gt
  • ltjspparam name"color" value"red"/gt
  • ...
  • lt/jspforwardgt
  • Parameters passed in this fashion may be accessed
    by the forwarded JSP (catalog.jsp) by the
    following call
  • request.getParameter("color")

40
Forwarding Requests
  • Request forwarding is typically used for
    authentication
  • lt
  • if(request.getParameter("login-info") null)
  • gt
  • ltjspforward page"login.jsp"/gt
  • lt
  • else
  • / Generate Error message no login /
  • gt

41
Summary
  • JSP JDBC provides a stable and well-tested
    platform for 3-tier web application design
  • Supports all major databases and all platforms
  • With Servlet compilation, performance is high
  • Many major applications like Wiki, major
    Government/Funding organizations like NSF are
    running on Java/JSP technology.
  • Web services are well-supported
  • J2EE provides a well-designed software design and
    development platform for enterprise systems.
Write a Comment
User Comments (0)
About PowerShow.com