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 c2d875a69f..133e729c33 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java @@ -3,6 +3,7 @@ package com.baeldung.servlets; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; import java.util.Arrays; +import java.util.Optional; /** * Created by adam. @@ -26,14 +27,13 @@ public class CookieReader { * Reads cookie by key from request. * * @param key the key of a cookie - * @return returns cookie value (or null if cookie with given key does not exist) + * @return returns cookie value */ - public String readCookie(String key) { + public Optional readCookie(String key) { return Arrays.stream(request.getCookies()) .filter(c -> key.equals(c.getName())) .map(Cookie::getValue) - .findFirst() - .orElse(null); + .findFirst(); } } 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 deb0e05ec4..ee4e73c956 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java @@ -5,6 +5,7 @@ import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.*; import java.io.IOException; +import java.util.Optional; /** * Created by adam. @@ -15,12 +16,12 @@ public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CookieReader cookieReader = new CookieReader(request); - String uiColor = cookieReader.readCookie("uiColor"); - String userName = cookieReader.readCookie("userName"); + Optional uiColor = cookieReader.readCookie("uiColor"); + Optional userName = cookieReader.readCookie("userName"); - request.setAttribute("uiColor", uiColor != null ? uiColor : "blue"); + request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue"); - if (userName == null || userName.isEmpty()) { + if (!userName.isPresent()) { RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp"); dispatcher.forward(request, response); } else { 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 b35be2fc93..dd66cb458e 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java @@ -8,6 +8,7 @@ import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.util.Optional; /** * Created by adam. @@ -18,15 +19,16 @@ public class WelcomeServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { CookieReader cookieReader = new CookieReader(request); - String uiColor = cookieReader.readCookie("uiColor"); - String userName = cookieReader.readCookie("userName"); + Optional uiColor = cookieReader.readCookie("uiColor"); + Optional userName = cookieReader.readCookie("userName"); - if (userName == null || userName.isEmpty()) { + if (!userName.isPresent()) { response.sendRedirect("/login"); } else { - request.setAttribute("uiColor", uiColor != null ? uiColor : "blue"); - request.setAttribute("userName", userName); - request.setAttribute("sessionAttribute", request.getSession().getAttribute("sampleKey")); + request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue"); + request.setAttribute("userName", userName.get()); + request.setAttribute("sessionAttribute", request.getSession() + .getAttribute("sampleKey")); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp"); dispatcher.forward(request, response);