44 lines
1.3 KiB
Plaintext
44 lines
1.3 KiB
Plaintext
== How to fix it in Java SE
|
|
|
|
=== Code examples
|
|
|
|
include::../../common/fix/code-rationale.adoc[]
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
Optional<Cookie> cookieOpt = Arrays.stream(request.getCookies())
|
|
.filter(c -> c.getName().equals("jsessionid"))
|
|
.findFirst();
|
|
|
|
if (!cookieOpt.isPresent()) {
|
|
String cookie = request.getParameter("cookie");
|
|
Cookie cookieObj = new Cookie("jsessionid", cookie);
|
|
response.addCookie(cookieObj);
|
|
}
|
|
|
|
response.sendRedirect("/welcome.jsp");
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
|
|
Optional<Cookie> cookieOpt = Arrays.stream(request.getCookies())
|
|
.filter(c -> c.getName().equals("jsessionid"))
|
|
.findFirst();
|
|
|
|
if (!cookieOpt.isPresent()) {
|
|
response.sendRedirect("/getCookie.jsp");
|
|
} else {
|
|
response.sendRedirect("/welcome.jsp");
|
|
}
|
|
}
|
|
----
|
|
|
|
include::../../common/fix/how-does-this-work.adoc[]
|