Java Server Pages (JSP) - PowerPoint PPT Presentation

1 / 221
About This Presentation
Title:

Java Server Pages (JSP)

Description:

Java Server Pages (JSP) ... – PowerPoint PPT presentation

Number of Views:858
Avg rating:3.0/5.0
Slides: 222
Provided by: 6649732
Category:
Tags: jsp | buttons | create | java | pages | server | using

less

Transcript and Presenter's Notes

Title: Java Server Pages (JSP)


1
Java Server Pages (JSP)
2
Symbol Names
Name Name
Tilde ' Single quotation mark, Apostrophe
Grave " Double quotation mark
! Exclamation mark / Slash, Virgule
_at_ At sign, Commercial at \ Back slash
Number sign, Crosshatch, Pound sign Vertical bar
Dollar sign , Comma
Percent sign . Dot, Period, Full stop
Hat sign, Circumflex ? Question mark
Ampersand ( ) (Left / Right) parenthesis
Asterisk, Star sign (Left / Right) braces, curved parenthesis
Dash, Hyphen, Minus sign (Left / Right) bracket, squared parenthesis
_ Underscore, Underline lt gt (Left / Right) angle bracket, pointed brackets
Plus sign ? ? (Left / Right) unidirectional arrows
Equal sign ? Bidirectional arrow
Colon
Semicolon
3
JSP Motivation
4
World Wide Web
  • The World Wide Web is a system of interlinked
    hypertext documents accessed via the Internet.
  • With a web browser, one can view Web pages that
    may contain text, images, videos, and other
    multimedia and navigate between them using
    hyperlinks.

5
Architecture of WWW
HTML file
content
request with arguments
user input
File Input
Web Browser
Web Server
GUI
HTTP
input
response
user output
Interface
Server-side program
output
6
DigressionHTML
7
HTML
  • HTML, an initialism of HyperText Markup Language,
    is the predominant markup language for web pages.
  • A markup language is a set of annotations to text
    that describe how it is to be structured, laid
    out, or formatted.
  • Hypertext refers to text on a computer that will
    lead the user to other, related information on
    demand.
  • Rather than remaining static like traditional
    text, hypertext makes possible a dynamic
    organization of information through links and
    connections (called hyperlinks).

8
HTML
  • HTML provides a means to describe the structure
    of text-based information in a document by
    denoting certain text as links, headings,
    paragraphs, lists, and so on and to supplement
    that text with interactive forms, embedded
    images, and other objects.
  • HTML is written in the form of tags, surrounded
    by angle brackets.

9
Simple Example
Tags
  • ltHTMLgt
  • ltHEADgt
  • ltTITLEgtSimple HTMLlt/TITLEgt
  • lt/HEADgt
  • ltBODYgt
  • ltH1gtHeadlt/H1gt
  • First paragraphltPgt
  • Second paragraphltPgt
  • lt/BODYgt
  • lt/HTMLgt

Angle brackets
Heading
Paragraphs
10
HTML Markup Tag
  • Formats
  • lttag-namegt
  • e.g. ltbrgt
  • lttag-namegt text lt/tag-namegt
  • e.g. ltbgtBoldlt/bgt, ltTITLEgt lt/TITLEgt, ltBODYgt
    lt/BODYgt
  • lttag-name attributeargumentgt
  • e.g. ltimg src"a.jpg"gt
  • lttag-name attributeargumentgt text lt/tag-namegt
  • e.g. lta href"http//www.muohio.edu"gtMiami
    Universitylt/agt
  • Note
  • Tag-names are case-insensitive.
  • ltHTMLgt, lthtmlgt, ltHtmlgt, lthTmLgt
  • Multiple consecutive whitespaces (space, tab,
    EOLN) are regarded as one space.

11
HTML Text
  • paragraph ltPgt ... lt/Pgt
  • line break ltBRgt
  • centering ltCENTERgt ... lt/CENTERgt
  • bold ltBgt ... lt/Bgt
  • italic ltIgt ... lt/Igt
  • headline ltHngt ... lt/Hngt
  • Many tags are deprecated now. (no more standard)

12
HTML Special Characters
  • Format
  • character-name
  • Special characters
  • lt ? LT
  • gt ? GT
  • " ? QUOTE
  • ? AMP

13
HTML Hyperlink
  • Anchor Tag
  • ltA HREF "address"gt ... lt/Agt
  • e.g.
  • ltA HREF"http//www.apple.com"gtApplelt/Agt
  • Relative Path vs. Absolute Path
  • HREF "abc.html"
  • HREF "../ddd/abc.html"
  • HREF "http//www.joongang.co.kr"

14
HTML List
  • Unnumbered Lists
  • ltULgt ltLIgt ... ltLIgt ... lt/ULgt
  • Numbered Lists
  • ltOLgt ltLIgt ... ltLIgt ... lt/OLgt
  • Definition Lists
  • ltDLgt ltDTgt ... ltDDgt ... ltDTgt ... ltDDgt ... lt/DLgt

15
HTML Image
  • ltIMG SRC "address/... .gif" ALIGN
    TOP/MIDDLE/BOTTOM/LEFT/RIGHTgt
  • ltIMG SRC "address/... .jpg"gt
  • e.g.
  • ltIMG SRC "picture.gif"gt

16
HTML Table
  • ltTABLE BORDER ngt
  • ltCAPTIONgt ... lt/CAPTIONgt
  • ltTRgt
  • ltTHgt ... lt/THgt
  • ltTDgt ... lt/TDgt
  • lt/TRgt
  • ltTRgt
  • ltTDgt ... lt/TDgt
  • ltTDgt ... lt/TDgt
  • lt/TRgt
  • lt/TABLEgt
  • e.g.
  • ltTABLE BORDERgt
  • ltTRgt
  • ltTDgt A11 lt/TDgt
  • ltTDgt A12 lt/TDgt
  • lt/TRgt
  • ltTRgt
  • ltTHgt B11 lt/THgt
  • ltTDgt B12 lt/TDgt
  • lt/TRgt
  • lt/TABLEgt

17
HTML Form
  • Will be addressed later.

