It contains two jsp files, namely login.jsp and index.jsp
The code is as follows:
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Login interface</title></head><body><form action="index.jsp" method="post">Username:<input type="text" name="name"/><input type="submit" value="submit"/></form></body></html>
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Welcome</title></head><body><%String name = request.getParameter("name");if(name != null && !name.trim().equals("")){Cookie cookie = new Cookie("name",name);cookie.setMaxAge(30); //Set the cookie validity period to 30sresponse.addCookie(cookie);}else{Cookie[] cookies = request.getCookies();if(cookies != null && cookies.length > 0){for(Cookie cookie:cookies){String cookieName = cookie.getName();if("name".equals(cookieName)){String val = cookie.getValue();name = val;}}}}if(name != null && !name.trim().equals("")){out.print("hello: " + name);}else{//Otherwise redirect to the login interface response.sendRedirect("login.jsp");}%></body></html>The above is the JavaWeb using cookies to simulate the automatic login function. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!