0 index
1 Client-Server programming
2 Classic 3 tier model
3 Client-side options
4 Uses of client-side coding
5 Server-side options
6 Standard Servers and ports
7 Connecting to a server
8 The SMTP protocol 'by hand'
9 Example: emailer
10 'Thin Client' solutions
11 Advantages of server-side work
12 The HTML side of the equation
13 HTML input elements
14 HTTP Submision
15 A sample GET request
16 GET and POST requests
17 URL Encoding
18 CGI - The Common Gateway Interface
19 CGI advantages
20 CGI disadvantages
21 Enter servlets
22 The request object
23 The response object
24 The servlet lifecycle
25 init()
26 service()
27 doGet()
28 doPost()
29 destroy()
30 Example: Password protection
31 servlet advantages
32 servlet disadvantages
33 Hello world servlet
34 Hello user servlet
35 Password servlet
36 Database servlet
37 Java Server Pages
38 Java Beans
39 JSP Demos

outline
created using slideshow.cgi by Andy Harris















IUPUI Computer Science: java2/Client-Server Programming
1. Client-Server programming
  • Historical precedent
  • Rationale
  • 3-tier models



































IUPUI Computer Science: java2/Client-Server Programming
2. Classic 3 tier model
  • Client
  • Server
  • Back-end service (database)



































IUPUI Computer Science: java2/Client-Server Programming
3. Client-side options
  • Custom client applications (FTP client)
  • Applets
  • Client side scripting languages (javascript)



































IUPUI Computer Science: java2/Client-Server Programming
4. Uses of client-side coding
  • Local processing
  • less drain on server
  • ideal for animation, interaction
  • form validation
  • client - dependant (but java and javascript improve this)



































IUPUI Computer Science: java2/Client-Server Programming
5. Server-side options
  • CGI - Common Gateway Interface
  • Java Servlets
  • Custom Server



































IUPUI Computer Science: java2/Client-Server Programming
6. Standard Servers and ports
  • Http 80
  • echo 7
  • FTP 21
  • telnet 23
  • smtp 25?
  • daytime 13



































IUPUI Computer Science: java2/Client-Server Programming
7. Connecting to a server
  • Echo
  • Date
  • SMTP
  • HTTP
  • telnet klingon 80
  • GET /index.html /HTTP1.0



































IUPUI Computer Science: java2/Client-Server Programming
8. The SMTP protocol 'by hand'

  • HELO ds9.cs.iupui.edu
    250 klingon.cs.iupui.edu Hello ds9 [134.68.140.211], pleased to meet you
    MAIL from: aharris@cs.iupui.edu
    250 aharris@cs.iupui.edu... Sender ok
    RCPT to: aharris@cs.iupui.edu
    250 aharris@cs.iupui.edu... Recipient ok
    DATA
    354 Enter mail, end with "." on a line by itself
    subject: THis is a bogus piece of mail
    .
    250 OAA04502 Message accepted for delivery
    quit



































IUPUI Computer Science: java2/Client-Server Programming
9. Example: emailer



































IUPUI Computer Science: java2/Client-Server Programming
10. 'Thin Client' solutions
  • Use standard HTML
  • HTTP calls for communication
  • Do most of the work on server side
  • Usually does NOT use applets



































IUPUI Computer Science: java2/Client-Server Programming
11. Advantages of server-side work
  • No platform dependance problems
  • client platform largely irrelevant
  • few security restrictions
  • access to local processes and resources



































IUPUI Computer Science: java2/Client-Server Programming
12. The HTML side of the equation
  • Form element
  • action = url of executable on server
  • method = GET or POST



































IUPUI Computer Science: java2/Client-Server Programming
13. HTML input elements
  • Textfield
  • Textarea
  • radio button
  • checkbox
  • selection object
  • Submit button
  • Hidden field



































IUPUI Computer Science: java2/Client-Server Programming
14. HTTP Submision
  • Pass array of Name/Value pairs
  • based on input elements of form
  • to action url
  • using method encoding



































IUPUI Computer Science: java2/Client-Server Programming
15. A sample GET request
  • note parameters sent as part of header



































IUPUI Computer Science: java2/Client-Server Programming
16. GET and POST requests
  • Get encodes in URL
  • Post encodes internally
  • Get has more limited length
  • Post is more robust
  • Get is more flexible



































IUPUI Computer Science: java2/Client-Server Programming
17. URL Encoding
  • HTTP is essentially a text-based protocol
  • Spaces and punctuation cause problems
  • form content automatically converted
  • must be decoded by server



































IUPUI Computer Science: java2/Client-Server Programming
18. CGI - The Common Gateway Interface
  • Grab input from environment variables
  • Send output to standard out
  • Use any language the server can read
  • de facto standard: perl
  • C and C++ also common choices



































