(十三)、Session(重点)
39.session详解:
1.什么是session? (1).服务器会给每一个用户(浏览器)创建一个Session对象; (2).一个Session单独占一个浏览器 (3).用户登入之后,整个网站都能访问它。==》保存购物车的信息 2.Session和Cookie的区别: (1).Cookie是把用户的数据写给用户的浏览器,浏览器保存 (可以保存多个) (2).Session是把用户的数据写到用户独占的Session中,服务器端保存 (保存重要的) (3).Session由服务端创建,不要我们new, 3.Session使用场景: (1).保存一个用户登入信息,可以登入多个服务端的网页 (2).在整个网站中会使用的数据,将其保存在session中,
1.Session的创建和赋值
package com.Jsxs.Cookie; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; public class Demo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-16"); req.setCharacterEncoding("UTF-16"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); // 请求得到session HttpSession session = req.getSession(); // 给session中存东西 session.setAttribute("name","吉士先生"); // 获取session的ID String id = session.getId(); // 判断session是否是新的 if(session.isNew()){ out.write("session新的装备已经创建,ID"+id); }else { out.write("session已经在服务器中存在了"+id); } // 实质: 调用session的时候,会自动生成一个cookie,所以此时此刻会显示所有 // Cookie cookie = new Cookie("JSESSIONID","id"); // resp.addCookie(cookie); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
2.得到创建的Session的值:
package com.Jsxs.Cookie; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import java.net.URLEncoder; public class Demo5 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("UTF-16"); resp.setCharacterEncoding("UTF-16"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); HttpSession session = req.getSession(); String name = (String) session.getAttribute("name"); out.write(name); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
3.运用对象:
package com.Jsxs.Cookie; import com.Jsxs.Cookie.Object.Person; import javax.servlet.ServletException; import javax.servlet.http.*; import java.io.IOException; import java.io.PrintWriter; import java.net.URLEncoder; public class Demo4 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-16"); req.setCharacterEncoding("UTF-16"); resp.setContentType("text/html"); PrintWriter out = resp.getWriter(); // 请求得到session HttpSession session = req.getSession(); // 给session中存东西 session.setAttribute("name",new Person("吉士先生",23)); // 获取session的ID String id = session.getId(); // 判断session是否是新的 if(session.isNew()){ out.write("session新的装备已经创建,ID"+id); }else { out.write("session已经在服务器中存在了"+id); } // 实质: 调用session的时候,会自动生成一个cookie,所以此时此刻会显示所有 // Cookie cookie = new Cookie("JSESSIONID","id"); // resp.addCookie(cookie); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
4.删除和移除的操作:
package com.Jsxs.Cookie; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.io.IOException; public class Demo6 extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { HttpSession session = req.getSession(); // 移除name session.removeAttribute("name"); // 注销session session.invalidate(); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
一个浏览器对应一个Session,删除和移除的操作:
5.web配置过期
<session-config> <!-- 单位是min--> <session-timeout>1</session-timeout> </session-config>
(十四)、JSP原理刨析
40.什么是JSP?
Java Server Pages: Java服务端页面,也和Servlet一样,用于动态的web开发 1.最大的特点: (1).写JSP就像在写HTML (2).写JSP不用在web里面注册信息。 2.区别: (1).HTML只给用户提供静态的数据 (2).JSP页面中可以嵌入JAVA代码,为用户提供动态数据
41.JSP原理
1.服务器内部工作: Tomcat中,有一个work目录,IDEA使用Tomcat会在IDEA中产生一个work目录
2.我的电脑地址: C:\Users\22612\AppData\Local\JetBrains\IntelliJIdea2021.1\tomcat\246560f4-bb63-4256-89d3-6446385cc047
发现页面转化为java程序