li tak sing comps311f. a web page that counts the number of times that you have visited the page....

Click here to load reader

Upload: earl-whitehead

Post on 13-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1

Li Tak Sing COMPS311F Slide 2 A web page that counts the number of times that you have visited the page. You can try the page at: http://plbpc001.ouhk.edu.hk/tma/Count You can get the source at: http://plbpc001.ouhk.edu.hk/~mt311f/2010-oct/Count.java Slide 3 Count public class Count extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String count = null; Cookie cookie[] = request.getCookies(); if (cookie != null) { for (Cookie c : cookie) { if (c.getName().equals("count")) { count = c.getValue(); } Slide 4 Count if (count == null) { out.println("You are new here."); count = "1"; } else { out.println("You have visited this site for " + count + " times."); count = Integer.toString(Integer.parseInt(count) + 1); } response.addCookie(new Cookie("count", count)); } finally { out.close(); } Slide 5 A page that allows that users to select the size of the fonts. You can try the page at: http://plbpc001.ouhk.edu.hk/tma/FontSize You can get the source at: http://plbpc001.ouhk.edu.hk/~mt311f/2010-oct/FontSize.java Slide 6 FontSize public class FontSize extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String size = null; if (request.getParameter("size") != null) { size = request.getParameter("size"); response.addCookie(new Cookie("size", size)); } Slide 7 FontSize if (size == null) { Cookie cookie[] = request.getCookies(); if (cookie != null) { for (Cookie c : cookie) { if (c.getName().equals("size")) { size = c.getValue(); } if (size==null) { size="3"; } Slide 8 FontSize out.println(" "); out.println("Font size: "); out.println(" 3 "); out.println(" 4 "); out.println(" 5 "); out.println(" "); out.println(" This text is just used to test the servlet. "); out.println(" Click this to reload the page "); out.println(" "); } finally { out.close(); } Slide 9 Other useful methods of Cookie public void setDomain(String pattern) Specifies the domain within which this cookie should be presented. By default, cookies are only returned to the server that sent them. This is the reason why cookie is dangerous. For example, when you visit site A and accept a cookie. Then when you visit site B and sent that cookie to site B. In this way, site B would know that you have visited site A. If that happens to many sites, then site B will now have your profile of internet surfing. Public String getDomain() Returns the domain name set for this cookie. Slide 10 Other useful methods of Cookie public void setPath(String pattern) Sets the path to which this cookie applies. If you don't specify a path, the cookie is returned for all URLs in the same directory as the current page as well as all subdirectories. This method can be used to specify something more general. For example, someCookie.setPath("/") specifies that all pages on the server should receive the cookie. Note that the path specified must include the current directory. Public String getPath() Get the path to which this cookie applies. Slide 11 Other useful methods of Cookie public void setMaxAge(int expiry) Sets the maximum age of the cookie in seconds. A positive value indicates that the cookie will expire after that many seconds have passed. Note that the value is the maximum age when the cookie will expire, not the cookie's current age. A negative value means that the cookie is not stored persistently and will be deleted when the Web browser exits. A zero value causes the cookie to be deleted. If you don't set this, the cookie will last only for the current session (i.e. until the user quits the browser), and will not be stored on disk.public int getMaxAge() Returns the maximum age of the cookie, specified in seconds, By default, -1 indicating the cookie will persist until browser shutdown. Slide 12 A cookie that expires in 10 seconds You can try the page at: http://plbpc001.ouhk.edu.hk/tma/Expire You can get the source at: http://plbpc001.ouhk.edu.hk/~mt311f/2010-oct/Expire.java Slide 13 A cookie that expires in 10 seconds public class Expire extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { if (request.getParameter("create") != null) { Cookie c = new Cookie("expire", "10"); c.setMaxAge(10); response.addCookie(c); } Cookie cookie[] = request.getCookies(); Slide 14 A cookie that expires in 10 seconds int fontsize = 3; if (cookie != null) { for (Cookie c : cookie) { if (c.getName().equals("expire")) { fontsize=10; } Slide 15 The page that changes font size out.println(" "); out.println(" This text is just used to test the servlet. "); out.println(" Click this to reload the page "); out.println(" "); } finally { out.close(); } Slide 37 Other useful methods of HttpSession public void setMaxInactiveInterval(int interval) Specifies the time, in seconds, between client requests before the servlet container will invalidate this session. A negative time indicates the session should never timeout. publit int getMaxInactiveInterval() Returns the maximum time interval, in seconds, that the servlet container will keep this session open between client accesses. After this interval, the servlet container will invalidate the session. The maximum time interval can be set with the setMaxInactiveInterval method. A negative time indicates the session should never timeout. Slide 38 A session that expires after 10 seconds of being inactive You can try the page at: http://plbpc001.ouhk.edu.hk/tma/SessionExpire You can get the source at: http://plbpc001.ouhk.edu.hk/~mt311f/2010-oct/SessionExpire.java Slide 39 ExpireSession public class SessionExpire extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session; if (request.getParameter("create")!=null) { session=request.getSession(); session.setMaxInactiveInterval(10); } else { session=request.getSession(false); } Slide 40 ExpireSession response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { int fontsize = 3; if (session!=null) { fontsize=10; } Slide 41