Ver Fonte

session

wuheng há 2 anos atrás
pai
commit
2236894b8f

+ 70 - 0
day09/src/com/lovecoding/request/Brand.java

@@ -0,0 +1,70 @@
+package com.lovecoding.request;
+
+public class Brand {
+    private int id;
+    private String brandName;
+    private String companyName;
+    private int ordered;
+    private String description;
+    private int status;
+
+    public int getId() {
+        return id;
+    }
+
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getBrandName() {
+        return brandName;
+    }
+
+    public void setBrandName(String brandName) {
+        this.brandName = brandName;
+    }
+
+    public String getCompanyName() {
+        return companyName;
+    }
+
+    public void setCompanyName(String companyName) {
+        this.companyName = companyName;
+    }
+
+    public int getOrdered() {
+        return ordered;
+    }
+
+    public void setOrdered(int ordered) {
+        this.ordered = ordered;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public int getStatus() {
+        return status;
+    }
+
+    public void setStatus(int status) {
+        this.status = status;
+    }
+
+    @Override
+    public String toString() {
+        return "Brand{" +
+                "id=" + id +
+                ", brandName='" + brandName + '\'' +
+                ", companyName='" + companyName + '\'' +
+                ", ordered=" + ordered +
+                ", description='" + description + '\'' +
+                ", status=" + status +
+                '}';
+    }
+}

+ 37 - 0
day09/src/com/lovecoding/request/Demo1Servlet.java

@@ -0,0 +1,37 @@
+package com.lovecoding.request;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@WebServlet("/demo1Servlet")
+public class Demo1Servlet extends HttpServlet {
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+        /**
+         * demo1Servlet?name=zhangsan
+         * 获取指定参数, 适用于 GET POST
+         */
+        //String name = req.getParameter("name");
+
+        /**
+         * 它默认就是编码是 ISO-8859-1
+         * 这个码表没有中文, 所以传递中文会乱码
+         */
+        req.setCharacterEncoding("utf-8");
+        String name = req.getParameter("name");
+        System.out.println( name );
+
+        /**
+         * 打印浏览器agent
+         */
+        String header = req.getHeader("User-Agent");
+        System.out.println( header );
+
+
+
+    }
+}

+ 21 - 0
day09/src/com/lovecoding/request/ResponsesServlet.java

@@ -0,0 +1,21 @@
+package com.lovecoding.request;
+
+import com.sun.deploy.net.HttpRequest;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@WebServlet("/response")
+public class ResponsesServlet extends HttpServlet {
+
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+        resp.setStatus(404);
+        System.out.println(  resp.getStatus()  );
+
+    }
+}

+ 54 - 0
day09/src/com/lovecoding/request/SessionServlet.java

@@ -0,0 +1,54 @@
+package com.lovecoding.request;
+
+import com.sun.deploy.util.StringUtils;
+
+import javax.servlet.ServletException;
+import javax.servlet.annotation.WebServlet;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+import java.io.IOException;
+
+@WebServlet("/session")
+public class SessionServlet extends HttpServlet{
+
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+        req.setCharacterEncoding("UTF-8");
+
+        /**
+         * 最初 WAP 年代, 用户身份ID 是URL 里保存携带的
+         * 后来 就开始使用 Session , 主要是用浏览器 cookie 存储用户身份ID
+         * 再后来 就开始使用 浏览器 session 存储用户身份ID
+         * 现在呢 我们使用 JWT 技术, 把用户的数据信息 加密存储到 浏览器端, 通过Header 传递到服务器
+         */
+
+        //获取用户Session,  参数的意思是 如果没有给用户开启Session 则开启session
+        HttpSession session = req.getSession(true);
+        System.out.println( session.getId() );
+
+        String username = req.getParameter("username");
+        if ( username == null ) {
+            username = "";
+        }
+        session.setAttribute("name", username );
+
+        Brand brand = new Brand();
+        brand.setBrandName("新华书社");
+        brand.setCompanyName("新华");
+        brand.setDescription("新华出版社");
+        brand.setId(1);
+
+        /**
+         * 也即是说 Session 是持久化的, session没过期之前都可以读取数据
+         * Request 是临时的, 数据是在内存里传递的
+         */
+
+        session.setAttribute( "brand", brand );
+        req.setAttribute( "brand", brand );
+        req.getRequestDispatcher("./session.jsp").forward(req, resp);
+
+    }
+
+}

BIN
day09/web/WEB-INF/lib/jsp-api.jar


+ 1 - 0
day09/web/index.jsp

@@ -9,6 +9,7 @@
 <html>
   <head>
     <title>$Title$</title>
+    <meta charset="UTF-8" />
   </head>
   <body>
   day09

+ 46 - 0
day09/web/session.jsp

@@ -0,0 +1,46 @@
+<%@ page import="java.io.PrintWriter" %>
+<%@ page import="com.lovecoding.request.Brand" %>
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/2/4
+  Time: 14:21
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Title</title>
+</head>
+<body>
+
+
+    <span style="color: green"> 当前用户是:
+    <%
+
+        String name = (String) session.getAttribute("name");
+        //response 会先于jsp 执行
+        PrintWriter writer = response.getWriter();
+        //writer.print(name);
+        out.print( name );
+    %> 童鞋 </span>
+
+<hr />
+    <%
+        Brand brand = (Brand) session.getAttribute("brand");
+        out.print( "您购买的图书出版社是: " + brand.getBrandName() + "<br />" );
+        out.print( "您购买的图书出版商是: " + brand.getCompanyName()+ "<br />" );
+        out.print( "您购买的图书书号是: " + brand.getId()+ "<br />" );
+    %>
+
+
+    <hr />
+    <%
+        Brand brand1 = (Brand) request.getAttribute("brand");
+        out.print( "您购买的图书出版社是: " + brand1.getBrandName() + "<br />" );
+        out.print( "您购买的图书出版商是: " + brand1.getCompanyName()+ "<br />" );
+        out.print( "您购买的图书书号是: " + brand1.getId()+ "<br />" );
+    %>
+
+</body>
+</html>