注意,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>
在类上加上@WebServlet及映射路径
@WebServlet(urlPatterns = "/s3.do") public class SessionDemo03 extends HttpServlet { }
会话:用户打开一个浏览器,点击了很多超链接,访问了多个Web资源,关闭了浏览器,这个过程可以称为会话
有状态会话:用户曾经访问过服务器,当他下次访问的时候,服务器知道他曾经访问过,称为有状态会话
怎么证明访问者的身份,怎么证明你来过,怎么向服务端证明客户端来过呢
cookie:服务端给客户端一个证件,客户端下次访问带上证件即可
session:服务器登记你来过,下次来的时候匹配即可
cookie
session
常见场景,网站登录后,下次不用再登录,第二次直接默认登录成功
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
编码与解码
Cookie namecookie = new Cookie("name", URLEncoder.encode("李逍遥","UTF-8")); out.write("上次访问的用户是:"+ URLDecoder.decode(cookie.getValue(),"UTF-8"));
什么是Session:
Session和Cookie的区别:
使用场景:
使用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); } }
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); } }
@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); } }
web.xml中配置
<!--设置Session失效时间--> <session-config> <!--15分钟后Session自动失效--> <session-timeout>15</session-timeout> </session-config>