标签:extends 时间 equal add map character mil exce get
用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话
一个网站怎么证明你来过
客户端 服务端
客户端技术(响应,请求)
public class CookieDemo01 extends HttpServlet {
@Override
protected void deGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决中文乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
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中的值
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.setMaxAge(24*60*60); //cookie有效期为一天
resp.addCookie(cookie);
}
@Override
protected void dePost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
<servlet>
<servlet-name>CookieDemo01</servlet-name>
<servlet-class>com.meng.servlet.CookieDemo01</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CookieDemo01</servlet-name>
<url-pattern>/c1</url-pattern>
</servlet-mapping>
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只能保存一个信息
一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
Cookie大小有限制4kb
300个cookie浏览器上限
删除Cookie:
解决中文乱码问题
Cookie cookie = new Cookie("name",URLEncoder.encode("秦疆","utf-8"));
out.write(URLDecoder.decode(cookie.getValue(),"UTF-8"));
服务端技术,利用这个技术,可以保存用户的会话信息?我们可以把信息或者数据放在Session中
什么是Session?
public class SessionDemo01 extends HttpServlet {
@Override
protected void deGet(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的ID
String sessionId = session.getId();
//判断Session是不是新创建
if (session.isNew()) {
resp.getWriter().write("session创建成功,ID:"+sessionId);
}else {
resp.getWriter().write("session已经在服务器中存在了,ID:"+sessionId);
}
//Session创建的时候做了什么事情
//Cookie cookie = new Cookie("JSESSIONID",sessionId);
//resp.addCookie(cookie);
}
@Override
protected void dePost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
public class SessionDemo02 extends HttpServlet {
@Override
protected void deGet(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();
String name = (String)session.getAttribute("name");
System.out.println(name);
}
@Override
protected void dePost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
}
//得到Session
HttpSession session = req.getSession();
Person person = (Person) session.getAttribute("name");
System.out.println(person.toString());
HttpSession session = req.getSession();
session.removeAttribute("name");
//手动注销Session
session.invalidate();
会话自动过期:web.xml配置
<!--设置Session默认的失效时间-->
<session-config>
<!--15分钟后Session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>
Session和cookie的区别
Session使用场景:
标签:extends 时间 equal add map character mil exce get
原文地址:https://www.cnblogs.com/oucmly/p/14669441.html