侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Java web的会话跟踪技术

Java  /  管理员 发布于 3年前   339

会话跟踪主要是用在用户页面点击不同的页面时,需要用到的技术点

 

会话:多次请求与响应的过程

 

 

1,url地址传递参数,实现页面跟踪技术

         格式:传一个参数的

url?名=值 

    传两个参数的

url?名=值 &名=值

 

关键代码实例:

// 获得提交的数据String userName = request.getParameter("userName");String userPwd = request.getParameter("userPwd");System.out.println(userName + "    " + userPwd);if ("abc".equals(userName) && "111".equals(userPwd)) {response.sendRedirect("suc.jsp?n=" + userName + "&p=" + userPwd);}

 

在登录成功的页面获取账号的密码信息;

<% //获取账号和密码 String names = request.getParameter("n"); String pwd = request.getParameter("p");%>账号:<%=names%>   密码:<%=pwd %>

 

url传递参数会将传递会显示条状页面的网页地址栏中

 

2,form表单的隐藏域传递参数

   格式如下:

form><input type="hidden" name="名" value=“值”/><input type="hidden" name="名" value=“值”/></form>

 

使用技术的关键代码实例:

传递姓名和密码:

// 获得提交的数据String userName = request.getParameter("userName");String userPwd = request.getParameter("userPwd");System.out.println(userName + "    " + userPwd);if ("abc".equals(userName) && "111".equals(userPwd)) {       request.setAttribute("aaa",userName);//使用隐藏域,不能使用重定向请求request.getRequestDispatcher("suc.jsp").forward(request, response);}

 

在jsp页面中获取账号和密码:

<% //获取java代码中的属性   String name = (String)request.getAttribute("aaa");%><form action="addBlog" method="post">//隐藏域获取jsp中的数据<input type="hidden" name="uname" value="<%=name %>" /></form>

 

 

3,Cookie会话跟踪技术;

    Cookie是保存在浏览器缓存/或临时文件中的一系列文本数据

//创建Cookie

Cookie ck = new Cookie("名",值);response.addCookie(ck);

 

 

//获取Cookie

Cookie[] cks = request.getCookies();for(Cookie ck:cks){String name = ck.getName();String value = ck.getValue();}

 

Cookie的作用:

1.跟踪特定对象:记录浏览过的商品2.统计网页的访问次数3.简化登录过程:自动登录

 

 

Cookie特点:

1.Cookie一定是保存在浏览器中的文本2.Cookie不安全,容易泄露个人信息3。用户可以禁用浏览器的Cookie功能

 

 

Cookie分为:

临时性Cookie:保存在浏览器的缓存中,当浏览器关闭后就没有了持久性Cookie:保存在系统的临时文件夹中,达到有效期后失效如何保存持久性Cookie:只要给Cookie设置有效时间,就是一个持久性CookieCookie ck = new Cookie("名",值);ck.setMaxAge(60);

 

Cookie的使用技术:

关键代码实例:

// 获得提交的数据String userName = request.getParameter("userName");String userPwd = request.getParameter("userPwd");System.out.println(userName + "    " + userPwd);if ("abc".equals(userName) && "111".equals(userPwd)) {response.sendRedirect("suc.jsp?n=" + userName + "&p=" + userPwd);//使用Cookie来保存数据Cookie cookie = new Cookie("user", userName+","+userPwd);//将获取的Cookie数据放在浏览器中response.addCookie(cookie);//设置时间cookie.setMaxAge(100);}

 

在jsp中获取Cookie保存的数据

<%String uname="",upwd="";        //获取Cookie中的键值      Cookie[] cs  = request.getCookies();   if(cs!=null){    for(int i=0;i<cs.length;i++){    Cookie c =cs[i];    if(c.getName().equals("user")){    String str = c.getValue();    String[] s = str.split(",");    uname = s[0];    upwd = s[1];     }     }   }%><form action="LoginServlet" method="post">账号:<input type="text" name="userName"  value="<%=uname %>"/>密码:<input type="password" name="userPwd" value="<%=upwd %>"/><input type="submit" value="提交"/></form>

 

浏览器可以关闭Cookie功能

 

4,Seesion会话跟踪技术;

格式:

HttpSession  会话对象//获得SessionHttpSession session = request.getSession();//设置数据session.setAttribute("名",值);//获取数据Object obj= session.getAttribute("名");

 

Seesion传递参数的关键代码实例:

// 获得提交的数据String userName = request.getParameter("userName");String userPwd = request.getParameter("userPwd");System.out.println(userName + "    " + userPwd);if ("abc".equals(userName) && "111".equals(userPwd)) {response.sendRedirect("suc.jsp?n=" + userName + "&p=" + userPwd);//将账号保存到Session对象中HttpSession session = request.getSession();session.setAttribute("bbbb",userName);}

 

java获取Session传递的参数;

 HttpSession session = request.getSession(); String name =(String)session.getAttribute("bbbb");//输出System.out.println("用户名:"+name);

 

 

Session会话跟踪式存在于服务器的Cookie会话跟踪是存在与浏览器的


  • 上一条:
    JavaScript动态改变样式访问技术
    下一条:
    php基于jquery的ajax技术传递json数据简单实例
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • java 正则表达式基础,实例学习资料收集大全 原创(0个评论)
    • java正则表达式彻底研究(0个评论)
    • java正则表达式验证函数(0个评论)
    • MVC、MVP和MVVM分别是什么(0个评论)
    • java 单例模式(饿汉模式与懒汉模式)(0个评论)
    • 近期文章
    • ChatGPT再出新功能,推出插件功能,能联网、搜索了(0个评论)
    • 在go语言中使用GoPDF包把html生成PDF文件示例(0个评论)
    • 在go语言中创建和解析(读取)符号链接示例(0个评论)
    • ubuntu 22.04系统中报错:Python 3.6 is no longer supported by the Python core team...解决方式(0个评论)
    • Laravel 10.4版本发布(0个评论)
    • mysql5.7中实现分区表及分区where in查询示例及分区分表对比浅析(0个评论)
    • nginx + vue配置实现同域名下不同路径访问不同项目(0个评论)
    • 在laravel框架中的5个HTTP客户端技巧分享(0个评论)
    • 在go语言中使用FFmpeg库实现PCM音频文件编码为mp3格式文件流程步骤(0个评论)
    • gopacket免安装Pcap实现驱动层流量抓包流程步骤(0个评论)
    • 近期评论
    • 博主 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 @ xiaoB 你只管努力,剩下的叫给天意;天若有情天亦老,..
    • xiaoB 在

      2023年国务院办公厅春节放假通知:1月21日起休7天中评论 会不会春节放假后又阳一次?..
    • BUG4 在

      你翻墙过吗?国内使用vpn翻墙可能会被网警抓,你需了解的事中评论 不是吧?..
    • 博主 在

      go语言+beego框架中获取get,post请求的所有参数中评论 @ t1  直接在router.go文件中配就ok..
    • Jade 在

      如何在MySQL查询中获得当月记录中评论 Dear zongscan.com team, We can skyroc..
    • 2016-11
    • 2018-03
    • 2020-03
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客