当前位置: 首页 > news >正文

网站建设免费空间注册导航企业网站seo排名

网站建设免费空间注册导航,企业网站seo排名,网站用图要怎么做,手机在线网站Cookie和Session出于安全考虑,浏览器不让网页直接操作文件系统,而Cookie就是一个折中的方案,可以让网页暂存一些数据在本地,不能存复杂的对象,只能存字符串。Cookie是按照域名分类的,这个很好理解。如何理解…

Cookie和Session

出于安全考虑,浏览器不让网页直接操作文件系统,而Cookie就是一个折中的方案,可以让网页暂存一些数据在本地,不能存复杂的对象,只能存字符串。

Cookie是按照域名分类的,这个很好理解。

如何理解Cookie从服务器来,到服务器去

来源:服务器将想进行存储的信息,通过字符串键值对的方式放到HTTP响应的Set-Cookie中

终点:服务器。客户端发送HTTP请求都会带上之前存储的Cookie信息(在HTTP的Header的Cookie中),让服务器去分析之前干了啥。

如何理解Cookie是在浏览器中工作的,session是在服务器这边工作的

识别用户信息的方式:

  1. 服务器直接通过Set-Cookie的方式返回用户信息给浏览器, 浏览器直接保存

  1. 服务器保存用户信息, 然后通过键值对进行保存. 其中键是由服务器(根据用户信息??)自动生成的唯一字符串, 值就是用户的详细信息. 服务器可以只把键(唯一字符串)通过Set-Cookie返回给浏览器.

两者的区别就是后者有点像是加密了, 后者将用户信息加密为键.

后面这样的处理方式, 就是会话方式. 键值对称之为session(会话), 唯一的字符串就称之为sessionId.

假如之前已经认证过信息,则再次登录流程如:

  1. 浏览器已有sessionId, 发送请求时候将sessionId一同发送给服务器

  1. 服务器根据接收的sessionId在哈希表中查找用户信息, 假如拿到了用户身份信息, 就认证成功, 反之失败.

服务器中用于存储用户信息的哈希表

以sessionId为key, 用户信息为value

每一个服务器中都会有很多个webapp,一个webapp对应一个存储session的哈希表,每一个哈希表的内容都是sessionId + session对象,每一个session对象中又可以又很多个键值对,如图:

创建一个前端页面:

<body><form action="login" method="post"><input type="text" name="Username"><input type="password" name="Password"><input type="submit" value="Submit"></form>
</body>

效果如:

点击Submit提交post请求后,后端处理登录信息:

  1. 查看用户名和密码输入格式,为空为null都要求重新输入

  1. 查看用户名和密码是否正确,不正确要求重新输入

@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; charset=utf8");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; charset=utf8");
//        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);}
}
http://www.mmbaike.com/news/71714.html

相关文章:

  • 网站网页和网址的关系线上营销策略
  • 阿里云虚拟主机做多个网站百度框架户开户渠道代理
  • 网站建设合同首付多少钱广州网络营销推广公司
  • 网站开发规格如何做百度竞价推广
  • 厦门成交型网站建设公司百度引擎搜索网址
  • 旅游网站设计的优点怎么注册网站
  • 天津建设银行网站首页在线识别图片百度识图
  • 网站优化seo是什么意思网盘搜索神器
  • 网站版块设计seo课程总结怎么写
  • 做网站颜色如何搭配小程序生成平台系统
  • 西安网络推广公司网络推广网站优化要做哪些
  • 27寸显示器网站建设aso关键词覆盖优化
  • 厦门网站建设门户seo自动发布外链工具
  • 衡水市网站制作矿泉水软文广告500字
  • 网站建设平台有哪些百度搜索引擎关键词优化
  • 盗版视频网站怎么做的怎么发外链
  • 网站开发实训教程网络广告设计
  • 怎样做水果外卖加盟网站百度收录查询工具官网
  • 个人软件制作网站源码广州今天刚刚发生的重大新闻
  • 网站建设公司济南收录网站排名
  • 二级网站排名做不上去知识营销案例
  • 大同网站建设优化推广免费网站推广网站在线
  • 免费视频网站推广软件优化网站推广教程整站
  • 深圳做物流网站启信聚客通网络营销策划
  • 泉州网站建设定制雅思培训班价格一览表
  • 现在网站开发的前端语言seo应该怎么做
  • 企业网站营销的成功案例如何优化网页加载速度
  • 广州做网站最好的公司磁力云搜索引擎入口
  • 烘焙类网站开发时代背景网络营销推广的
  • 河南旅游网站建设搜索排名竞价