1.会话概念
会话是一个抽象的词,我们可以这样简单的理解:当用户开一个浏览器,点击了很多个链接,访问服务器多个web资源,然后关闭浏览器,这整个过程称为会话。‘
2.保存会话数据的两种技术
- Cookie:客户端技术,程序把每个用户的数据以cookie的形式写给用户各自的浏览器。当用户使用浏览器再去访问服务器中的web资源时,就会带着各自的数据去。这样,web资源处理的就是用户各自的数据了
- Session:服务器端技术,利用这个技术,服务器在运行时可以为每一个用户的浏览器创建一个其独享的session对象,由于session为用户浏览器独享,所以用户在访问服务器的web资源时,可以把各自的数据放在各自的session中,当用户再去访问服务器中的其它web资源时,其它web资源再从用户各自的session中取出数据为用户服务
3.Cookie类中的一些常用方法
| 方法 | 描述 |
| :-----------------------------------: | :-----------------------------------------------------: |
| Cookie(String name,String value) | 构造方法,实例化Cookie对象,name为Cookie的名称,value为值 |
| public String getName() | 取得 Cookie的名称 |
| public String getValue() | 取得Cookie的值 |
| public void setValue(String newValue) | 设置Cookie的值 |
| public void setMaxAge(int expiry) | 设置Cookie的最大保存时间,就是Cookie的有效期 |
| public int getMaxAge() | 获取Cookie的有效期 |
| public void setPath(String uri) | 设置Cookie的有效路径 |
| public String getPath | 获取Cookie的有效路径 |
| public void setDomain(String pattern) | 设置Cookie的有效域 |
| public String getDomain() | 获取Cookie的有效域 |
4.使用Cookie记录用户上一次访问时间
Java类:
import javax.servlet.http.Cookie;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieTest extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
//设置服务器端以UTF-8编码进行输出
response.setCharacterEncoding("UTF-8");
//设置浏览器以UTF-8编码进行接收,解决中文乱码问题
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
//获取浏览器访问访问服务器时传递过来的cookie数组
Cookie[] cookies = request.getCookies();
//如果用户是第一次访问,那么得到的cookies将是null
if (cookies!=null) {
out.write("您上次访问的时间是:");
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("lastAccessTime")) {
Long lastAccessTime =Long.parseLong(cookie.getValue());
Date date = new Date(lastAccessTime);
out.write(date.toLocaleString());
}
}
}else {
out.write("这是您第一次访问本站!");
}
//用户访问过之后重新设置用户的访问时间,存储到cookie中,然后发送到客户端浏览器
Cookie cookie = new Cookie("lastAccessTime", System.currentTimeMillis()+"");//创建一个cookie,cookie的名字是lastAccessTime
//将cookie对象添加到response对象中,这样服务器在输出response对象中的内容时就会把cookie也输出到客户端浏览器
response.addCookie(cookie);
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doPost(request, response);
}
}
填写web.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>CookieTest</servlet-name>
<servlet-class>Servlet.CookieTest</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieTest</servlet-name>
<url-pattern>/c1</url-pattern>
</web-app>
启动Tomcat后,在网页上输入http://localhost:8081/Cookie/c1后,网页显示: