•当浏览器访问Web服务器时,Servlet容器就会创建一个Session对象和ID属性,其中,Session对象就相当于病历档案,ID就相当于就医卡号。
•当客户端后续访问服务器时,只要将标识号传递给服务器,服务器就能判断出该请求是哪个客户端发送的,从而选择与之对应的Session对象为其服务。
•通常情况下,Session是借助Cookie技术来传递ID属性的。
获取Session
•public HttpSession getSession(boolean create)
•public HttpSession getSession()
方法声明 | 功能描述 |
String getId() | 用于返回与当前HttpSession对象关联的会话标识号 |
long getCreationTime() | 返回Session创建的时间,这个时间是创建Session的时间与1970年1月1日00:00:00之间时间差的毫秒表示形式 |
long getLastAccessedTime() | 返回客户端最后一次发送与Session相关请求的时间,这个时间是发送请求的时间与1970年1月1日00:00:00之间时间差的毫秒表示形式 |
void setMaxInactiveInterval(int interval) | 用于设置当前HttpSession对象可空闲的以秒为单位的最长时间,也就是修改当前会话的默认超时间隔 |
boolean isNew() | 判断当前HttpSession对象是否是新创建的 |
void invalidate() | 用于强制使Session对象无效 |
ServletContext getServletContext() | 用于返回当前HttpSession对象所属于的WEB应用程序对象,即代表当前WEB应用程序的ServletContext对象 |
void setAttribite(String name,Object value) | 用于将一个对象与一个名称关联后存储到当前的HttpSession对象中 |
String getAttribute() | 用于从当前HttpSession对象中返回指定名称的属性对象 |
void removeAttribute(String name) | 用于从当前HttpSession对象中删除指定名称的属性 |
Web服务器采用了超时管理来判断客户端是否存在!
在一定时间内,如果某个客户端一直没有请求访问,那么,Web服务器会认为客户端已经结束请求,将对应的Session对象变成垃圾对象,并进行清除。反之,Web服务器会创建一个新的Session对象,并分配一个新的ID属性。
会话的有效时间可以在web.xml文件中设置,其默认值由Servlet容器定义。在<tomcat安装目录>\conf\web.xml文件中,可以找到如下一段配置信息:
如果将<session-timeout>元素中的时间值设置成0或负数,则表示会话永不超时!
利用URL重写Session跟踪
服务器在传递Session对象的ID属性时,是以Cookie的形式传递给浏览器的。但是,如果浏览器的Cookie功能被禁止,
引入了URL重写机制来保存用户的会话信息
indexServlet.java
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
// 解决乱码问题
response.setContentType("text/html;charset=utf-8");
// 创建或者获取保存用户信息的Session对象
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
if (user == null) {
response.getWriter().print(
"您还没有登录,请<a href='/chapter05/login.html'>登录</a>");
} else {
response.getWriter().print("您已登录,欢迎你," + user.getUsername() + "!");
response.getWriter().print(
"<a href='/chapter05/LogoutServlet'>退出</a>");
// 创建Cookie存放Session的标识号
Cookie cookie = new Cookie("JSESSIONID", session.getId());
cookie.setMaxAge(60 * 30);
cookie.setPath("/chapter05");
response.addCookie(cookie);
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
LoginServlet.java
public class LoginServlet extends HttpServlet {
/**
* 未添加验证码
*/
// public void doGet(HttpServletRequest request,
// HttpServletResponse response)
// throws ServletException, IOException {
// response.setContentType("text/html;charset=utf-8");
// String username = request.getParameter("username");
// String password = request.getParameter("password");
// PrintWriter pw = response.getWriter();
// //假设正确的用户名 是itcast 密码是123
// if (("itcast").equals(username) && ("123").equals(password)) {
// User user = new User();
// user.setUsername(username);
// user.setPassword(password);
// request.getSession().setAttribute("user", user);
// response.sendRedirect("/chapter05/IndexServlet");
// } else {
// pw.write("用户名或密码错误,登录失败");
// }
// }
/**
* 添加了验证码
*/
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkCode = request.getParameter("check_code");
String savedCode = (String) request.getSession().getAttribute(
"check_code");
PrintWriter pw = response.getWriter();
if (("itcast").equals(username) && ("123").equals(password)
&& checkCode.equals(savedCode)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
request.getSession().setAttribute("user", user);
response.sendRedirect("/chapter05/IndexServlet");
} else if (checkCode.equals(savedCode)) {
pw.write("用户名或密码错误,登录失败");
} else {
pw.write("验证码错误");
}
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}