IUPUI Computer Science: java2/Client-Server Programming
19. CGI advantages
  • Great flexibility in language choice
  • Supported by most servers
  • Handy libraries in popular languages
  • Freely available utilities written
  • Currently most popular choice



































IUPUI Computer Science: java2/Client-Server Programming
20. CGI disadvantages
  • Requires permission of server administrator
  • Programs go in specified directories
  • No built - in security constraints
  • Not all languages will be platform independant
  • Each call results in new process - could flood server
  • Most popular cgi language (perl) interpreted: quite slow
  • Based on HTTP, which is stateless.



































IUPUI Computer Science: java2/Client-Server Programming
21. Enter servlets
  • Designed to support HTML like CGI
  • Use servlet API
  • Based on request and response objects
  • override methods to respond to requests from form



































IUPUI Computer Science: java2/Client-Server Programming
22. The request object
  • Encapsulates a request FROM client
  • (usually an HTML form sent to this url)
  • getParameter method - returns back a parameter from form
  • getParameterValues - used for multivalue parameters
  • other methods to return header info, etc.



































IUPUI Computer Science: java2/Client-Server Programming
23. The response object
  • Encapsulates a response BACK TO client
  • (usually an HTML page - could be another form)
  • getWriter method returns a PrintWriter
  • write HTML out to this object
  • other methods for adding cookies, etc



































IUPUI Computer Science: java2/Client-Server Programming
24. The servlet lifecycle
  • init
  • service
  • do methods
  • destroy



































IUPUI Computer Science: java2/Client-Server Programming
25. init()
  • used for initialization
  • connection to services such as databases
  • pass servletConfig back to super



































IUPUI Computer Science: java2/Client-Server Programming
26. service()
  • general purpose request handler
  • receives a response and a request as parameters
  • throws IOexception and servletException
  • calls one of the specialty handlers (doGet, doPost, etc)
  • generally should not be over-written



































IUPUI Computer Science: java2/Client-Server Programming
27. doGet()
  • called by service method when a Get operation is requested
  • passes response and request from service
  • throws same exceptions
  • HTML requests with no forms are GETs
  • Often used to handle an initial request,
  • generate a form for later POST handling on response object
  • Also used when we want to send data through URLs with GET encoding



































IUPUI Computer Science: java2/Client-Server Programming
28. doPost()
  • called by service method when a Post operation is requested
  • passes response and request from service
  • throws same exceptions
  • generally used to analyse a form
  • Heavy use of getParameter, getParameterValues methods of request object
  • examine HTML form input elements, act accordingly
  • write out a web page using response object



































IUPUI Computer Science: java2/Client-Server Programming
29. destroy()
  • used to do garbage collection
  • especially useful when you want the servlet to close itself down
  • servlets are persistant, must be shut down on explicitly



































IUPUI Computer Science: java2/Client-Server Programming
30. Example: Password protection



































IUPUI Computer Science: java2/Client-Server Programming
31. servlet advantages
  • speed
  • power of java
  • JDBC
  • security



































IUPUI Computer Science: java2/Client-Server Programming
32. servlet disadvantages
  • Not supported by all servers
  • harder to debug than interpreted langs
  • moving target



































IUPUI Computer Science: java2/Client-Server Programming
33. Hello world servlet



































IUPUI Computer Science: java2/Client-Server Programming
34. Hello user servlet



































IUPUI Computer Science: java2/Client-Server Programming
35. Password servlet



































IUPUI Computer Science: java2/Client-Server Programming
36. Database servlet



































IUPUI Computer Science: java2/Client-Server Programming
37. Java Server Pages
  • similar to ASP
  • HTML with embedded java
  • converted to Servlet



































IUPUI Computer Science: java2/Client-Server Programming
38. Java Beans
  • Class with get / set values
  • must implement Serializable interface
  • Most servlets already do both



































IUPUI Computer Science: java2/Client-Server Programming
39. JSP Demos



































outline

Client-Server programming

Classic 3 tier model

Client-side options

Uses of client-side coding

Server-side options

Standard Servers and ports

Connecting to a server

The SMTP protocol 'by hand'

Example: emailer

'Thin Client' solutions

Advantages of server-side work

The HTML side of the equation

HTML input elements

HTTP Submision

A sample GET request

GET and POST requests

URL Encoding

CGI - The Common Gateway Interface

CGI advantages

CGI disadvantages

Enter servlets

The request object

The response object

The servlet lifecycle

init()

service()

doGet()

doPost()

destroy()

Example: Password protection

servlet advantages

servlet disadvantages

Hello world servlet

Hello user servlet

Password servlet

Database servlet

Java Server Pages

Java Beans

JSP Demos