Servlets 4 Lec 30 Web Design and Development
Looking Back… Response Redirection Sending a standard redirect Sending a redirect to an error page Request Dispatching Forward Include
Include Diagram
Example Code Request Dispatching: include
Forward Diagram
Example Code Request Dispatching: forward
HTTPServletRequest methods
More HttpServletRequest methods setAttribute (String, Object) / getAttribute(String) Stores an attribute in this request as key-value pair form Attributes are set/reset between requests Often used in conjunction with RequestDispatcher
More HttpServletRequest methods cont. getMethod() Returns the name of HTTP method (get & post etc) getRequestURL() Returns the part of this request’s URL Query string is not included getProtocol() Returns the name & version of the protocol used
More HttpServletRequest methods cont. getHeaderNames() Returns an enumeration of all the header names this request contains getHeader(String headername) Returns the value of the specified request header as String Returns null, if no header with the specified name
HTTPServletResponse methods
More HttpServletResponse methods setContentType(String) Servlets almost always use this header MIME types setContentLength(int) Sets the Content-Length header. addCookie(Cookie) Adds a value to the Set-Cookie header. sendRedirect (String) Redirect client to the specified URL Can accept relatives URL
Session Tracking
HTTP is a stateless protocol Every request is considered independent of every other request Many web applications need to maintain a conversational state with the client A shopping cart is a classic example
Continuity Problem (User’s Point of View) Page 1 Page 2 Page 3 Page 4 Server State Added book to cart CC# = XXX Billing address Order submitted & logged Credit: Stanford’s java course
The illusion of Continuity User thinks that choices made on page 1 are remembered on page 3 However HTTP is Stateless Requests from same user do not necessarily come in adjacent requests
Continuity Problem (Server’s Point of View) Request 1 Request 2 Credit: Stanford’s java course