18
Exercise 1 Text and Hyperlinklta href""gtlt/agt
19
Exercise 2 Numbered Listltolgtltligtltligtlt/olgt
20
Exercise 3 Numbered List and Hyperlink
ltolgtltligtlta href""gtlt/agtltligtlt/olgt
21
Exercise 4 Table lttablegtlttrgtlttdgtlt/tdgt...lt/trgtlt/
tablegt
22
Exercise 5 Table and Hyperlinklttablegtlttrgtlttdgtlta
href""gtlt/agtlt/tdgt...lt/trgtlt/tablegt
23
Exercise 6 Table and Heightlttd height"50"gt
24
Exercise 7 Table and Widthlttd width"200"gt
25
Exercise 8 Table and Sizelttd height"50"
width"200"gt
26
Exercise 9 Image ltimg src"a.jpg"gt
27
Exercise 10 Image and Hyperlink lta
href""gtltimg src""gtlt/agt
28
Exercise 11 Table, Image and Hyperlink
lttablegtlttrgtlttdgtlt/tdgtlttdgtlta href""gtltimg
src""gtlt/agtlt/tdgtlt/trgtlt/tablegt
29
Architecture of WWW
HTML file
content
request with arguments
user input
File Input
Web Browser
Web Server
GUI
HTTP
input
response
user output
Interface
Server-side program
output
30
Architecture of WWW
  • What happens if Web servers support only HTML
    files?

31
Architecture of WWW (?)
HTML file
content
request with arguments
user input
File Input
Web Browser
Web Server
GUI
HTTP
input
response
Server-side program
user output
Interface
output
32
Architecture of WWW
  • What happens if Web servers support only HTML
    files?
  • Static Web Pages vs. Dynamic Web Pages
  • The dynamic Web pages are used for
  • Login
  • Registration
  • Shopping cart
  • Payment
  • Enrollment

33
Dynamic Web Pages
  • What if Web servers themselves process the
    dynamics?
  • Fast
  • Developing Web servers requires expertise, so it
    would be complicated and even risky for
    application developers to modify a Web server.
  • Alternative approach?
  • An independent program executed by a Web server

34
Architecture of WWW
HTML file
content
request with arguments
user input
File Input
Web Browser
Web Server
GUI
HTTP
input
response
Server-side program
user output
Interface
output
35
Dynamic Web Pages
  • Requirements for Dynamic Web Pages
  • User input
  • ? Webform by HTML FORM Tag
  • Processing based on the user input
  • ? JSP

36
DigressionWebform
37
Webform
  • A webform on a web page allows a user to enter
    data that is sent to a server for processing.
  • Webforms resemble paper forms because internet
    users fill out the forms using checkboxes, radio
    buttons, or text fields.
  • For example, webforms can be used to enter
    shipping or credit card data to order a product
    or can be used to retrieve data (e.g., searching
    on a search engine).

38
Webform FORM Tag
  • Syntax
  • ltFORM
  • METHOD"POST"/"GET"
  • ACTION"server-side-program"
  • ENCTYPE"application/x-www-form-urlencoded"gt
  • ...
  • lt/FORMgt
  • The FORM tag is just an envelop for a Webform.
    Additional tags like INPUT, TEXTAREA, SELECT are
    required for a Webform.

39
Webform INPUT Tag
  • Syntax
  • ltINPUT NAME"name"
  • VALUE"value"
  • SIZE"n"
  • MAXLENGTH"n"
  • TYPE TEXT/PASSWORD
  • /CHECKBOX/RADIO
  • /SUBMIT/RESETgt
  • Used to input a line of characters

40
Webform INPUT Tag
  • e.g.
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHOD"POST" ACTION"login.jsp"gt
  • Username ltINPUT TYPE"TEXT" NAME"UN"gt ltBRgt
  • Password ltINPUT TYPE"PASSWORD" NAME"PW"gt
  • ltBRgt
  • ltINPUT TYPE"SUBMIT" VALUE"Login"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

41
Webform TEXTAREA Tag
  • Syntax
  • ltTEXTAREA NAME"name" ROWS"n" COLS"n"gt
  • Line 1
  • Line 2
  • Line n
  • lt/TEXTAREAgt
  • Used to input multiple lines of text

42
Webform TEXTAREA Tag
  • e.g.
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHOD"POST" ACTION"hello.jsp"gt
  • ltDLgt
  • ltDTgt
  • ltBgt Commentlt/Bgt
  • ltDDgt
  • ltTEXTAREA NAME"Comment" ROWS"8" COLS"55"gt
  • Hello!
  • How is it going?
  • Fine, thank you.
  • lt/TEXTAREAgt
  • lt/DLgt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

43
Webform SELECT Tag
  • Syntax
  • ltSELECT NAME"name" SIZE"n" MULTIPLEgt
  • ltOPTIONgt option 1
  • ltOPTIONgt option 2
  • ltOPTIONgt option n
  • lt/SELECTgt
  • Used to support a list box or a combo box

44
Webform SELECT Tag
  • e.g.
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHOD"POST" ACTION"select.jsp"gt
  • ltSELECT NAME"group" SIZE"3" MULTIPLEgt
  • ltOPTIONgt Title
  • ltOPTIONgt Description
  • ltOPTIONgt URL
  • lt/SELECTgt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

45
Exercise 1 Edit Box ltINPUT TYPE"TEXT" gt
46
Exercise 2 Edit Box with Initial Value ltINPUT
TYPE"TEXT" VALUE"Song" gt
47
Exercise 3 Edit Box and ButtonltINPUT
TYPE"SUBMIT" gt
48
Exercise 4 Edit Box and Button ltBRgt
49
Exercise 5 Login
50
Exercise 6 Check BoxltINPUT TYPE"CHECKBOX" gt
51
Exercise 7 Radio ButtonltINPUT TYPE"RADIO"
NAME"" VALUE""gt
52
Exercise 8 Form with Table
53
Exercise 9 Colspan ltTD COLSPAN"2"gtlt/TDgt
54
Exercise 10 Colspan and AlignltTD COLSPAN"2"
ALIGN"CENTER"gtlt/TDgt
55
Exercise 11 Table without Border
56
Exercise 12 General Form
57
JSP Getting Started
58
What is JSP?
  • Java Server Pages
  • Based on Java programming language
  • State-of-the-art technology to build Web pages
    having dynamic contents
  • Contrasts to Web pages having static contents by
    using HTML only
  • JSP-based Web pages delivers "different" Web
    contents to different persons, while HTML-based
    Web pages delivers "same" Web contents to
    different persons.

