Java Servlets

1 / 14
About This Presentation
Title:

Java Servlets

Description:

Contains information about request (GET or POST) and cookies stored on client machine ... HttpSession is an alternative to cookies. ... –

Number of Views:108
Avg rating:3.0/5.0
Slides: 15
Provided by: aalg
Category:
Tags: cookies | java | servlets

less

Transcript and Presenter's Notes

Title: Java Servlets


1
Java Servlets
  • By
  • Abdulmalik S. Al-Gahmi

2
Outline
  • Overview of Servlet Technology
  • The Servlet API
  • HttpServlet Class
  • HttpServletRequest Interface
  • HttpServletResponse Interface
  • Handling HTTP GET Requests
  • Handling HTTP POST Requests
  • Session Tracking
  • Hidden variables
  • Cookies
  • Session Tracking with HttpSession

3
Introduction
  • Client-server relationship
  • Client request action
  • Server performs action, responds to client
  • This view is foundation of servlets
  • Highest-level view of networking
  • Servlet extends functionality of server
  • Useful for database-intensive applications
  • Thin clients - little client-side support needed
  • Server controls database access
  • Logic code written once, on server

4
Overview of Servlet Technology
  • HyperText Transfer Protocol (HTTP) Basis for WWW
    and Uses URLs (Uniform/Universal Resource
    Locators) which
  • Locates data on internet
  • Represent files or directories
  • Can represent tasks like database lookups
  • Servlets
  • Analog to applets
  • Execute on server's machine, supported by most
    web servers
  • Demonstrate communication via HTTP protocol
  • Client sends HTTP request
  • Server receives request, servlets process it
  • Results returned (HTML document, images, binary
    data)

5
The Servlet API(1)
  • Servlet interface
  • Implemented by all servlets
  • Many methods invoked automatically by server
  • Similar to applets (paint, init, start, etc.)
  • abstract classes that implement Servlet
  • GenericServlet (javax.servlet)
  • HTTPServlet (javax.servlet.http)
  • Methods
  • void init( ServletConfig config )
  • Automatically called, argument provided

6
The Servlet API (2)
  • Methods
  • ServletConfig getServletConfig()
  • Returns reference to object, gives access to
    config info
  • void service ( ServletRequest request,
    ServletResponse response )
  • Key method in all servlets
  • Provide access to input and output streams
  • Read from and send to client
  • void destroy()
  • Cleanup method, called when servlet exiting

7
HttpServlet Class
  • HttpServlet
  • Base class for web-based servlets
  • Overrides method service
  • Request methods
  • GET - retrieve HTML documents or image
  • POST - send server data from HTML form
  • Methods doGet and doPost respond to GET and POST
  • Called by service
  • Receive HttpServletRequest and HttpServletResponse
    (return void) objects

8
HttpServletRequest Interface
  • HttpServletRequest interface
  • Object passed to doGet and doPost
  • Extends ServletRequest
  • Methods
  • String getParameter( String name )
  • Returns value of parameter name (part of GET or
    POST)
  • Enumeration getParameterNames()
  • Returns names of parameters (POST)
  • String getParameterValues( String name )
  • Returns array of strings containing values of a
    parameter
  • Cookie getCookies()
  • Returns array of Cookie objects, can be used to
    identify client

9
HttpServletResponse Interface
  • HttpServletResponse
  • Object passed to doGet and doPost
  • Extends ServletResponse
  • Methods
  • void addCookie( Cookie cookie )
  • Add Cookie to header of response to client
  • ServletOutputStream getOutputStream()
  • Gets byte-based output stream, send binary data
    to client
  • PrintWriter getWriter()
  • Gets character-based output stream, send text to
    client
  • void setContentType( String type )
  • Specify MIME type of the response
  • Helps display data

10
Handling HTTP GET Requests
  • HTTP GET requests
  • Usually gets content of specified URL
  • Usually HTML document (web page)
  • Method doGet
  • Responds to GET requests
  • Default action BAD_REQUEST error (file not
    found)
  • Override for custom GET processing
  • Arguments represent client request and server
    response!"

11
Handling HTTP POST Requests
  • HTTP POST
  • Used to post data to server-side form handler
    (i.e. surveys)
  • Both GET and POST can supply parameters
  • doPost
  • Responds to POST requests (default BAD_REQUEST)
  • Same arguments as doGet (client request, server
    response)

12
Session Tracking
  • Web sites
  • Many have custom web pages functionality
  • Custom home pages - http//my.yahoo.com/
  • Shopping carts
  • Marketing
  • HTTP protocol does not support persistent
    information
  • Cannot distinguish clients
  • Distinguishing clients
  • Hidden variables
  • Cookies
  • Session Tracking

13
Cookies
  • Cookies
  • Small files that store information on client's
    computer
  • Servlet can check previous cookies for
    information
  • Header
  • In every HTTP client-server interaction
  • Contains information about request (GET or POST)
    and cookies stored on client machine
  • Response header includes cookies servers wants to
    store
  • Age
  • Cookies have a lifespan
  • Can set maximum age
  • Cookies can expire and are deleted

14
Session Tracking with HttpSession
  • HttpSession is an alternative to cookies. It
    keeps the session data available until browsing
    ends
  • Methods
  • getSession( createNew )
  • Class HttpServletRequest
  • Returns client's previous HttpSession object
  • putvalue( name, value )
  • Adds a name/value pair to object
  • getValueNames()
  • Returns array of Strings with names
  • getValue( name )
  • Returns value of name as an Object
  • Cast to proper type
Write a Comment
User Comments (0)
About PowerShow.com