I'm a new bird learning Servlet. When I use cookie in Servlet, I found cookie can't be added after visiting web page. Here's my code:
import jakarta.servlet.ServletException;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LastAccessServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String lastAccessTime = null;
Cookie[] cookies = req.getCookies();
PrintWriter writer = resp.getWriter();
for (int i = 0; cookies != null && i < cookies.length; i){
if ("lastAccess".equals(cookies[i].getName())) {
lastAccessTime = cookies[i].getValue();
break;
}
}
if (lastAccessTime == null){
writer.println("Your first visit.");
} else {
writer.println("Last time" lastAccessTime);
}
String currentTime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());
Cookie cookie = new Cookie("lastAccess", currentTime);
cookie.setMaxAge(999999999);
cookie.setPath("/");
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
Here's my WebPage:
No matter how many times I refresh, reload or change browsers, the result is the same.
I check the cookies in my edge browser.
The cookie hasn't the value name lastAccess
I added, so I think the problem is in resp.addCookie(cookie);
.But don't know how to solve it.
CodePudding user response:
Your problem is not actually with the code/logic but data here. You are trying to set a cookie value which is more like a Date string looking like 2022-10-01 03:01:22
.
The cookie value here is illegal. As per the RFC6265, you are not allowed to have some of the special characters in the cookie. A more detailed description is available with this answer
You can solve it by changing may be the way you form the cookie value. For eg use the timestamp instead of date string. Like below.
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");
String lastAccessTime = null;
Cookie[] cookies = req.getCookies();
PrintWriter writer = resp.getWriter();
for (int i = 0; cookies != null && i < cookies.length; i){
if ("lastAccess".equals(cookies[i].getName())) {
lastAccessTime = cookies[i].getValue();
break;
}
}
if (lastAccessTime == null){
writer.println("Your first visit.");
} else {
writer.println("Last time" new Date(Long.parseLong(lastAccessTime)));
}
String currentTime = String.valueOf( new Date().getTime());
Cookie cookie = new Cookie("lastAccess", currentTime);
cookie.setMaxAge(999999999);
cookie.setPath("/");
resp.addCookie(cookie);
}