标签:enc set 请求 parse 删除 多个 str stl git
会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为状态会话;
一个网站,怎么证明你来过?
客户端 服务端
1.服务端给客户端一个信件,客户端下次访问服务端带上信件就可以了;cookie
2.服务器登记你来过了,下次你来的时候我来匹配你;session
cookie
session
1.从请求中拿到cookie信息
2.服务器响应给客户端cookie
Cookie[] cookies = req.getCookies(); //获得cookie
cookie.getName(); //获得cookie中的key
cookie.getValue; //获得cookie中的value
new Cookie("lastLoginTime",System.currentTimeMillis() + ""); //新建一个cookie
cookie.setMaxAge(24*60*60); //设置cookie的有效期
resp.addCookie(cookie); //响应给客户端一个cookie
cookie:一般会保存在本地的用户目录下appdata;
一个网站cookie是否存在上限?细节问题
删除cookie
不设置有效期,关闭浏览器,自动失效;
设置有效期时间为0;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//创建一个cookie,名字要和删除的名字一致
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
cookie.setMaxAge(0);
resp.addCookie(cookie);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//服务器,告诉你,你来的时间,把这个时间封装成为一个信件,你下次带来,就知道你来了
//解决中文乱码
resp.setContentType("text/html");
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
PrintWriter out = resp.getWriter();
//Cookie,服务器端从客户端获取
Cookie[] cookies = req.getCookies();
//判断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中的值
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() + "");
//设置cookie有效期
cookie.setMaxAge(24*60*60);
resp.addCookie(cookie);
resp.setCharacterEncoding("utf-8");
}
第一次访问:
第二次访问:
编码解码
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html");
Cookie[] cookies = req.getCookies();
PrintWriter out = resp.getWriter();
if ( cookies != null ) {
out.write("你上次访问的时间是:");
for ( int i = 0; i < cookies.length; i++ ) {
Cookie cookie = cookies[i];
if ( cookie.getName().equals("name") ) {
//解码
out.write(URLDecoder.decode(cookie.getValue(),"utf-8"));
}
}
} else {
out.write("这是你第一次访问");
}
//编码
Cookie cookie = new Cookie("name", URLEncoder.encode("提莫", "utf-8"));
resp.addCookie(cookie);
}
什么是session:
Session和Cookie的区别
使用Session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//给session中存东西
//session.setAttribute("name","卡莎");
session.setAttribute("name",new Person("多多",2));
//获取session的ID
String sessionId = session.getId();
//判断session是不是新创建的
if ( session.isNew() ) {
resp.getWriter().write("session创建成功,id为:" + sessionId);
} else {
resp.getWriter().write("session已经在服务器中存在了,id为:" + sessionId);
}
}
得到Session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//得到session
HttpSession session = req.getSession();
//获取session
// String name = (String) session.getAttribute("name");
//System.out.println(name);
Person name = (Person) session.getAttribute("name");
System.out.println(name);
}
手动注销Session
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("name");
//手动注销Session
session.invalidate();
}
会话自动过期:web.xml配置
<!-- 设置Session的默认注销时间 -->
<session-config>
<!-- x分钟后session自动失效,以分钟为单位 -->
<session-timeout>15</session-timeout>
</session-config>
标签:enc set 请求 parse 删除 多个 str stl git
原文地址:https://www.cnblogs.com/duoruic/p/14924810.html