网站功能建设与栏目划分,公司网站怎么建,4c网络营销策略,电子工程师Cookie和Session出于安全考虑#xff0c;浏览器不让网页直接操作文件系统#xff0c;而Cookie就是一个折中的方案#xff0c;可以让网页暂存一些数据在本地#xff0c;不能存复杂的对象#xff0c;只能存字符串。Cookie是按照域名分类的#xff0c;这个很好理解。如何理解…Cookie和Session出于安全考虑浏览器不让网页直接操作文件系统而Cookie就是一个折中的方案可以让网页暂存一些数据在本地不能存复杂的对象只能存字符串。Cookie是按照域名分类的这个很好理解。 如何理解Cookie从服务器来到服务器去来源服务器将想进行存储的信息通过字符串键值对的方式放到HTTP响应的Set-Cookie中终点服务器。客户端发送HTTP请求都会带上之前存储的Cookie信息在HTTP的Header的Cookie中让服务器去分析之前干了啥。 如何理解Cookie是在浏览器中工作的session是在服务器这边工作的 识别用户信息的方式服务器直接通过Set-Cookie的方式返回用户信息给浏览器, 浏览器直接保存服务器保存用户信息, 然后通过键值对进行保存. 其中键是由服务器(根据用户信息??)自动生成的唯一字符串, 值就是用户的详细信息. 服务器可以只把键(唯一字符串)通过Set-Cookie返回给浏览器. 两者的区别就是后者有点像是加密了, 后者将用户信息加密为键.后面这样的处理方式, 就是会话方式. 键值对称之为session(会话), 唯一的字符串就称之为sessionId.假如之前已经认证过信息,则再次登录流程如:浏览器已有sessionId, 发送请求时候将sessionId一同发送给服务器服务器根据接收的sessionId在哈希表中查找用户信息, 假如拿到了用户身份信息, 就认证成功, 反之失败. 服务器中用于存储用户信息的哈希表以sessionId为key, 用户信息为value每一个服务器中都会有很多个webapp一个webapp对应一个存储session的哈希表每一个哈希表的内容都是sessionId session对象每一个session对象中又可以又很多个键值对,如图:创建一个前端页面bodyform actionlogin methodpostinput typetext nameUsernameinput typepassword namePasswordinput typesubmit valueSubmit/form
/body效果如点击Submit提交post请求后后端处理登录信息查看用户名和密码输入格式为空为null都要求重新输入查看用户名和密码是否正确不正确要求重新输入WebServlet(/login)
public class LoginServlet extends HttpServlet {Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username req.getParameter(Username);String password req.getParameter(Password);if (username null || username.equals() || password null || password.equals()) {
// 信息有误重定向到登录页面resp.sendRedirect(login.html);return;}// 用户名密码错误重定向到登录页面if (!username.equals(Mattylyh) !password.equals(222)) {resp.getWriter().write(Login failed, check your info before try again.);return;}// 登录信息正确创建sessionHttpSession session req.getSession(true);
// 将用户信息键值对写到session中session.setAttribute(username, Mattylyh);
// 页面重定向到index初始页面 参数是相对路径resp.sendRedirect(index);}
}验证信息成功后WebServlet(/index)
public class IndexServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {// index页面的作用就是将用户信息显示出来HttpSession session req.getSession(false);if (session null) {
// 登录失败就跳转登录页面resp.sendRedirect(login.html);return;}String username session.getAttribute(Username);resp.setContentType(text/html; charsetutf8);resp.getWriter().write(当前用户 username);}
} session和Cookie的协同作用主要是用来保持登录状态吧即第一次验证登录成功后服务器给浏览器一个Set-Cookie然后浏览器根据这个Set-Cookie的内容设置好Cookie。这个Set-Cookie和Cookie的内容都是一个JSESSIONID有了这个JSESSIONID之后每次访问服务器都会知道你是通过了登陆验证的就不会需要你重复登录了。但是这个管理会话的哈希表是在服务器内存中的假如服务器重启了那原理啊的哈希表也没了就会需要重启会话。 改进成一个可看到访问次数通过sessionId判断同一用户的代码WebServlet(/login)
public class LoginServlet extends HttpServlet {Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {String username req.getParameter(username);String password req.getParameter(password);if (username null || username.equals() || password null || password.equals()) {
// 信息有误重定向到登录页面resp.sendRedirect(login.html);return;}if (!username.equals(Mattylyh) || !password.equals(222)) {resp.getWriter().write(Login failed, check your info before try again.);return;}HttpSession session req.getSession(true);
// 将用户信息键值对写到session中session.setAttribute(username, Mattylyh);session.setAttribute(visitCount, 0);
// 页面重定向到index初始页面 参数是相对路径resp.sendRedirect(index);}
} WebServlet(/index)
public class IndexServlet extends HttpServlet {Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {resp.setContentType(text/html; charsetutf8);
// index页面的作用就是将用户信息显示出来HttpSession session req.getSession(false);if (session null) {
// 登录失败就跳转登录页面resp.sendRedirect(login.html);return;}String username String.valueOf(session.getAttribute(username));Integer visitCount (Integer) session.getAttribute(visitCount);visitCount 1;session.setAttribute(visitCount, visitCount);resp.getWriter().write(当前用户 username 访问次数 visitCount);}
}