L09-DataSharing

L09-DataSharing - Session 9 Data Sharing Session 9 Data...

Info iconThis preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Session 9 – Data Sharing 10/1/2010 1 © Robert Kelly, 2001-2010 Session 9 Data Sharing & Cookies 1 Reading & Reference Reading Chapter 5, pages 185-204 Reference – http state management www.ietf.org/rfc/rfc2109.txt?number=2109 © Robert Kelly, 2001-2010 2
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Session 9 – Data Sharing 10/1/2010 2 © Robert Kelly, 2001-2010 Lecture Objectives Understand how the Web Container uses cookies to store server data so that it is available to separate servlet executions Know how to use server shared objects to store state information Understand the scope differences for ServletContext and Session objects © Robert Kelly, 2001-2010 3 Understand the ways in which a session object is implemented by the Web container Understand how the Web container uses threads to match user requests to servlets When Do You Need to Share Data? Among servlets cooperating on an application Among servlets cooperating to satisfy the requests from a single user (e.g., shopping cart) Usually on the same workstation and browser © Robert Kelly, 2001-2010 4
Background image of page 2
Session 9 – Data Sharing 10/1/2010 3 © Robert Kelly, 2001-2010 Servlet Data Sharing The Http protocol is stateless, so your servlet only responds to a single request Question: Where do you store data from your servlet when the data is needed for multiple requests? Answer: Anywhere you can Approaches: browser side and server side ServletContext B Browser side state data Server side state data © Robert Kelly, 2001-2010 5 Servlet HttpSession Browser (cookies and hidden form fields) HttpServletRequest Browser Side Storage request Data stored on the browser is included in the response object and returned to the servlet through the request objec Web Container response © Robert Kelly, 2001-2010 6 through the request object What data is transmitted through http? Form data set Cookies
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Session 9 – Data Sharing 10/1/2010 4 © Robert Kelly, 2001-2010 Data Sharing Example Implement a counter using: Hidden form field Cookies ServletContext Session © Robert Kelly, 2001-2010 7 The servlet stores a counter that is updated whenever a user sends another request Browser Storage: Hidden Form Fields Store the data in a form field generated by your servlet. The hidden form field is not displayed in the browser window © Robert Kelly, 2001-2010 8 <form action="http://localhost:8080/CodeCSE336/datasharing"> <input type = "hidden" name = "hiddenCounter" value = "1"> <input type="submit"> </form> The value of hiddenCounter is 1 the first time the servlet is invoked
Background image of page 4
Session 9 – Data Sharing 10/1/2010 5 © Robert Kelly, 2001-2010 Example – Hidden Form Field Code String sCounter = request.getParameter("hiddenCounter"); if (sCounter != null) Servlet Code Fragment hffCount = Integer.parseInt(sCounter) else hffCount = 0; hffCount++; out.println("<form action=\”http://localhost:8080/CodeCSE336/datasharing\”>"); out.println("<input type = \"hidden\" name = \"hiddenCounter\" value = \"" Counter is updated and added to html © Robert Kelly, 2001-2010 \ hiddenCounter\ value = \ + hffCount + "\">"); out.println("<input type=\"submit\">"); out.println("</form>"); out.println("<p>Hidden form field variable - "); out.println(hsCount + "</p>");
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 21

L09-DataSharing - Session 9 Data Sharing Session 9 Data...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online