From 94fc6997fe7d1fcc899b226ee8ace04824d92692 Mon Sep 17 00:00:00 2001 From: Grzegorz Piwowarek Date: Mon, 19 Mar 2018 20:02:28 +0100 Subject: [PATCH] Servlet refactor (#3851) --- .../src/main/java/com/baeldung/servlets/CookieReader.java | 2 +- .../src/main/java/com/baeldung/servlets/LoginServlet.java | 2 +- .../src/main/java/com/baeldung/servlets/WelcomeServlet.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java b/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java index 133e729c33..13241e36f7 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java @@ -33,7 +33,7 @@ public class CookieReader { return Arrays.stream(request.getCookies()) .filter(c -> key.equals(c.getName())) .map(Cookie::getValue) - .findFirst(); + .findAny(); } } diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java index ee4e73c956..f6e612a0d0 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java @@ -19,7 +19,7 @@ public class LoginServlet extends HttpServlet { Optional uiColor = cookieReader.readCookie("uiColor"); Optional userName = cookieReader.readCookie("userName"); - request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue"); + request.setAttribute("uiColor", uiColor.orElse("blue")); if (!userName.isPresent()) { RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp"); diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java index dd66cb458e..262ad3061a 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java @@ -25,7 +25,7 @@ public class WelcomeServlet extends HttpServlet { if (!userName.isPresent()) { response.sendRedirect("/login"); } else { - request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue"); + request.setAttribute("uiColor", uiColor.orElse("blue")); request.setAttribute("userName", userName.get()); request.setAttribute("sessionAttribute", request.getSession() .getAttribute("sampleKey"));