[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
最近学习ajax技术,前端改成了HTML静态网页来编写,那如何获取登陆的用户名呢?大家首先想到的肯定是利用Cookie和Session
,在JSP页面这样做可以,但是静态HTML页面就不行了。
*
首先Cookie和session是什么呢?
首先大家都知道,HTTP协议是无状态的协议。一旦数据交换完毕,客户端与服务器端的连接就会关闭,再次交换数据需要建立新的连接
。这就意味着服务器无法从连接上跟踪会话。这时候就出现了cookie会话跟踪技术。
给客户端们颁发一个通行证吧,每人一个,无论谁访问都必须携带自己通行证。这样服务器就能从通行证上确认客户身份了
。这就是Cookie的工作原理,是存储在客户端上的。
Session是服务器端使用的一种记录客户端状态的机制,相当于在服务器端建立一份用户档案表,只需要在档案表中进行查询,判断与当前客户是否匹配。
Session也是一种key-value的属性对,通过getAttribute(Stringkey)和setAttribute(String
key,Object value)方法读写客户状态信息。Servlet里通过request.getSession()方法获取该客户的Session。
*
HTML获取登陆用户名
session是JSP的九大内置对象之一
。对于HTML不太友好,那静态资源如何获取呢?一是建立一个过渡的Servlet,保存用户session,进行获取。第二个是
通过后台把路径+用户名进行拼接通过ajax传到前台,前台获取url对其进行操作。
*
第一步:把URL路径传到需要的页面
$.ajax({ url: '/login', data: { username: username, password: password },
type: 'GET', success: function (result) { if (result.code === 0) { if(val ==
num){ layer.msg("登录成功", function () { window.location.href =
"index1.html?"+$("#username").val()+""; }) }else{ layer.msg('验证码错误') } } else {
layer.msg("登录失败," + result.msg, {icon: 2}) } } }) }
重要的是 window.location.href = "index.html?"+$("#username").val()+"";这一句,其中
$("#username").val()获取用户名的值,username为用户名的id.
* 第二步,在index.html对路径进行获取和操作。
先定义一个接收用户名的id <p id = "name"></p>
再通过JS进行获取
<script type="text/javascript"> var url = location.href; //alert(url); var num
=url.indexOf("?"); var str = url.substr(num+1); $("#name").html("欢迎" +str); </
script>
在最后不要忘了添加
<script type="text/javascript" src="./layui/js/jquery.min.js"></script>
大功告成。