Faced such a problem. In the database, I recorded a class object Cookie like this:
public void addUser(String telephone , String name , Cookie cookie) { try { String query = "INSERT INTO users (idTelephone , Name , cookie) VALUES ('"+telephone+"','"+name+"', ?);"; System.out.println(query); prepare = connect.prepareStatement(query); prepare.setObject(1, cookie); prepare.executeUpdate(); prepare.close(); }catch(SQLException ex) { ex.printStackTrace(); } } I read this way:
public Cookie enter(String telephone , String name) { Cookie cookie = null; try { String query = "SELECT Cookie FROM users WHERE idTelephone ='"+telephone+"' "+" AND Name = '"+name+"';"; stat = connect.createStatement(); result = stat.executeQuery(query); result.next(); cookie = (Cookie) result.getObject("Cookie"); stat.close(); }catch(SQLException ex) { ex.printStackTrace(); } return cookie; } This is where I accept cookies:
ModelFiles model = new ModelFiles((Connection) getServletContext().getAttribute("Connection")); Cookie cookie = model.enter(telephone , name); if(cookie != null) { response.addCookie((Cookie)cookie); response.sendRedirect("Load.jsp"); } else { response.sendRedirect("FailAuthorization.html"); } } Knocks out such an error:
[B cannot be cast to javax.servlet.http.Cookie at Models.ModelFiles.enter (ModelFiles.java:145)
Cookie = (Cookie) result.getObject ("Cookie");
Tried to check on instofof before attempting to lead to Cookie
if(cookie instanceof Cookie){ system.out.println("Кастится") }else{ system.out.println("Не кастится"); Result:
It does not cast (that is, it is not given);
In the database I store the cookie in the column type Blob here:
In OBD OBJECT RECORDED! Just back I just can not pick up)
