目录
一、会话
会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话。
有状态会话:一个同学来过教师,下次再来教室,我们会知道这个同学曾经来过,称之为有状态会话。
一个网站,怎么证明你访问过?
1、服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了;cookie
2、服务器登记你来过,下次你来的时候我来匹配你;seesion
二、保存会话的两种技术
cookie
- 客户端技术(响应,请求)
session - 服务器技术,利用这个技术,可以保存用户的会话信息,我们可以把信息或者数据放在Session中!
1、代码:
//保存用户上一次访问的时间
public class Cookiedemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器告诉你的时间,把这个时间封装成为一个信件,你下次带来,我就知道你来了
//解决中文乱码问题
req.setCharacterEncoding("utf-16");
resp.setCharacterEncoding("utf-16");
PrintWriter out = resp.getWriter();
//Cookie,服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明Cookie可能存在多个
//判断Cookie是否存在
if(cookies!=null){
//如果存在
out.write("你上一次访问的时间是:");
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
//获取cookie中的名字
if (cookie.getName().equals("lastLoginTime")) {
//cookie.getValue();是个字符串
long lastLoginTime = Long.parseLong(cookie.getValue());
Date date = new Date(lastLoginTime);
out.write(date.toLocaleString());
}
}
}else{
out.write("这是你第一次访问本站");
}
//服务器给客户端响应一个cookie
Cookie cookie = new Cookie("lastLoginTime",System.currentTimeMillis()+"");
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
配置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"
metadata-complete="true">
<servlet>
<servlet-name>Cookiedemo1</servlet-name>
<servlet-class>com.lv.servlet.Cookiedemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Cookiedemo1</servlet-name>
<url-pattern>/c1</url-pattern>
</servlet-mapping>
</web-app>
代码分析:
2、删除Cookie
- 不设置有效期,关闭浏览器,自动失效
- 设置有效期为0
代码:
public class Cookiedemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建一个cookie。名字必须和要删除的名字一致
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
//将cookie有效期设置为0
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
配置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"
metadata-complete="true">
<servlet>
<servlet-name>Cookiedemo1</servlet-name>
<servlet-class>com.lv.servlet.Cookiedemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Cookiedemo1</servlet-name>
<url-pattern>/c1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Cookiedemo2</servlet-name>
<servlet-class>com.lv.servlet.Cookiedemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Cookiedemo2</servlet-name>
<url-pattern>/c2</url-pattern>
</servlet-mapping>
</web-app>
3、中文数据传递
public class Cookiedemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// req.setCharacterEncoding("utf-16");
// resp.setCharacterEncoding("utf-16");
//Cookie,服务器端从客户端获取
Cookie[] cookies = req.getCookies();//这里返回数组,说明Cookie可能存在多个
PrintWriter out = resp.getWriter();
//判断Cookie是否存在
if(cookies!=null){
//如果存在
out.write("你上一次访问的时间是:");
for(int i=0;i<cookies.length;i++) {
Cookie cookie = cookies[i];
//获取cookie中的名字
if (cookie.getName().equals("name")) {
//解码
out.write(URLDecoder.decode(cookie.getValue(), "UTF-8"));
}
}
}else{
out.write("这是你第一次访问本站");
}
//编码
Cookie cookie = new Cookie("name", URLDecoder.decode("小虎","UTF-8"));
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
结尾
感谢看到最后的朋友,都看到最后了,点个赞再走啊,如有不对之处还请多多指正。
网友评论