Servlets - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Servlets

Description:

typically information from an HTML form. to send the server information so it can search ... String URL = 'jdbc:odbc:GuestBook'; 13) 14) public void init ... – PowerPoint PPT presentation

Number of Views:240
Avg rating:3.0/5.0
Slides: 31
Provided by: mattme
Category:

less

Transcript and Presenter's Notes

Title: Servlets


1
Chapter 19
  • Servlets

2
Servlets
Can you read this
  • Highest level view of networking in Java
  • Uses client/server request-response model of
    communication
  • Extends the functionality of a server
  • Most common form today - World Wide Web servers

3
Servlet Technology
  • Servlets are analog on the server side to applets
    on the client side
  • Servlets in Ch. 19 - demonstrate communication
    between clients and servers by way of the HTTP
    protocol of the World Wide Web

4
The Servlet API
  • All servlets must implement the Servlet interface
  • The methods of interface Servlet are invoked
    automatically by the server
  • Ch.19 examples all extend class HttpServlet
  • Key method is service which recieves
    ServletRequest and ServletResponse objects

5
HttpServlet Class
  • Web-based servlets typically extend class
    HttpServlet
  • Two most common HTTP request types are GET and
    POST

6
GET and POST
  • GET - retrieves information from the server
  • typically an HTML document or an image
  • POST - sends data to the server
  • typically information from an HTML form
  • to send the server information so it can search
  • to send authentication information to the server

7
doGet and doPost
  • Respond to GET and POST requests from a client
  • Called by Http-Servlet classs service method

8
HttpServletRequest Interface
  • Every call to doGet or doPost receives an object
    that implements interface HttpServletRequest
  • The Web server that executes the servlet creates
    an object and passes it to the servlets service
    method
  • The service method then passes it to doGet or
    doPost

9
HttpServletResponse Interface
  • Every call to doGet or doPost for an HttpServlet
    receives an object that implements interface
    HttpServletResponse
  • The Web server creates an object and passes it to
    the servlets service method
  • The servlets service method then passes it to
    doGet or doPost
  • This object contains the response to the client

10
Downloading the Java Servlet Development Kit
  • Before you can program with servlets you must
    download the JSDK for free from Sun Microsystems
    at
  • http//java.sun.com/products/servlet/index.html
  • near bottom of page

11
Handling Http GET Requests Example 19.5 - Imports

1) // Fig. 19.5 HTTPGetServlet.java 2) //
Creating and sending a page to the client 3)
import javax.servlet. 4) import
javax.servlet.http. 5) import java.io.
12
Example 19.5Superclass HttpServlet
7) public class HTTPGetServlet extends
HttpServlet 8) public void doGet(
HttpServletRequest request, 9)
HttpServletResponse response ) 10) throws
ServletException, IOException 11) 12)
PrintWriter output 13) 14) response.setContentT
ype( "text/html" ) // content type 15) output
response.getWriter() // get writer
13
Example 19.5Create HTML document
17) // create and send HTML page to client 18)
StringBuffer buf new StringBuffer() 19)
buf.append( "ltHTMLgtltHEADgtltTITLEgt\n" ) 20)
buf.append( "A Simple Servlet Example\n" ) 21)
buf.append( "lt/TITLEgtlt/HEADgtltBODYgt\n" ) 22)
buf.append( "ltH1gtWelcome to Servlets!lt/H1gt\n" )
23) buf.append( "lt/BODYgtlt/HTMLgt" ) 24)
output.println( buf.toString() ) 25)
output.close() // close PrintWriter stream
14
Example 19.5 HTML
1) lt!-- Fig. 19.6 HTTPGetServlet.html --gt 2)
ltHTMLgt 3) ltHEADgt 4) ltTITLEgt 5)
Servlet HTTP GET Example 6)
lt/TITLEgt 7) lt/HEADgt 8) ltBODYgt 9)
ltFORM 10) ACTION"http//localhost8080
/servlet/HTTPGetServlet" 11)
METHOD"GET"gt 12) ltPgtClick the button
to have the servlet send 13) an HTML
documentlt/Pgt 14) ltINPUT TYPE"submit"
VALUE"Get HTML Document"gt 15) lt/FORMgt
16) lt/BODYgt 17) lt/HTMLgt
15
Example 19.5 Output
  • Produces SUBMIT button in form
  • Form performs ACTION
  • ACTION indicates that the METHOD is GET
  • The service method responds with doGet

16
Handling HTTP Post Requests
  • An HTTP POST request is often used to post data
    from an HTML form to a server-side form handler
    that processes the data
  • Example 19.7 - Survey responses are sent as an
    HTTP POST request to the servlet

17
Session Tracking
  • Many Web sites provide custom Web pages and/or
    functionality on a client-by-client basis
  • Example - at Yahoo! - Users can customize how the
    Yahoo! site appears in the future
  • Session tracking is also used in shopping carts
    on the Web and as marketing tools

18
Cookies
  • Cookies customize Web pages by storing
    information on the users computer for retrieval
    later in the same or future browsing sessions
  • Example - Storing a users shopping preferences
    for a Web site
  • Text Example 19.9 uses cookies to store
    preferences and makes recommendations according
    to those preferences

