这里写目录标题
0 @WebServlet注解的使用
0.1 配置.xml文档头
注意,version>3.0,metadata-complete=“false”,使用注解式配置
<?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_3_1.xsd"
version="3.1"
metadata-complete="false">
</web-app>
0.2 代码注解配置
在类上加上@WebServlet及映射路径
@WebServlet(urlPatterns = "/s3.do")
public class SessionDemo03 extends HttpServlet {
}
1 会话
会话:用户打开一个浏览器,点击了很多超链接,访问了多个Web资源,关闭了浏览器,这个过程可以称为会话
有状态会话:用户曾经访问过服务器,当他下次访问的时候,服务器知道他曾经访问过,称为有状态会话
怎么证明访问者的身份,怎么证明你来过,怎么向服务端证明客户端来过呢
cookie:服务端给客户端一个证件,客户端下次访问带上证件即可
session:服务器登记你来过,下次来的时候匹配即可
2 保存会话的两种技术
cookie
- 客户端技术(请求、响应)
session
- 服务器技术,利用这个技术,可以保存用户的会话信息,我们可以把信息或数据放在session中
常见场景,网站登录后,下次不用再登录,第二次直接默认登录成功
3 Cookies
1.从请求中拿到Cookie
2.服务器响应给客户端
Cookie[] cookies = req.getCookies();//从请求中获得Cookies
cookie.getName();//获得cookie的名字
cookie.getValue();//获得cookie的值
Cookie cookie1 = new Cookie("time",System.currentTimeMillis()+"");//新建一个cookie
cookie.setMaxAge(24*60*60);//设置cookie有效期
resp.addCookie(cookie);//把cookie响应给客户端
cookie,一般保存在本地的用户目录下的AppData
- 一个cookie只能保存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
- cookie大小有限制
- 浏览器上限一般300个cookie
删除cookie
- 不设置有效期,关闭浏览器自动失效
- 设置有效期为0
编码与解码
Cookie namecookie = new Cookie("name", URLEncoder.encode("李逍遥","UTF-8"));
out.write("上次访问的用户是:"+ URLDecoder.decode(cookie.getValue(),"UTF-8"));
3 Session(重点)
什么是Session:
- 服务器会给每个用户(浏览器)创建一个Session
- 一个Session独占一个Session,只要浏览器没有关闭,Session就存在
- 用户登录之后,整个网站都能访问!–>保存用户信息;保存购物车;
Session和Cookie的区别:
- Cookie是把用户的数据写给浏览器,浏览器保存(可以保存多个)
- Session把用户的数据写到用户独占的Session中,服务器端保存(保存重要信息,减少服务器资源占用)
- Session对象由服务器默认创建,浏览器第一次登录时创建,旧Session失效后默认创建新Session,客户端访问时通过Cookie携带SessionID访问服务器上的Session
- Cookie对象由服务器主动创建,可以创建多个
使用场景:
- 保存用户登录信息
- 购物车信息
- 在整个网站中经常会使用的数据,保存在Session中
使用Session的方法
3.1 向Session存入数据
public class SessionDemo01 extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码
req.setCharacterEncoding("UTF-8");
resp.setContentType("text/html;charset=UTF-8");
//得到Session
HttpSession session = req.getSession();
//存入读取返回
session.setAttribute("person",new Person("明夜",21));
String id = session.getId();
PrintWriter writer = resp.getWriter();
if(session.isNew()){
writer.write("Session创建成功,ID:"+id);
}else{
writer.write("Session已存在,ID:"+id);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.2 从Session中取出数据
public class SessionDemo02 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
HttpSession session = req.getSession();
Person person = (Person)session.getAttribute("person");
PrintWriter writer = resp.getWriter();
writer.write(person.toString());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.3 注销Session
@WebServlet(urlPatterns = "/s3.do")
public class SessionDemo03 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("person");
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
3.4 Session会话自动过期
web.xml中配置
<!--设置Session失效时间-->
<session-config>
<!--15分钟后Session自动失效-->
<session-timeout>15</session-timeout>
</session-config>