From 27e8c3cf51769dd944bc3d49a5c5776bfcb8aa65 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Wed, 11 Apr 2018 09:57:04 +0200 Subject: [PATCH] exo7: add cookies handling code. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- ROOT/exo7/cookie-form.html | 13 +++++++++++++ ROOT/exo7/nextpage.jsp | 7 +++++-- ROOT/exo7/setcookie.jsp | 16 ++++++++++++++++ ROOT/exo7/viewcookie.jsp | 13 +++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 ROOT/exo7/cookie-form.html create mode 100644 ROOT/exo7/setcookie.jsp create mode 100644 ROOT/exo7/viewcookie.jsp diff --git a/ROOT/exo7/cookie-form.html b/ROOT/exo7/cookie-form.html new file mode 100644 index 0000000..10bbcaa --- /dev/null +++ b/ROOT/exo7/cookie-form.html @@ -0,0 +1,13 @@ + + + +
+ First name:
+
+ Last name:
+

+ +
+ + + diff --git a/ROOT/exo7/nextpage.jsp b/ROOT/exo7/nextpage.jsp index 37153a6..f54b811 100644 --- a/ROOT/exo7/nextpage.jsp +++ b/ROOT/exo7/nextpage.jsp @@ -1,6 +1,9 @@ - Bonjour <% out.print(session.getAttribute("firstname")); %> <% out.print(session.getAttribute("lastname")); %>, <% out.print(session.getAttribute("age")); %> years old - living at <% out.print(session.getAttribute("address")); %> working as <% out.print(session.getAttribute("work")); %> + Bonjour <% out.print(session.getAttribute("firstname")); %> + <% out.print(session.getAttribute("lastname")); %>, + <% out.print(session.getAttribute("age")); %> years old + living at <% out.print(session.getAttribute("address")); %> + working as <% out.print(session.getAttribute("work")); %> diff --git a/ROOT/exo7/setcookie.jsp b/ROOT/exo7/setcookie.jsp new file mode 100644 index 0000000..43ffe8d --- /dev/null +++ b/ROOT/exo7/setcookie.jsp @@ -0,0 +1,16 @@ +<%@ page import="java.io.*, java.util.*, javax.servlet.*" %> +<% +String firstname = request.getParameter("firstname"); +String lastname = request.getParameter("lastname"); +String username = firstname + lastname; +Date timestamp = new Date(); +Cookie cookie = new Cookie("username", username); +cookie.setMaxAge(60*60*24); //24 hours +response.addCookie(cookie); +%> + + + + Continue + + diff --git a/ROOT/exo7/viewcookie.jsp b/ROOT/exo7/viewcookie.jsp new file mode 100644 index 0000000..2a705f6 --- /dev/null +++ b/ROOT/exo7/viewcookie.jsp @@ -0,0 +1,13 @@ +<% Cookie[] cookies = request.getCookies(); %> + + + + Bonjour
+ <% + for(int i = 0; i < cookies.length; i++) { + Cookie c = cookies[i]; + out.println(c.getName() + " = " + c.getValue() + "
"); + } + %> + + -- 2.34.1