19
Session Tracking with HttpSession
  • An alternative to cookies is to track a session
    with JSDKs interfaces and classes from package
    javax.servlet.http
  • Example 19.13 takes a modified 19.9 and uses
    objects to implement interface HttpSession

20
Multitier ApplicationsUsing JDBC from a Servlet
  • Servlets can communicate with databases by way of
    JDBC
  • In multitier achitectures, Web servers are
    increasingly used in the middle tier
  • provide business logic that manipulates data
  • and communicates with client Web browsers
  • Servlets, through JDBC, can interact with popular
    database systems

21
Multitier ApplicationsUsing JDBC from a Servlet
  • Examples 19.16 and 19.17 in the text demonstrate
    a three-tier distributed application that
    displays the user interface in a browser using
    HTML
  • Middle tier - Java servlet
  • handles requests from client
  • provides access to the third tier
  • Third tier- Microsoft Access Database
  • accesses via JDBC

22
Example 19.16Guest Book Imports, Classes
3) import java.io. 4) import javax.servlet.
5) import javax.servlet.http. 6) import
java.util. 7) import java.sql. 8) 9)
public class GuestBookServlet extends HttpServlet
10) private Statement statement null 11)
private Connection connection null 12)
private String URL "jdbcodbcGuestBook" 13)
14) public void init( ServletConfig config )
15) throws ServletException
23
Example 19.16 Guest BookPOST Input Strings
34) String email, firstName, lastName, company,
35) snailmailList, cppList, javaList, vbList,
36) iwwwList 37) 38) email
req.getParameter( "Email" ) 39) firstName
req.getParameter( "FirstName" ) 40) lastName
req.getParameter( "LastName" ) 41) company
req.getParameter( "Company" ) 42) snailmailList
req.getParameter( "mail" ) 43) cppList
req.getParameter( "c_cpp" ) 44) javaList
req.getParameter( "java" ) 45) vbList
req.getParameter( "vb" ) 46) iwwwList
req.getParameter( "iwww" )
24
Example 19.16 Guest BookInsert Fields into
Database
65) boolean success insertIntoDB( 66) "'"
email "','" firstName "','" lastName
67) "','" company "',' ',' ',' ',' ',' ','"
68) ( snailmailList ! null ? "yes" "no" )
"','" 69) ( cppList ! null ? "yes" "no" )
"','" 70) ( javaList ! null ? "yes" "no" )
"','" 71) ( vbList ! null ? "yes" "no" )
"','" 72) ( iwwwList ! null ? "yes" "no"
) "'" )
25
Guest Book ExampleHTML
1) lt!-- Fig. 19.17 GuestBookForm.html --gt 2)
ltHTMLgt 3) ltHEADgt 4) ltTITLEgtDeitel Guest Book
Formlt/TITLEgt 5) lt/HEADgt 6) 7) ltBODYgt 8)
ltH1gtGuest Booklt/H1gt 9) ltFORM 10)
ACTIONhttp//localhost8080/servlet/GuestBookServ
let 11) METHODPOSTgtltPREgt 12) Email address
ltINPUT TYPEtext NAMEEmailgt 13) First Name
ltINPUT TYPEtext NAMEFirstNamegt
26
Guest Book ExampleHTML
14) Last name ltINPUT TYPEtext NAMELastNamegt
15) Company ltINPUT TYPEtext NAMECompanygt 16)
17) fields are required 18) lt/PREgt 19) 20)
ltPgtSelect mailing lists from which you want 21)
to receive informationltBRgt 22) ltINPUT
TYPECHECKBOX NAMEmail VALUEmailgt 23) Snail
MailltBRgt 24) ltINPUT TYPECHECKBOX NAMEc_cpp
VALUEc_cppgt 25) ltIgtC How to Program C How
to Programlt/IgtltBRgt 26) ltINPUT TYPECHECKBOX
NAMEjava VALUEjavagt
27
Guest Book ExampleHTML
27) ltIgtJava How to Programlt/IgtltBRgt 28) ltINPUT
TYPECHECKBOX NAMEvb VALUEvbgt 29) ltIgtVisual
Basic How to Programlt/IgtltBRgt 30) ltINPUT
TYPECHECKBOX NAMEiwww VALUEiwwwgt 31)
ltIgtInternet and World Wide Web How to
Programlt/IgtltBRgt 32) lt/Pgt 33) ltINPUT TYPESUBMIT
Value"Submit"gt 34) lt/FORMgt 35) lt/BODYgt 36)
lt/HTMLgt
28
Guest Book ExampleOutput
  • Displays guest book interface which prompts user
    to enter
  • E-mail, name, company
  • Selection of mailing lists
  • Submit the form
  • Upon submission displays notice
  • Thank you Name for registering.

29
Electronic Commerce
  • New revolution beginning for electronic Commercie
  • Examples
  • Amazon.com, barnesandnoble.com, borders.com for
    electronic sales
  • Business-to-business transactions booming

30
How Servlets Can HelpElectronic Commerce
  • Shipment tracking
  • Invoice tracking
  • Small business into to e-commerce
  • Client-server technology
Write a Comment
User Comments (0)
About PowerShow.com