When a user is authorized, cookies are created: There is a main page - index.jsp There is a home page, which get after authorization - home.jsp There is a Filter, which is tied to home.jsp. Authorization takes place in the filter (the class connects to the database) and if everything is successful, then cookies are created and then everything goes to home.jsp. In home.jsp at the very beginning there is a check for cookies and if they are, then the user remains on the page, if not, I do Redirect to index.jsp.
The essence of the problem is that after exiting the Filter there are no cookies. After it, there is a check in home.jsp and, accordingly, it redirects to index and there already cookies appear and it redirects to home. It’s the same if you do it all through Servlen
The question is how to treat this? I know the option to add cookies already on the home page itself.
Code to add to cookies:
Cookie cookie = new Cookie("login", Login); cookie.setMaxAge(CookiesTime); res.addCookie(cookie); Check for cookies in home.jsp
<% String login = ""; String hash = ""; Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { if (cookie.getName().equals("badAuth")) { System.err.println("home.jsp -> Found badAuth!"); response.sendRedirect("index"); return; } else { if (cookie.getName().equals("login")) { System.out.println("home.jsp -> Found login"); login = cookie.getValue(); } if (cookie.getName().equals("hash")) { System.out.println("home.jsp -> Found hash"); hash = cookie.getValue(); } } } dbConnect connect = new dbConnect(); if (!login.equals("") && !hash.equals("")) { if (connect.checkHash(login, hash)) { int roleNum = connect.checkUserRole(login, dbConnect.ROLE_USER); if(roleNum == 1 || roleNum == 2) { request.setAttribute("role", roleNum); ArrayList<Link> linkArr = connect.GetLinks(connect.getId(login)); request.setAttribute("linkArr", linkArr); } else { response.sendRedirect("index"); return; } } } else { response.sendRedirect("index"); return; } %> <!DOCTYPE html> html страница...
res.addCookie(cookie);- How do I understand the addition of cookies in response? Andcookies = request.getCookies();get a cookie from a request? Filter adds in response. jsp searches in the request. And does not find, because the request was without cookies, and the filter put it in response. Make it so that the filter alsores.addCookie(...); req.addCookie(...);it into the query (empty, everyone thinks that it was always there)res.addCookie(...); req.addCookie(...);res.addCookie(...); req.addCookie(...);- Sergey