什么是会话跟踪技术
客户向某一个服务器发出第一个请求开始,会话就开始了,直到客户关闭了浏览器会话结束,在一个会话的多个请求中共享数据,这就是会话跟踪技术。
会话跟踪技术常用的四种方法
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">保存一天
<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="男">男
<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("修改学生的业务逻辑");
}
}