exo7: add cookies handling code.
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 11 Apr 2018 07:57:04 +0000 (09:57 +0200)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Wed, 11 Apr 2018 07:57:04 +0000 (09:57 +0200)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
ROOT/exo7/cookie-form.html [new file with mode: 0644]
ROOT/exo7/nextpage.jsp
ROOT/exo7/setcookie.jsp [new file with mode: 0644]
ROOT/exo7/viewcookie.jsp [new file with mode: 0644]

diff --git a/ROOT/exo7/cookie-form.html b/ROOT/exo7/cookie-form.html
new file mode 100644 (file)
index 0000000..10bbcaa
--- /dev/null
@@ -0,0 +1,13 @@
+<html>
+<body>
+
+<form action="setcookie.jsp">
+  First name:<br>
+  <input type="text" name="firstname"><br>
+  Last name:<br>
+  <input type="text" name="lastname"><br><br>
+  <input type="submit" value="Submit">
+</form>
+
+</body>
+</html>
index 37153a64a120fe0652bc917785449d675ea0d848..f54b811eac7ed3a6246f97201c95ea3f103639ca 100644 (file)
@@ -1,6 +1,9 @@
 <html>
     <body>
-        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")); %>
     </body>
 </html>
diff --git a/ROOT/exo7/setcookie.jsp b/ROOT/exo7/setcookie.jsp
new file mode 100644 (file)
index 0000000..43ffe8d
--- /dev/null
@@ -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);
+%>
+
+<html>
+    <body>
+        <a href="viewcookie.jsp">Continue</a>
+    </body>
+</html>
diff --git a/ROOT/exo7/viewcookie.jsp b/ROOT/exo7/viewcookie.jsp
new file mode 100644 (file)
index 0000000..2a705f6
--- /dev/null
@@ -0,0 +1,13 @@
+<% Cookie[] cookies = request.getCookies(); %>
+
+<html>
+    <body>
+        Bonjour <br>
+                <%
+                for(int i = 0; i < cookies.length; i++) {
+                    Cookie c = cookies[i];
+                    out.println(c.getName() + " = " + c.getValue() + " <br>");
+                }
+                %>
+    </body>
+</html>