59
Dynamic Content Technologies
  • Common Gateway Interface (CGI)
  • ColdFusion
  • Active Server Pages
  • Server-side JavaScript
  • PHP (Personal Home Page, Professional Hypertext
    Preprocessor)
  • Java Servlets
  • Java Server Pages

60
What We Need?
  • JDK (Java Development Kit)
  • Core module for Java programming
  • Web Server
  • Sun Java System Application Server
  • IDE (Integrated Development Environment)
  • Supports Editing, Compile, Debugging, etc.
  • NetBeans (http//www.netbeans.org/)

61
Where are the Tools?
  • http//java.sun.com/
  • Java SE JDK
  • jdk-6u4-windows-i586-p.exe or later
  • Java EE Tools
  • java-tools-bundle-update4-windows or later

62
Using NetBeans IDE
  • Build a New Project
  • Edit the Source Code
  • Compile
  • Execute (Run)
  • Debug

63
Hello, World! - I
  • ltHTMLgt
  • ltBODYgt
  • Hello, World!
  • lt/BODYgt
  • lt/HTMLgt

64
Hello, World! - II
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String visitor request.getParameter("name")
  • if (visitor null) visitor "World"
  • gt
  • Hello, lt visitor gt!
  • lt/BODYgt
  • lt/HTMLgt

Scriptlet
Expression
65
Visiting the Page
  • http//localhost8080/hello.jsp
  • http//localhost8080/hello.jsp?nameSong

66
JSP Scripting elements
67
JSP Tag Styles
  • Scripting-oriented tags
  • lt gt
  • e.g.
  • lt_at_ page import"java.io." gt
  • lt r 7.5 gt
  • lt r 2 gt
  • XML-based tags
  • ltjsptag attribute1value1 /gt
  • ltjsptag attribute1value1 gt lt/jsptaggt
  • e.g.
  • ltjspdirective.page import"java.io." /gt

68
JSP Tags
  • JSP Directives
  • lt_at_ page gt
  • e.g. lt_at_ page import"java.io." gt
  • lt_at_ include gt
  • lt_at_ taglib gt
  • Scripting Elements
  • lt! declarations gt
  • lt expression gt
  • e.g. lt r 2 gt
  • lt scriptlet gt
  • e.g. lt r 7.5 gt

69
Scripting Element - Expression
  • lt expression gt
  • e.g.
  • lt a b 3 - f / d gt
  • lt fact(7) gt

70
Scripting Element - Expression
  • ltHTMLgt
  • ltBODYgt
  • sin(3.14)
  • lt Math.sin(3.14) gt
  • lt/BODYgt
  • lt/HTMLgt

71
Scripting Element - Scriptlet
  • lt statements gt
  • e.g.
  • lt int i, f, s 0
  • for (i 1, f 0 i lt f i)
  • s i gt

72
Scripting Element - Scriptlet
  • ltHTMLgt
  • ltBODYgt
  • lt
  • int i
  • for (i 0 i lt 100 i)
  • out.println(i "ltBRgt")
  • gt
  • lt/BODYgt
  • lt/HTMLgt

73
Hello, World! - II
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String visitor request.getParameter("name")
  • if (visitor null) visitor "World"
  • gt
  • Hello, lt visitor gt!
  • lt/BODYgt
  • lt/HTMLgt

74
Comments
  • Content Comments
  • lt! comment --gt
  • Comments for HTML
  • Web browser processes the comments.
  • JSP Comments
  • lt-- comment --gt
  • Comments for JSP
  • JSP processes the comments.
  • Scripting Language Comments
  • // comment
  • / comment /
  • Comments for Java
  • Expression or Scriptlet process the comments.

75
JSP Request object
76
Implicit Objects
  • Servlet-related object
  • page
  • config
  • Input/Output object
  • request
  • response
  • out
  • Contextual object
  • session
  • application
  • pageContext
  • Error-processing object
  • exception

77
page Object
  • getServletInfo()
  • e.g.
  • lt ((javax.servlet.jsp.HttpJspPage)page).getServl
    etInfo() gt

78
config Object
  • getInitParameterNames()
  • getInitParameter(String name)
  • e.g.
  • lt String un config.getInitParameter("username")
    gt

79
request Object
  • Request Contents
  • getParameterNames()
  • getParameter(String name)
  • getParameterValues(String name)
  • Request Header
  • getHeaderNames()
  • getHeader(String name)
  • getHeaders(String name)
  • getIntHeader(String name)
  • getDateHeader(String name)
  • getCookies()
  • Others
  • getMethod()
  • getRequestURL()
  • getQueryString()
  • getSession(flag)
  • getRequestDispatcher(path)
  • getRemoteHost()
  • getRemoteAddr()
  • getRemoteUser()
  • getSession(flag)

80
Parameter
  • login.html
  • login.jsp
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHODPOST ACTION"login.jsp"gt
  • Username ltINPUT TYPETEXT NAME"UN"gt ltBRgt
  • Password ltINPUT TYPEPASSWORD NAME"PW"gt ltBRgt
  • ltINPUT TYPESUBMIT VALUE"Login"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String username request.getParameter("UN")
  • String password request.getParameter("PW")
  • out.println(username "/" password)
  • gt
  • lt/BODYgt
  • lt/HTMLgt

81
Types of Inputs
  • ltINPUTgt
  • Text
  • Password
  • Checkbox
  • Radio
  • Submit
  • ltTEXTAREAgt
  • ltSELECTgt

82
Example
  • test.html
  • set.jsp
  • (e.g.)
  • ltHTMLgt
  • ltBODYgt
  • ltFORM methodpost action"set.jsp"gt
  • ltINPUT typetext nameInput value"OK" size"10"
    maxlength"15"gt
  • ltINPUT typesubmit namebutton value"Prev"gt
  • ltINPUT typesubmit namebutton value"Index"gt
  • ltINPUT typesubmit namebutton value"Next"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt
  • lt
  • String a request.getParameter("Input")
  • String b request.getParameter("button")
  • gt

83
Exercise 1 Edit Box
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • ltinput typetext name"username" value"Song"gt
    ltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

84
Exercise 2 Login
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • Username ltinput typetext name"un"gt ltBRgt
  • Password ltinput typepassword name"pw"gt ltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

85
Exercise 3 Check Box
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • Username ltinput typetext name"un"gt ltBRgt
  • Password ltinput typepassword name"pw"gt ltBRgt
  • ltinput typecheckbox name"bs"gt Secure mail ltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

86
Exercise 4 Radio Button
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • Username ltinput typetext name"un"gt ltBRgt
  • Password ltinput typepassword name"pw"gt ltBRgt
  • ltinput typecheckbox name"bs"gt Secure mail ltBRgt
  • ltinput typeradio name"sx"gt Female ltinput
    typeradio name"sx"gt MaleltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

87
Exercise 5 Colspan and Align
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • lttablegt
  • lttrgt
  • lttdgtUsernamelt/tdgt lttdgtltinput typetext
    name"un"gt lt/tdgt
  • lt/trgt
  • lttrgt
  • lttdgtPasswordlt/tdgt lttdgtltinput typepassword
    name"pw"gt lt/tdgt
  • lt/trgt
  • lttrgt
  • lttd colspan2 align"center"gtltinput typesubmit
    name"submit" value" OK "gtlt/tdgt
  • lt/trgt
  • lt/formgt
  • lt/bodygt

88
Exercise 6 General Form
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • lttablegt
  • lttrgt lttdgtUsernamelt/tdgt lttdgtltinput typetext
    name"un"gt lt/tdgt lt/trgt
  • lttrgt lttdgtPasswordlt/tdgt lttdgtltinput typepassword
    name"pw"gt lt/tdgt lt/trgt
  • lttrgt lttdgtGenderlt/tdgt lttdgtltinput typeradio
    name"sx"gtF ltinput typeradio name"sx"gtMlt/tdgt
    lt/trgt
  • lttrgt
  • lttdgtHobbylt/tdgt lttdgtltinput typecheckbox
    name"h1"gtMusic ltinput typecheckbox
    name"h2"gtStamp ltinput typecheckbox
    name"h3"gtHiking ltinput typecheckbox
    name"h4"gtSoccerlt/tdgt
  • lt/trgt
  • lttrgt lttd colspan2 align"center"gtltinput
    typesubmit name"submit" value" OK "gtlt/tdgt
    lt/trgt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

89
JSP Response object
90
response Object
  • Content Setting
  • setContentType()
  • getCharacterEncoding()
  • addCookie(cookie)
  • containsHeader(name)
  • setHeader(String name, String value)
  • setIntHeader(String name, int value)
  • setDateHeader(String name, Date value)
  • addHeader(String name, String value)
  • addIntHeader(String name, int value)
  • addDateHeader(String name, Date value)
  • Response Setting
  • setStatus(code)
  • sendError(status, msg)
  • sendRedirect(url)
  • URL Encoding
  • encodeRedirectURL(url)
  • encodeURL(name)

91
Redirect
  • login.html
  • login.jsp
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHODPOST ACTION"login.jsp"gt
  • Username ltINPUT TYPETEXT NAME"UN"gt ltBRgt
  • Password ltINPUT TYPEPASSWORD NAME"PW"gt ltBRgt
  • ltINPUT TYPESUBMIT VALUE"Login"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String username request.getParameter("UN")
  • String password request.getParameter("PW")
  • if (username.equals("a") password.equals("b"))
  • out.println("Login success!ltBRgt")
  • else
  • response.sendRedirect("login.html")
  • gt
  • lt/BODYgt
  • lt/HTMLgt

92
Digressionif statement
93
if statement
  • A control statement for division
  • Syntax
  • if (expression)
  • statement(s)1
  • else
  • statement(s)2
  • Control and execution
  • If evaluation result of the "expression" is true,
    then "statement(s)1" is executed.
  • Otherwise, "statement(s)2" is executed.
  • e.g.
  • if (n gt 0)
  • a 1
  • else
  • a 0
  • Controls in programming
  • Sequence
  • Division
  • Repetition

94
if statement Java1
  • Example
  • String response "I love you."
  • if (response.contains("love"))
  • System.out.println(response)
  • System.out.println("Comment You are
    lucky.")
  • else
  • System.out.println(response)
  • In an object-oriented programming language like
    Java, we can apply a command like "contains" to a
    String variable like "response".
  • Almost commands in Java return some values
    "contains" returns "true" or "false".

95
if statement
  • Exercise
  • Make a Java program such that
  • Set a string variable response as "I am fine.".
  • Print out the value of the response
  • If the response contains a string "fine", then
    add a comment "Comment Good!".
  • Project name
  • Java1X1

96
Exercise 1 Teenager
  • Write a JSP program
  • Assume an age.
  • If the age is between 13 and 19, display
    "Teenager".
  • Otherwise, display "Not teenager".

97
Exercise 2 Gender
  • Write a JSP program
  • Assume a gender.
  • If the gender is "M", display "Male".
  • Otherwise, display "Female".

98
Extension of "if" Statements
  • Extended Syntax
  • if (expression)
  • statement
  • else if (expression)
  • statement
  • else
  • statement
  • (e.g.)
  • if (x lt 0)
  • i -1
  • else if (x lt 100)
  • i 0
  • else
  • i 1

99
Exercise 3 Teenager
  • Write a JSP program
  • Assume an age.
  • If the age is less than 13, display "Child".
  • If the age is between 13 and 19, display
    "Teenager".
  • Otherwise, display "Not teenager".

100
Exercise 4 Gender
  • Write a JSP program
  • Assume a gender.
  • If the gender is "M", display "Male".
  • If the gender is "F", display "Female".
  • Otherwise, display "Transgender".

101
End of Digression
  • "if" Statement of Java

102
Exercise 1 Check Box
103
Exercise 1 Check Box
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • Username ltinput typetext name"un"gt ltBRgt
  • Password ltinput typepassword name"pw"gt ltBRgt
  • ltinput typecheckbox name"bs"gt Secure mail ltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

104
Exercise 2 Radio Button
105
Exercise 2 Radio Button
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • Username ltinput typetext name"un"gt ltBRgt
  • Password ltinput typepassword name"pw"gt ltBRgt
  • ltinput typecheckbox name"bs"gt Secure mail ltBRgt
  • ltinput typeradio name"sx"gt Female ltinput
    typeradio name"sx"gt MaleltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

106
Exercise 3 General Form
107
Exercise 3 General Form
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • lttablegt
  • lttrgt lttdgtUsernamelt/tdgt lttdgtltinput typetext
    name"un"gt lt/tdgt lt/trgt
  • lttrgt lttdgtPasswordlt/tdgt lttdgtltinput typepassword
    name"pw"gt lt/tdgt lt/trgt
  • lttrgt lttdgtGenderlt/tdgt lttdgtltinput typeradio
    name"sx"gtF ltinput typeradio name"sx"gtMlt/tdgt
    lt/trgt
  • lttrgt
  • lttdgtHobbylt/tdgt lttdgtltinput typecheckbox
    name"h1"gtMusic ltinput typecheckbox
    name"h2"gtStamp ltinput typecheckbox
    name"h3"gtHiking ltinput typecheckbox
    name"h4"gtSoccerlt/tdgt
  • lt/trgt
  • lttrgt lttd colspan2 align"center"gtltinput
    typesubmit name"submit" value" OK "gtlt/tdgt
    lt/trgt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

108
Exercise 4 Edit Box for Number
109
Exercise 4 Edit Box for Number
  • lthtmlgt
  • ltbodygt
  • lth1gtWeb Programminglt/h1gt
  • ltform methodpost action"a.jsp"gt
  • Age ltinput typetext name"age"gt ltBRgt
  • ltinput typesubmit name"submit" value" OK "gt
  • lt/formgt
  • lt/bodygt
  • lt/htmlgt

110
Exercise 4 Edit Box for Number
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String stringAge request.getParameter("age")
  • int age ______________?________________
  • if (age lt 13)
  • out.println("Child")
  • else if (age lt 20)
  • out.println("Teenager")
  • else
  • out.println("Not Teenager")
  • gt
  • lt/BODYgt
  • lt/HTMLgt

111
JSP Out object
112
out Object
  • print()
  • println()
  • isAutoFlush()
  • getBufferSize()
  • getRemaining()
  • clearBuffer()
  • clear()
  • newLine()
  • flush()
  • close()

113
Login
  • login.html
  • login.jsp
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHODPOST ACTION"login.jsp"gt
  • Username ltINPUT TYPETEXT NAME"UN"gt ltBRgt
  • Password ltINPUT TYPEPASSWORD NAME"PW"gt ltBRgt
  • ltINPUT TYPESUBMIT VALUE"Login"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String username request.getParameter("UN")
  • String password request.getParameter("PW")
  • out.println(username)
  • out.println(password)
  • gt
  • lt/BODYgt
  • lt/HTMLgt

114
Login
  • login.html
  • login.jsp
  • ltHTMLgt
  • ltBODYgt
  • ltFORM METHODPOST ACTION"login.jsp"gt
  • Username ltINPUT TYPETEXT NAME"UN"gt ltBRgt
  • Password ltINPUT TYPEPASSWORD NAME"PW"gt ltBRgt
  • ltINPUT TYPESUBMIT VALUE"Login"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String username request.getParameter("UN")
  • String password request.getParameter("PW")
  • gt
  • lt username gt
  • lt/ password gt
  • lt/BODYgt
  • lt/HTMLgt

115
Teenager
  • out.println()
  • expression
  • ltHTMLgt
  • ltBODYgt
  • lt
  • int age 20
  • if (a lt 13)
  • out.println("Child")
  • else if (a lt 20)
  • out.println("Teenager")
  • else
  • out.println("Not Teenager")
  • gt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • int age 20
  • if (a lt 13)gt
  • lt "Child"gt
  • ltelse if (a lt 20) gt
  • lt "Teenager"gt
  • ltelsegt
  • lt "Not Teenager"gt
  • lt/BODYgt
  • lt/HTMLgt

116
Digressionfor statement
117
for statement
  • A control statement for repetition
  • Syntax
  • for (expression1 expression2 expression3)
  • statement(s)
  • Control and execution
  • The "expression1" is evaluated.
  • If evaluation result of the "expression2" is
    true, then "statement(s)" is executed.
  • After the execution, the "expression3" is
    evaluated.
  • Go to the evaluation of the "expression2".
  • Otherwise, the whole "for" statement is finished.
  • e.g.
  • for (i 0 i lt 100 i)
  • System.out.println(i)
  • Controls in programming
  • Sequence
  • Division
  • Repetition

118
for statement Java3
  • Example
  • import java.io.
  • try
  • BufferedReader reader new
    BufferedReader(new InputStreamReader(System.in))
  • int i
  • String response ""
  • for (i 0 response.contains("Quit")
    false i)
  • System.out.print("Inputgt ")
  • response reader.readLine()
  • System.out.println(i " " response)
  • catch (Exception e)
  • System.err.println(e)
  • Note "contains" returns "true" or "false".

119
for statement
  • Exercise
  • Make a Java program such that
  • Input a string from user and print it out as in
    Java3.
  • If the string contains "Quit", then stop.
  • If the string contains a string "fine", then add
    a comment "Comment Good!".
  • Project name
  • Java3X1

120
Exercise 1 Display
  • Write a JSP program
  • Assume a max number n.
  • Display all numbers between 1 and n.

121
Exercise 2 Summation
  • Write a JSP program
  • Assume a max number n.
  • Calculate summation of all numbers between 1 and
    n.

122
End of Digression
  • "for" Statement of Java

123
JSP Session object
124
Session
  • A session is a semi-permanent interactive
    information exchange, also known as a dialogue, a
    conversation or a meeting, between two or more
    communicating devices, or between a computer and
    user.
  • A session is set up or established at a certain
    point in time, and torn down at a later point in
    time.
  • A session is typically, but not always, stateful,
    meaning that at least one of the communicating
    parts need to save information about the session
    history in order to be able to communicate.
  • A session in Web is established between a Web
    browser and a Web server, and the server needs to
    save information about the session history.
  • (e.g.) Login session

125
Session HTTP
  • Is HTTP stateful?
  • Does HTTP support a session?
  • Does HTTP have a mechanism to save information
    about the session history.
  • Basically, no!
  • HTTP is not stateful but stateless.
  • However, HTTP provides headers for "Cookies" such
    as Set-Cookie and Cookie which can be used to
    support a session.
  • The implicit object "session" in JSP provides a
    set of commands for a JSP program to save
    information about the session history.
  • This session object is based on the Set-Cookie
    and the Cookie headers of HTTP.

126
session Object
  • getAttribute(name)
  • getId()
  • getCreationTime()
  • getLastAccessTime()
  • getMaxInactiveInterval()
  • setAttribute(name, object)
  • setMaxInactiveInterval(second)
  • isNew()
  • invalidate()

127
Exercise 1 My Name
  • Write a HTML program (a.html)
  • Input users name
  • Write a JSP program (a.jsp)
  • Memorize the users name
  • Write another JSP program (b.jsp)
  • Retrieve and output the users name

128
Exercise 1 My Name
  • ltHTMLgt
  • ltBODYgt
  • ltFORM methodpost action"a.jsp"gt
  • Nameltinput typetext name"n"gtltbrgt
  • ltinput typesubmit value"OK"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

129
Exercise 1 My Name
  • a.jsp
  • b.jsp
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String name request.getParameter("n")
  • session.setAttribute("name", name)
  • out.println("You can enter ltA HREF'b.jsp'gtour
    sitelt/AgtltBRgt")
  • gt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String n (String)session.getAttribute("name")
  • out.println("Your name is ")
  • out.println(n)
  • out.println(". Right?")
  • gt
  • lt/BODYgt
  • lt/HTMLgt

130
Exercise 2 My Hobby
  • Write a HTML program (a.html)
  • Input users hobbies by using check boxes
  • Write a JSP program (a.jsp)
  • Memorize the users hobbies
  • Write another JSP program (b.jsp)
  • Retrieve and output the users hobbies

131
Exercise 2 My Hobby
132
Exercise SSN
  • Write a HTML program (a.html)
  • Input users social security number
  • Write a JSP program (a.jsp)
  • Memorize the users SSN
  • Write another JSP program (b.jsp)
  • Retrieve and output the users SSN

133
Exercise 3 Login
  • login.jsp
  • main.jsp
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String username request.getParameter("UN")
  • String password request.getParameter("PW")
  • if (username.equals("a") password.equals("b"))
  • session.setAttribute("Username", username)
  • out.println("Login success!ltBRgt")
  • out.println("You can enter ltA HREF'main.jsp'gtour
    sitelt/AgtltBRgt")
  • else
  • out.println("Login failed!ltBRgt")
  • out.println("You can NOT enter ltA
    HREF'main.jsp'gtour sitelt/AgtltBRgt")
  • gt
  • lt/BODYgt
  • lt/HTMLgt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String username (String)session.getAttribute("Us
    ername")
  • if (username ! null)
  • out.println("Welcome!ltBRgt")
  • out.println("You can see everything in our
    site.ltBRgt")
  • else
  • out.println("You are not allowed here.ltBRgt")
  • gt
  • lt/BODYgt
  • lt/HTMLgt

134
Session
  • ltHTMLgt
  • ltBODYgt
  • lt
  • session.invalidate()
  • out.println("ltA HREF'main.jsp'gtMainlt/Agt")
  • gt
  • lt/BODYgt
  • lt/HTMLgt

135
Exercise 3 Logout
  • Write a JSP program (logout.jsp)
  • Invalidate users session
  • Redirect to "login.html"

136
HW
  • Build a login-based Web site, which has
  • Login
  • At least 3 Web pages which are allowed only for
    authorized user
  • Logout

137
DigressionJava Database Connectivity (JDBC)
138
Java Database Connectivity
  • Definitions
  • JDBC is a standard Java interface for connecting
    from Java to relational databases.
  • JDBC is an API for the Java programming language
    that defines how a client may access a relational
    database.
  • It provides methods for querying and updating
    data in a database.
  • The JDBC standard was defined by Sun
    Microsystems, allowing individual providers to
    implement and extend the standard with their own
    JDBC drivers.

139
Steps in JDBC Programming
  • Create a database
  • Create a table
  • Set up connection between JSP (or Java) and the
    database
  • e.g. ODBC
  • JDBC Programming in JSP

140
JDBC Create a database (MS Access)
141
JDBC Create a database (MS Access)
142
JDBC Create a database (MS Access)
143
Exercise 1 Create a database
  • Create a database file named "mydb.mdb"

144
Exercise 2 Create a database
  • Create a database file named "shop.mdb"

145
JDBC Create a table (MS Access)
146
JDBC Create a table (MS Access)
147
JDBC Create a table (MS Access)
148
JDBC Create a table (MS Access)
149
Exercise 1 Create a table
  • Create a table named "customer" in database
    "mydb.mdb", of which schema are

Field Type Size
cname Text 20
username Text 10
password Text 20
address Text 50
150
Exercise 2 Create a table
  • Create a table named "product" in database
    "shop.mdb", of which schema are

Field Type Size
PID Text 10
pname Text 20
color Integer
price Integer
size Double
151
JDBC Set up ODBC connection (MS Access)
152
JDBC Set up ODBC connection (MS Access)
153
JDBC Set up ODBC connection (MS Access)
154
JDBC Set up ODBC connection (MS Access)
155
JDBC Set up ODBC connection (MS Access)
156
JDBC Set up ODBC connection (MS Access)
157
JDBC Set up ODBC connection (MS Access)
158
JDBC Set up ODBC connection (MS Access)
159
Exercise 1 Set up a connection
  • Set up an ODBC connection between a MS-Access
    file "mydb.mdb" and a DSN "mydsn".

160
Exercise 2 Set up a connection
  • Set up an ODBC connection between a MS-Access
    file "shop.mdb" and a DSN "shopdsn".

161
JDBC JDBC Programming in JSP (ODBC)
  • Syntax
  • lt_at_ page import"java.sql." gt
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • Connection conn DriverManager.getConnection
    ("jdbcodbcdsn", "user", "password")
  • Statement stmt conn.createStatement()
  • stmt.execute("") // SQL statement here
  • ResultSet rs stmt.getResultSet()
  • rs.next()
  • s rs.getString()
  • rs.close()
  • stmt.close()
  • conn.close()

Black words reserved words Violet words
what you must change Red words what you may
change
162
JDBC Example (1) (ODBC) JDBC1
  • lt_at_ page import"java.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String DB_URL "jdbcodbcydsn"
  • String DB_USER ""
  • String DB_PASSWORD ""
  • Connection conn
  • Statement stmt
  • ResultSet rs
  • String query "SELECT FROM customer"
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()

163
JDBC Example (2) (ODBC) JDBC1
  • stmt.execute(query)
  • rs stmt.getResultSet()
  • while (rs.next())
  • out.println(rs.getString("f_name") " / ")
  • out.println(rs.getString("f_username") " /
    ")
  • out.println(rs.getString("f_password") " /
    ")
  • out.println(rs.getString("f_address") " /
    ")
  • out.println("ltBRgt")
  • rs.close()
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

164
JDBC Example (1) (Oracle) JDBC1
  • lt_at_ page import"java.sql." gt
  • lt_at_ page import"oracle.jdbc." gt
  • lt_at_ page import"oracle.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String DB_URL "jdbcoraclethin_at_134.53.
    40.61521SBA"
  • String DB_USER "songyu"
  • String DB_PASSWORD "songyu"
  • Connection conn
  • Statement stmt
  • ResultSet rs
  • String query "SELECT FROM customer"
  • DriverManager.registerDriver(new
    oracle.jdbc.OracleDriver())
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()

165
JDBC Example (2) (Oracle) JDBC1
  • stmt.execute(query)
  • rs stmt.getResultSet()
  • for ( rs.next())
  • out.println(rs.getString("f_name")
    " / ")
  • out.println(rs.getString("f_username")
    " / ")
  • out.println(rs.getString("f_password")
    " / ")
  • out.println(rs.getString("f_address")
    " / ")
  • out.println("ltBRgt")
  • rs.close()
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

166
Exercise 1 JDBC Programming JDBCX1
  • Write a JSP program which displays all data in
    "customer" table of "mydb.mdb" database.

167
Exercise 2 JDBC Programming and Table JDBCX2
  • Write a JSP program which displays all data in
    "customer" table of "mydb.mdb" database by using
    TABLE tag of HTML.

168
Exercise 3 JDBC Programming JDBCX3
  • Write a JSP program which displays all data in
    "product" table of "shop.mdb" database.

169
Exercise 4 JDBC Programming and Table JDBCX4
  • Write a JSP program which displays all data in
    "product" table of "shop.mdb" database by using
    TABLE tag of HTML.

170
DigressionStructured Query Language (SQL)
171
INSERT
172
SQL INSERT
  • INSERT INTO customer
  • (f_username, f_password, f_name, f_address)
  • VALUES
  • ('yusong', '1234', 'Yong Uk Song', '234, Maji,
    Wonju')
  • INSERT INTO emp
  • (empno, ename, job, sal, comm, deptno)
  • VALUES
  • (7890, 'JINKS', 'CLERK', 1.2E3, NULL, 40)

173
SQL INSERT Simple Sample (1) SQL1S
  • lt_at_ page import"java.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String DB_URL "jdbcodbcydsn"
  • String DB_USER ""
  • String DB_PASSWORD ""
  • Connection conn
  • Statement stmt
  • String query "INSERT INTO customer (f_name,
    f_username, f_password, f_address) VALUES ('Yong
    Uk Song', 'yusong', '1234', '234, Maji, Wonju')"

174
SQL INSERT Simple Sample (2) SQL1S
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()
  • stmt.execute(query)
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

175
SQL INSERT Advanced Sample (1) SQL1A
  • ltHTMLgt
  • ltBODYgt
  • ltFORM method"post" action"insert.jsp"gt
  • Nameltinput typetext namenamegtltbrgt
  • Userameltinput typetext nameusernamegtltbrgt
  • Passwordltinput typetext namepasswordgtltbrgt
  • Addressltinput typetext nameaddressgtltbrgt
  • ltinput typesubmit value"Add"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

176
SQL INSERT Advanced Sample (2) SQL1A
  • lt_at_ page import"java.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String name request.getParameter("name")
  • String username request.getParameter("username"
    )
  • String password request.getParameter("password"
    )
  • String address request.getParameter("address")
  • String query "INSERT INTO customer (f_name,
    f_username, f_password, f_address) VALUES ('"
    name "', '" username "', '" password
    "', '" address "')"

177
SQL INSERT Advanced Sample (3) SQL1A
  • String DB_URL "jdbcodbcydsn"
  • String DB_USER ""
  • String DB_PASSWORD ""
  • Connection conn
  • Statement stmt
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()
  • stmt.execute(query)
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

178
SQL INSERT Simple vs. Advanced
  • "INSERT INTO customer (f_name, f_username,
    f_password, f_address) VALUES ('Yong Uk Song',
    'yusong', '1234', '234, Maji, Wonju')"
  • "INSERT INTO customer (f_name, f_username,
    f_password, f_address) VALUES (' " name " ',
    ' " username " ', ' " password " ', ' "
    address " ')"

179
Exercise 1 INSERT SQL1X1
  • Write a JSP program which adds a data into the
    "product" table of "shop.mdb" database.

180
Exercise 2 INSERT SQL1X2
  • Write an HTML file and a JSP program which inputs
    a data from a user and adds the data into the
    "product" table of "shop.mdb" database.

181
UPDATE
182
SQL UPDATE
  • UPDATE customer
  • SET f_name 'Hong', f_address 'Oxford'
  • WHERE f_username 'yusong'
  • UPDATE emp
  • SET comm NULL
  • WHERE job 'TRAINEE'

183
SQL UPDATE Simple Sample (1) SQL2S
  • lt_at_ page import"java.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String DB_URL "jdbcodbcydsn"
  • String DB_USER ""
  • String DB_PASSWORD ""
  • Connection conn
  • Statement stmt
  • String query "UPDATE customer SET
    address'Wonju' WHERE username'yusong' "

184
SQL UPDATE Simple Sample (2) SQL2S
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()
  • stmt.execute(query)
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

185
SQL UPDATE Advanced Sample (1) SQL2A
  • ltHTMLgt
  • ltBODYgt
  • ltFORM method"post" action"update.jsp"gt
  • Nameltinput typetext namenamegtltbrgt
  • Userameltinput typetext nameusernamegtltbrgt
  • Passwordltinput typetext namepasswordgtltbrgt
  • Addressltinput typetext nameaddressgtltbrgt
  • ltinput typesubmit value"Update"gt
  • lt/FORMgt
  • lt/BODYgt
  • lt/HTMLgt

186
SQL UPDATE Advanced Sample (2) SQL2A
  • lt_at_ page import"java.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String name request.getParameter("name")
  • String username request.getParameter("username"
    )
  • String password request.getParameter("password"
    )
  • String address request.getParameter("address")
  • String query "UPDATE customer SET f_address'"
    address "' WHERE f_username'" username
    "'"

187
SQL UPDATE Advanced Sample (3) SQL2A
  • String DB_URL "jdbcodbcydsn"
  • String DB_USER ""
  • String DB_PASSWORD ""
  • Connection conn
  • Statement stmt
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()
  • stmt.execute(query)
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

188
SQL UPDATE Simple vs. Advanced
  • "UPDATE customer SET f_address'Wonju' WHERE
    f_username'yusong' "
  • "UPDATE customer SET f_address' " address "
    ' WHERE f_username' " username " ' "

189
Exercise 1 UPDATE SQL2X1
  • Write a JSP program which updates a data in the
    "product" table of "shop. mdb" database.

190
Exercise 2 UPDATE SQL2X2
  • Write an HTML file and a JSP program which inputs
    a data from a user and updates the data in the
    "product" table of "shop.mdb" database.

191
DELETE
192
SQL DELETE
  • DELETE FROM customer
  • WHERE f_username 'yusong'
  • DELETE FROM emp
  • WHERE job 'SALESMAN'
  • AND comm lt 100

193
SELECT
194
SQL SELECT (1)
  • SELECT f_name, f_address
  • FROM customer
  • WHERE f_username 'yusong'
  • SELECT
  • FROM tblasp
  • ORDER BY name
  • WHERE age lt 30

195
SQL SELECT (2)
  • SELECT
  • FROM emp
  • WHERE deptno 40
  • SELECT ename, job, sal, deptno
  • FROM emp
  • WHERE NOT (job 'SALESMAN' AND deptno 30)
  • ORDER BY deptno ASC, sal DESC

196
SQL SELECT Simple Sample (1) SQL4S
  • lt_at_ page import"java.sql." gt
  • ltHTMLgt
  • ltBODYgt
  • lt
  • String DB_URL "jdbcodbcydsn"
  • String DB_USER ""
  • String DB_PASSWORD ""
  • Connection conn
  • Statement stmt
  • ResultSet rs
  • String query "SELECT password FROM customer
    WHERE username'yusong'"
  • Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")
  • conn DriverManager.getConnection(DB_URL,
    DB_USER, DB_PASSWORD)
  • stmt conn.createStatement()

197
SQL SELECT Simple Sample (2) SQL4S
  • stmt.execute(query)
  • rs stmt.getResultSet()
  • if (!rs.next())
  • out.println("Unknown user")
  • else if (rs.getString("f_password").equals("1234"
    ))
  • out.println("Login success")
  • else
  • out.println("Login failure")
  • stmt.close()
  • conn.close()
  • gt
  • lt/BODYgt
  • lt/HTMLgt

198
SQL Result Set (1)

yusong 1234 Yong Song Wonju, Korea
wkim abcd Woo Kim Seoul, Korea
spark qwer Sang Park Suwon, Korea
slee asdf Soo Lee Ohio, U.S.A

jhong zxcv June Hong Seoul, Kora

rs.getString("f_username") ? Error rs.getString("f
_password") ? Error rs.getString("f_name") ?
Error rs.getString("f_address") ? Error
Firstly,
199
SQL Result Set (2)

yusong 1234 Yong Song Wonju, Korea
wkim abcd Woo Kim Seoul, Korea
spark qwer Sang Park Suwon, Korea
slee asdf Soo Lee Ohio, U.S.A

jhong zxcv June Hong Seoul, Kora

rs.getString("f_username") ? "yusong" rs.getString
("f_password") ? "1234" rs.getString("f_name") ?
"Yong Song" rs.getString("f_address") ? "Wonju,
Korea"
rs.next( ) ? true
200
SQL Result Set (3)

yusong 1234 Yong Song Wonju, Korea
wkim abcd Woo Kim Seoul, Korea
spark qwer Sang Park Suwon, Korea
slee asdf Soo Lee Ohio, U.S.A

jhong zxcv June Hong Seoul, Kora

rs.getString("f_username") ? "wkim" rs.getString("
f_password") ? "abcd" rs.getString("f_name") ?
"Woo Kim" rs.getString("f_address") ? "Seoul,
Korea"
rs.next( ) ? true
201
SQL Result Set (4)

yusong 1234 Yong Song Wonju, Korea
wkim abcd Woo Kim Seoul, Korea
spark qwer Sang Park Suwon, Korea
slee asdf Soo Lee Ohio, U.S.A

jhong zxcv June Hong Seoul, Kora

rs.getString("f_username") ? "spark" rs.getString(
"f_password") ? "qwer" rs.getString("f_name") ?
"Sang Park" rs.getString("f_address") ? "Suwon,
Korea"
rs.next( ) ? true
202
SQL Result Set (5)

yusong 1234 Yong Song Wonju, Korea
wkim abcd Woo Kim Seoul, Korea
spark qwer Sang Park Suwon, Korea
slee asdf Soo Lee Ohio, U.S.A

jhong zxcv June Hong Seoul, Kora

rs.getString("f_username") ? "slee" rs.getString("
f_password") ? "as
Write a Comment
User Comments (0)
About PowerShow.com