BAEL-1567 (#3824)
* 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
This commit is contained in:
parent
ffe7e39f51
commit
d9e4ec2d2d
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*
|
||||
* Class which simplifies reading cookies from request.
|
||||
*/
|
||||
public class CookieReader {
|
||||
|
||||
private HttpServletRequest request;
|
||||
|
||||
/**
|
||||
* The constructor.
|
||||
*
|
||||
* @param request request from which cookies will be read
|
||||
*/
|
||||
public CookieReader(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)
|
||||
*/
|
||||
public String readCookie(String key) {
|
||||
return Arrays.stream(request.getCookies())
|
||||
.filter(c -> key.equals(c.getName()))
|
||||
.map(Cookie::getValue)
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.*;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@WebServlet(name = "LoginServlet", urlPatterns = "/login")
|
||||
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");
|
||||
|
||||
request.setAttribute("uiColor", uiColor != null ? uiColor : "blue");
|
||||
|
||||
if (userName == null || userName.isEmpty()) {
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/login.jsp");
|
||||
dispatcher.forward(request, response);
|
||||
} else {
|
||||
response.sendRedirect("/welcome");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
HttpSession session = request.getSession();
|
||||
session.setAttribute("sampleKey", "Sample Value");
|
||||
|
||||
String uiColor = request.getParameter("color");
|
||||
String userName = request.getParameter("name");
|
||||
|
||||
Cookie uiColorCookie = new Cookie("uiColor", uiColor);
|
||||
response.addCookie(uiColorCookie);
|
||||
|
||||
Cookie userNameCookie = new Cookie("userName", userName);
|
||||
response.addCookie(userNameCookie);
|
||||
|
||||
response.sendRedirect("/welcome");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
package com.baeldung.servlets;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.Cookie;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by adam.
|
||||
*/
|
||||
@WebServlet(name = "WelcomeServlet", urlPatterns = "/welcome")
|
||||
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");
|
||||
|
||||
if (userName == null || userName.isEmpty()) {
|
||||
response.sendRedirect("/login");
|
||||
} else {
|
||||
request.setAttribute("uiColor", uiColor != null ? uiColor : "blue");
|
||||
request.setAttribute("userName", userName);
|
||||
request.setAttribute("sessionAttribute", request.getSession().getAttribute("sampleKey"));
|
||||
|
||||
RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/welcome.jsp");
|
||||
dispatcher.forward(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
Cookie userNameCookieRemove = new Cookie("userName", "");
|
||||
userNameCookieRemove.setMaxAge(0);
|
||||
response.addCookie(userNameCookieRemove);
|
||||
|
||||
response.sendRedirect("/login");
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<%@ page contentType="text/html;charset=UTF-8"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello New User:</title>
|
||||
</head>
|
||||
<body style="background: ${uiColor}">
|
||||
<form name="userForm" action="login" method="POST">
|
||||
<label>Your Name:</label>
|
||||
<input type="text" name="name"/>
|
||||
|
||||
<label>UI Color:</label>
|
||||
<input type="text" name="color"/>
|
||||
|
||||
<button type="submit">Confirm</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
|
@ -0,0 +1,13 @@
|
|||
<%@ page contentType="text/html;charset=UTF-8"%>
|
||||
<html>
|
||||
<head>
|
||||
<title>Hello Known User:</title>
|
||||
</head>
|
||||
<body style="background: ${uiColor}">
|
||||
<form action="welcome" method="POST">
|
||||
<label>Your name: ${userName}</label>
|
||||
<label>Session param: ${sessionAttribute}</label>
|
||||
<button type="submit">Logout</button>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in New Issue