会话跟踪技术

什么是会话跟踪技术

客户向某一个服务器发出第一个请求开始,会话就开始了,直到客户关闭了浏览器会话结束,在一个会话的多个请求中共享数据,这就是会话跟踪技术。

会话跟踪技术常用的四种方法

1,使用servlet API中的session机制
2,使用cookie
3,分别是将会话id存放在url路径中,执行重写
4,隐藏表单域

cookie方法代码演示

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="cookie" method="post">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="pwd"><br/>
保存时间:<input type="radio" name="time" value="86400">保存一天&nbsp;
<input type="radio" name="time" value="3600">保存一小时<br/>
<input type="submit" value="登录">
</form>
</body>
</html>

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
	String name=request.getParameter("username");
	String pwd=request.getParameter("pwd");
	String time=request.getParameter("time");
	Cookie c=new Cookie("name", name);
	Cookie c1=new Cookie("pwd", pwd);
	c.setMaxAge(Integer.parseInt(time));
	c1.setMaxAge(Integer.parseInt(time));
	response.addCookie(c);
	response.addCookie(c1);
	}

}

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
 Cookie[]c=request.getCookies(); 
for( int i=0;i<c.length;i++){
	out.print(c[i].getName());
	out.print(c[i].getValue());
}
%>
</body>
</html>

url重写代码示例

	request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
	   String path=getServletContext().getContextPath();
	   String newpath=response.encodeURL(path+"/first2.jsp");
	   //如果说这个新地址后面有session,意味着cookie被禁用
	   if (newpath.endsWith(session.getId())) {
		out.print("cookie被禁用了");
		out.print("URL重写了"+newpath);
	}else {
		out.print("cookie没被禁用");
		out.print("URL没重写"+newpath);
	}
	}

隐藏表单域代码示例

<form action="login" method="post">
学生姓名<input type="text" name="username">;<br/>
学生性别<input type="radio" name="sex" value="男">&nbsp;
<input type="radio" name="sex" value="女"><br/>
学生电话<input type="text" name="phone"><br/>
<input type="hidden" name="id" value="${name}">
<input type="submit" value="提交">
</form>
request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String name=request.getParameter("username");
		String sex=request.getParameter("sex");
		String phone=request.getParameter("phone");
		String id=request.getParameter("id");
		if (id.equals(null)||id.equals("")) {
			//调用添加的学生的业务逻辑
			System.out.println("添加学生业务逻辑");
		}else {
			//调用修改学生的业务逻辑
			System.out.println("修改学生的业务逻辑");
		}
	}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值