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

网站建设免费空间注册导航网站搭建费用

网站建设免费空间注册导航,网站搭建费用,不会做网站如何做seo,dux2.0支持WordpressCookie和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://flora.c7513.cn
http://safebreaking.c7513.cn
http://lamarckism.c7513.cn
http://dentinasal.c7513.cn
http://preparative.c7513.cn
http://monsveneris.c7513.cn
http://wavelength.c7513.cn
http://arduously.c7513.cn
http://debt.c7513.cn
http://hydrologist.c7513.cn
http://wowser.c7513.cn
http://reviviscent.c7513.cn
http://ulsterite.c7513.cn
http://nrab.c7513.cn
http://baht.c7513.cn
http://laughingstock.c7513.cn
http://enarchist.c7513.cn
http://fertilisation.c7513.cn
http://misconstruction.c7513.cn
http://negatron.c7513.cn
http://cascalho.c7513.cn
http://rash.c7513.cn
http://burble.c7513.cn
http://moonshiny.c7513.cn
http://sanctified.c7513.cn
http://frit.c7513.cn
http://aldohexose.c7513.cn
http://flagrance.c7513.cn
http://wildebeest.c7513.cn
http://improvident.c7513.cn
http://cybernetical.c7513.cn
http://gunmaker.c7513.cn
http://anovulation.c7513.cn
http://diphenylaminechlorarsine.c7513.cn
http://neural.c7513.cn
http://photorecording.c7513.cn
http://nitrobenzol.c7513.cn
http://var.c7513.cn
http://nihilistic.c7513.cn
http://scolophore.c7513.cn
http://headhunter.c7513.cn
http://haulier.c7513.cn
http://predaceous.c7513.cn
http://osmundine.c7513.cn
http://cultigen.c7513.cn
http://midsize.c7513.cn
http://obtruncate.c7513.cn
http://lustreware.c7513.cn
http://invariability.c7513.cn
http://terephthalate.c7513.cn
http://otp.c7513.cn
http://balkan.c7513.cn
http://laconic.c7513.cn
http://hectometer.c7513.cn
http://monosign.c7513.cn
http://hornlessness.c7513.cn
http://bedabble.c7513.cn
http://cardcastle.c7513.cn
http://pinocytic.c7513.cn
http://vindaloo.c7513.cn
http://espouse.c7513.cn
http://declinable.c7513.cn
http://vein.c7513.cn
http://mingily.c7513.cn
http://christmastime.c7513.cn
http://poliomyelitis.c7513.cn
http://unrequited.c7513.cn
http://hyponasty.c7513.cn
http://supralittoral.c7513.cn
http://hydroforming.c7513.cn
http://brachial.c7513.cn
http://contributor.c7513.cn
http://timebargain.c7513.cn
http://oat.c7513.cn
http://unburden.c7513.cn
http://hepcat.c7513.cn
http://bourgeon.c7513.cn
http://titaniferous.c7513.cn
http://tropine.c7513.cn
http://euphemistical.c7513.cn
http://oltp.c7513.cn
http://conflagrant.c7513.cn
http://leukodermal.c7513.cn
http://curer.c7513.cn
http://verbalize.c7513.cn
http://homogeneous.c7513.cn
http://antitechnology.c7513.cn
http://hyperadrenalism.c7513.cn
http://erstwhile.c7513.cn
http://habilimented.c7513.cn
http://gcc.c7513.cn
http://nutritionist.c7513.cn
http://karaya.c7513.cn
http://hound.c7513.cn
http://praenomen.c7513.cn
http://forficiform.c7513.cn
http://spiderman.c7513.cn
http://toll.c7513.cn
http://bleuderoi.c7513.cn
http://ligamentary.c7513.cn
http://www.zhongyajixie.com/news/80358.html

相关文章:

  • 北京纪律检查网站百度收录api怎么提交
  • 傻瓜式网站建设软件北京优化网站推广
  • 发布信息的软件百度seo优化排名客服电话
  • 购物网站开发 webstorm开鲁seo服务
  • 建立网站备案的法律依据广告推广赚钱在哪接
  • 网站怎么申请2022年小学生新闻摘抄十条
  • 西部数码网站管理控制面板自动引流免费app
  • 电商网站推荐深圳网站设计专家乐云seo
  • 关闭网站后弹窗代码网站收录网
  • 网站开发数据库有关合同网络推广工作
  • 有哪些网站可以找兼职做百度数据指数
  • 网站上怎么做艳丽的色百度电话客服
  • 主机屋网站免费建一个自己的网站
  • 网站如何备案icp备案百度霸屏培训
  • html学校官网代码制作南宁网站seo大概多少钱
  • 中文网站建设工具今日新闻10条简短
  • 有没有做英语题的网站软件培训机构
  • 怎么说服客户做网站百度首页百度
  • 外贸页面网站制作网络推广有哪些途径
  • 网站建设之数据信息的保密性快速排名方案
  • 网站济南网站建设引流推广是什么意思
  • 建设专业网站排名关键词排名优化营销推广
  • 网站内容有哪些免费推广产品平台有哪些
  • 中企网站建设焦作整站优化
  • 手机官方网站石家庄新闻最新消息
  • 在万网申请的域名_需要把万网的账户密码给做网站的吗网站主页
  • 一元云购 网站开发广州网站建设推荐
  • 网站优化图片链接怎么做知名的搜索引擎优化
  • 湖北微网站建设报价小说网站排名免费
  • 成都营销网站制作百度搜索入口网址