1 / 13

HTTP Sessions

HTTP Sessions. http://flic.kr/p/8KTNbe. What are you going to learn about today?. How cookies work How to manage user sessions. http://flic.kr/p/8JpkTg. Recall: HTTP/Servlet interaction. Client A and Client B could be the same!.

lbrannon
Download Presentation

HTTP Sessions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HTTP Sessions http://flic.kr/p/8KTNbe

  2. What are you goingto learn about today? • How cookies work • How to manage user sessions http://flic.kr/p/8JpkTg

  3. Recall: HTTP/Servlet interaction Client A and Client B could be the same!

  4. Sometimes you’d like your web app to have a “conversation” with a userProblem: HTTP is stateless, so don’t know who sent a given requestAny idea how to solve this problem? This is what cookies were made for! http://flic.kr/p/9ksxQa

  5. How cookies work Session ID

  6. Good news! You don’t have to mess with cookies yourself—there’s HttpSession protected void doPost(HttpServletRequest request, HttpServletResponse response) { … HttpSession session = request.getSession(); … } public interface HttpSession { public void setAttribute(String name, Object value) {…} public Object getAttribute(String name) {…} public boolean isNew() {…} … }

  7. How sessions work: First request

  8. How sessions work: Second request, same session Same servlet Different thread Same session Same user

  9. How sessions work: Different user/session Same servlet Different thread Different session Different user

  10. Activity: Creating “Ping Pong”Servlets with Sessions • Create two servlets, PingServlet and PongServlet • Keep track of how many times each has been visited in a session • Each servlet displays the two counts and provides a link to the other servlet What happens if you disable cookies in your browser? Try it! http://flic.kr/p/5dfuqL

  11. Don’t worry: There’s a backup planin case cookies are disabled

  12. Unfortunately, you must “encode” all URLs (to yourwebapp) that you link to in your response HTML public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); out.println(“<html><body>”); out.println(“<a href=\”” + response.encodeURL(“/BeerTest.do”) + “\”>click me</a>”); out.println(“</body></html>”); }

  13. Summary • HttpSession • Cookies for session management • URL encoding as a backup http://flic.kr/p/YSY3X

More Related