BAEL-1567 use optional (#3849)

* BAEL-1412 add java 8 spring data features

* BAEL-21 new HTTP API overview

* BAEL-21 fix executor

* BAEL-1432 add custom gradle task

* BAEL-1567 add samples of cookie and session in serlvet

* BAEL-1567 use stream api

* BAEL-1567 fix optional
This commit is contained in:
abialas 2018-03-18 21:02:16 +01:00 committed by maibin
parent e6024bfdd0
commit b2eaf0af58
3 changed files with 17 additions and 14 deletions

View File

@ -3,6 +3,7 @@ package com.baeldung.servlets;
import javax.servlet.http.Cookie; import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.util.Arrays; import java.util.Arrays;
import java.util.Optional;
/** /**
* Created by adam. * Created by adam.
@ -26,14 +27,13 @@ public class CookieReader {
* Reads cookie by key from request. * Reads cookie by key from request.
* *
* @param key the key of a cookie * @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<String> readCookie(String key) {
return Arrays.stream(request.getCookies()) return Arrays.stream(request.getCookies())
.filter(c -> key.equals(c.getName())) .filter(c -> key.equals(c.getName()))
.map(Cookie::getValue) .map(Cookie::getValue)
.findFirst() .findFirst();
.orElse(null);
} }
} }

View File

@ -5,6 +5,7 @@ import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*; import javax.servlet.http.*;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
/** /**
* Created by adam. * Created by adam.
@ -15,12 +16,12 @@ public class LoginServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CookieReader cookieReader = new CookieReader(request); CookieReader cookieReader = new CookieReader(request);
String uiColor = cookieReader.readCookie("uiColor"); Optional<String> uiColor = cookieReader.readCookie("uiColor");
String userName = cookieReader.readCookie("userName"); Optional<String> 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"); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);
} else { } else {

View File

@ -8,6 +8,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
/** /**
* Created by adam. * Created by adam.
@ -18,15 +19,16 @@ public class WelcomeServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
CookieReader cookieReader = new CookieReader(request); CookieReader cookieReader = new CookieReader(request);
String uiColor = cookieReader.readCookie("uiColor"); Optional<String> uiColor = cookieReader.readCookie("uiColor");
String userName = cookieReader.readCookie("userName"); Optional<String> userName = cookieReader.readCookie("userName");
if (userName == null || userName.isEmpty()) { if (!userName.isPresent()) {
response.sendRedirect("/login"); response.sendRedirect("/login");
} else { } else {
request.setAttribute("uiColor", uiColor != null ? uiColor : "blue"); request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue");
request.setAttribute("userName", userName); request.setAttribute("userName", userName.get());
request.setAttribute("sessionAttribute", request.getSession().getAttribute("sampleKey")); request.setAttribute("sessionAttribute", request.getSession()
.getAttribute("sampleKey"));
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp"); RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp");
dispatcher.forward(request, response); dispatcher.forward(request, response);