EBIZ 5535 Lecture 2 - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

EBIZ 5535 Lecture 2

Description:

... Performed by Manipulating the Status Line. Forwarding the user to ... Like setting the status codes, this must be done before any document content is sent. ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 26
Provided by: jeffreyv
Category:
Tags: ebiz | lecture | status

less

Transcript and Presenter's Notes

Title: EBIZ 5535 Lecture 2


1
EBIZ 5535Lecture 2
  • Form Data and Headers

2
Outline
  • Handling Form Data
  • Reading HTTP request headers
  • Setting HTTP status codes
  • Specifying HTTP response headers

3
Handling Form Data
  • URLs like http//host/path?userJeffVBoriginbwi
    destlax
  • The part after the ? is form data attached to a
    get request (or appearing on lines after the
    first in a post request
  • Unlike CGI, Java servlets handle the parsing of
    form data for you
  • You simply call the getParameter method of the
    HttpServletRequest, supplying the parameter name
    as an argument
  • String parm1 request.getParameter(user)

4
Getting Parameters
  • Parameter names are case sensitive
  • The getParameter method is called the same way
    whether a get or a post
  • An empty String is returned if the parameter
    exists but has no value
  • and null is returned if there was no such
    parameter

5
getParameterValues
  • getParameterValues can be used instead of
    getParameter
  • This returns an array of strings
  • It is sometimes useful to get a full list
  • Or use getParameterNames for this
  • returns an Enumeration, each entry of which can
    be cast to a String and used in a getParameter
    call

6
Example of Getting Parameters
  • import
  • public class ThreeParams extends HttpServlet
  • public void doGet(HttpServletRequest request,
    HttpServletResponse response)
  • throws ServletException, IOException
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • String title "Reading Three Request
    Parameters"
  • out.println(ServletUtilities.headWithTitle(ti
    tle) "ltBODYgt\n" "ltH1 ALIGNCENTERgt" title
    "lt/H1gt\n" "ltULgt\n" " ltLIgtparam1 "
    request.getParameter("param1") "\n" "
    ltLIgtparam2 " request.getParameter("param2")
    "\n" " ltLIgtparam3 " request.getParameter("par
    am3") "\n" "lt/ULgt\n" "lt/BODYgtlt/HTMLgt")

7
Can also have a post method
  • public void doPost
  • (HttpServletRequest request,
  • HttpServletResponse response)
  • throws ServletException, IOException
    doGet(request, response)

8
Listing All Form Data
  • PostForm.html and ShowParams.java example

9
Steps Typically Performed to Handle an HTTP
request
HTTP request (e.g., get)
Read request headers
Decide on a response
Set status code and response headers
Generate HTML response
10
Reading HTTP Request Headers
  • When an HTTP client (e.g. a browser) sends a
    request, it is required to supply a request line
  • Also sends headers, which are optional except for
    Content-Length with posts
  • A servlet can read these headers

11
Some Common Headers
  • Accept The MIME types the browser prefers
  • Accept-Charset The character set the browser
    expects.
  • Accept-Encoding The types of data encodings (such
    as gzip) the browser knows how to decode
  • Accept-Language The language the browser is
    expecting, in case the server has versions in
    more than one language

12
Common Headers contd
  • Authorization Authorization info, usually in
    response to a WWW-Authenticate header from the
    server.
  • Connection Use persistent connection?
  • Content-Length (for POST messages, how much data
    is attached)
  • Cookie (well talk about this later)
  • Host (host and port as listed in the original
    URL)
  • If-Modified-Since (only return documents newer
    than this, otherwise send a 304 "Not Modified"
    response)

13
Reading Request Headers in Servlets
  • Just call the getHeader method of the
    HttpServletRequest
  • returns a String if the header was supplied on
    this request, null otherwise
  • The getCookies method returns the contents of the
    Cookie header, parsed and stored in an array of
    Cookie objects.
  • The getAuthType and getRemoteUser methods break
    the Authorization header into its components
  • The getDateHeader and getIntHeader methods read
    the specified header and then convert them to
    Date and int values, respectively

14
Example
  • public class ShowRequestHeaders extends
    HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse response)
  • throws ServletException, IOException
  • response.setContentType("text/html")
  • PrintWriter out response.getWriter()
  • String title "Servlet Example Showing
    Request Headers"
  • out.println(ServletUtilities.headWithTitle(tit
    le)
  • "ltBODY BGCOLOR\"FDF5E6\"gt\n"
  • "ltH1 ALIGNCENTERgt" title
    "lt/H1gt\n"
  • "ltBgtRequest Method lt/Bgt"
  • request.getMethod() "ltBRgt\n"
  • "ltBgtRequest URI lt/Bgt"
  • request.getRequestURI()
    "ltBRgt\n"
  • "ltBgtRequest Protocol lt/Bgt"
  • request.getProtocol()
    "ltBRgtltBRgt\n"
  • "ltTABLE BORDER1 ALIGNCENTERgt\n"
  • "ltTR BGCOLOR\"FFAD00\"gt\n"
  • "ltTHgtHeader NameltTHgtHeader
    Value")

15
  • Enumeration headerNames request.getHeaderNames()
  • while(headerNames.hasMoreElements())
  • String headerName (String)headerNames.next
    Element()
  • out.println("ltTRgtltTDgt" headerName)
  • out.println(" ltTDgt"
  • request.getHeader(headerName))
  • out.println("lt/TABLEgt\nlt/BODYgtlt/HTMLgt")
  • public void doPost(HttpServletRequest request,
  • HttpServletResponse
    response)
  • throws ServletException, IOException
  • doGet(request, response)

16
HTTP Status Codes
  • When a Web server responds to a request from a
    browser or other Web client, the response
    typically begins with a status line
  • HTTP/1.1 200 OK Content-Type text/plain
  • Hello World

17
The Status Line
  • The status line consists of
  • the HTTP version
  • an integer that is interpreted as a status code
  • and a very short status message
  • In most cases, all of the headers are optional
    except for Content-Type, which specifies the MIME
    type of the document that follows.
  • Servlets can perform a variety of tasks by
    manipulating the status line and the response
    headers

18
Setting HTTP Status Codes
  • The server sets
  • The HTTP version
  • The message (depending on the status code)
  • A Servlet need only set the status code
  • setStatus method of HttpServletResponse
  • Status codes (passed to setStatus) are int
    constants named from the status message
  • SC_OK
  • SC_NOT_FOUND

19
Caution
  • Always set status code before sending any content
  • Status code defaults to SC_OK

20
Some Tasks Performed by Manipulating the Status
Line
  • Forwarding the user to other sites
  • Indicating that the attached document is
  • an image
  • Adobe Acrobat file
  • or (most commonly) HTML file
  • Telling the user that a password is required to
    access the document

21
Specifying HTTP Response Headers
  • A response from a Web server consists of
  • a status line
  • one or more response headers
  • a blank line
  • the document
  • Specifying headers can be used to indicate
    errors, but can also be useful under normal
    circumstances
  • can be used to specify cookies
  • to supply the modification date (for caching)
  • to instruct the browser to reload the page after
    a designated interval
  • to say how long the file is so that persistent
    HTTP connections can be used

22
Specifying Headers contd
  • The most general way to specify headers is by the
    setHeader method of HttpServletResponse, which
    takes two strings
  • the header name and the header value
  • Like setting the status codes, this must be done
    before any document content is sent.
  • Special methods of HttpServletResponse
  • sendError
  • sendRedirect

23
Some Other Interesting Response Headers
24
Example GZipped Web Pages
  • import
  • public class EncodedPage extends HttpServlet
  • public void doGet(HttpServletRequest request,
  • HttpServletResponse response)
  • throws ServletException, IOException
  • response.setContentType("text/html")
  • String encodings request.getHeader("Accept-E
    ncoding")
  • String encodeFlag request.getParameter("enco
    ding")
  • PrintWriter out
  • String title
  • if ((encodings ! null)
  • (encodings.indexOf("gzip") ! -1)
  • !"none".equals(encodeFlag))
  • title "Page Encoded with GZip"
  • OutputStream out1 response.getOutputStream
    ()
  • out new PrintWriter(new
    GZIPOutputStream(out1), false)
  • response.setHeader("Content-Encoding",
    "gzip")
  • else
  • title "Unencoded Page"

25
  • out.println(ServletUtilities.headWithTitle(title)
  • "ltBODY BGCOLOR\"FDF5E6\"gt\n"
  • "ltH1 ALIGNCENTERgt" title
    "lt/H1gt\n")
  • String line "Blah, blah, blah, blah, blah.
    "
  • "Yadda, yadda, yadda, yadda."
  • for(int i0 ilt10000 i)
  • out.println(line)
  • out.println("lt/BODYgtlt/HTMLgt")
  • out.close()
Write a Comment
User Comments (0)
About PowerShow.com