Skip to content

Commit b2eaf0a

Browse files
abialasmaibin
authored andcommitted
BAEL-1567 use optional (eugenp#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
1 parent e6024bf commit b2eaf0a

3 files changed

Lines changed: 17 additions & 14 deletions

File tree

‎javax-servlets/src/main/java/com/baeldung/servlets/CookieReader.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import javax.servlet.http.Cookie;
44
import javax.servlet.http.HttpServletRequest;
55
import java.util.Arrays;
6+
import java.util.Optional;
67

78
/**
89
* Created by adam.
@@ -26,14 +27,13 @@ public CookieReader(HttpServletRequest request) {
2627
* Reads cookie by key from request.
2728
*
2829
* @param key the key of a cookie
29-
* @return returns cookie value (or null if cookie with given key does not exist)
30+
* @return returns cookie value
3031
*/
31-
public String readCookie(String key) {
32+
public Optional<String> readCookie(String key) {
3233
return Arrays.stream(request.getCookies())
3334
.filter(c -> key.equals(c.getName()))
3435
.map(Cookie::getValue)
35-
.findFirst()
36-
.orElse(null);
36+
.findFirst();
3737
}
3838

3939
}

‎javax-servlets/src/main/java/com/baeldung/servlets/LoginServlet.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javax.servlet.annotation.WebServlet;
66
import javax.servlet.http.*;
77
import java.io.IOException;
8+
import java.util.Optional;
89

910
/**
1011
* Created by adam.
@@ -15,12 +16,12 @@ public class LoginServlet extends HttpServlet {
1516
@Override
1617
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
1718
CookieReader cookieReader = new CookieReader(request);
18-
String uiColor = cookieReader.readCookie("uiColor");
19-
String userName = cookieReader.readCookie("userName");
19+
Optional<String> uiColor = cookieReader.readCookie("uiColor");
20+
Optional<String> userName = cookieReader.readCookie("userName");
2021

21-
request.setAttribute("uiColor", uiColor != null ? uiColor : "blue");
22+
request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue");
2223

23-
if (userName == null || userName.isEmpty()) {
24+
if (!userName.isPresent()) {
2425
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
2526
dispatcher.forward(request, response);
2627
} else {

‎javax-servlets/src/main/java/com/baeldung/servlets/WelcomeServlet.java‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import javax.servlet.http.HttpServletRequest;
99
import javax.servlet.http.HttpServletResponse;
1010
import java.io.IOException;
11+
import java.util.Optional;
1112

1213
/**
1314
* Created by adam.
@@ -18,15 +19,16 @@ public class WelcomeServlet extends HttpServlet {
1819
@Override
1920
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
2021
CookieReader cookieReader = new CookieReader(request);
21-
String uiColor = cookieReader.readCookie("uiColor");
22-
String userName = cookieReader.readCookie("userName");
22+
Optional<String> uiColor = cookieReader.readCookie("uiColor");
23+
Optional<String> userName = cookieReader.readCookie("userName");
2324

24-
if (userName == null || userName.isEmpty()) {
25+
if (!userName.isPresent()) {
2526
response.sendRedirect("/login");
2627
} else {
27-
request.setAttribute("uiColor", uiColor != null ? uiColor : "blue");
28-
request.setAttribute("userName", userName);
29-
request.setAttribute("sessionAttribute", request.getSession().getAttribute("sampleKey"));
28+
request.setAttribute("uiColor", uiColor.isPresent() ? uiColor.get() : "blue");
29+
request.setAttribute("userName", userName.get());
30+
request.setAttribute("sessionAttribute", request.getSession()
31+
.getAttribute("sampleKey"));
3032

3133
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp");
3234
dispatcher.forward(request, response);

0 commit comments

Comments
 (0)