wuheng 2 gadi atpakaļ
vecāks
revīzija
a1146b668a

BIN
day12/web/WEB-INF/lib/fastjson-1.2.76.jar


+ 36 - 0
day13/src/com/lovecoding/ajax/AjaxServlet.java

@@ -0,0 +1,36 @@
+package com.lovecoding.ajax;
+
+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("/ajaxRequest")
+public class AjaxServlet extends HttpServlet {
+
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+        //模拟 请求延时 15秒
+//        try {
+//            Thread.sleep(15000);
+//        } catch (InterruptedException e) {
+//            e.printStackTrace();
+//        }
+// 私域流量
+// 孤岛效应
+//
+
+        resp.setContentType("text/html; charset=utf-8");
+        resp.getWriter().print("<ul>\n" +
+                "    <li>苹果</li>\n" +
+                "    <li>香蕉</li>\n" +
+                "    <li>鸭梨</li>\n" +
+                "    <li>水蜜桃</li>\n" +
+                "  </ul>");
+
+
+    }
+
+}

+ 79 - 0
day13/src/com/lovecoding/ajax/Brand.java

@@ -0,0 +1,79 @@
+package com.lovecoding.ajax;
+
+public class Brand {
+    private int id;
+    private String brandName;
+    private String companyName;
+    private int ordered;
+    private String description;
+    private int status;
+
+    public Brand(int i, String brandName, String companyName, int i1, String description, int i2) {
+        this.id = i;
+        this.brandName = brandName;
+        this.companyName = companyName;
+        this.description = description;
+        this.ordered = i1;
+        this.status = i2;
+    }
+
+    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 +
+                '}';
+    }
+}

+ 51 - 0
day13/src/com/lovecoding/ajax/JsonServlet.java

@@ -0,0 +1,51 @@
+package com.lovecoding.ajax;
+
+import com.alibaba.fastjson.JSON;
+
+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;
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
+@WebServlet("/jsonRequest")
+public class JsonServlet extends HttpServlet {
+
+
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+        resp.setContentType("text/json; charset=utf-8");
+
+        /**
+         * 前端发展特别好
+         * Ajax 传递 html 数据, 不方便交互
+         * JSON 是一种数据格式, JSON 没出现之前 大家交互数据都用 XML
+         * XML 用着非常不方便, XML 由于历史原因, 非常不安全
+         * JSON 就特带了 XML
+         * 微信支付 就爆出 XML 提权漏洞
+         * 微信支付 全站发邮件 提醒用户 处理 漏洞
+         */
+
+        Brand brand1 = new Brand( 1, "小米", "小米科技", 1, "are you ok", 1 );
+        Brand brand2 = new Brand( 1, "小米", "小米科技", 1, "are you ok", 1 );
+        Brand brand3 = new Brand( 1, "小米", "小米科技", 1, "are you ok", 1 );
+
+        List<Brand> brands = new ArrayList<Brand>();
+
+        brands.add(brand1);
+        brands.add(brand2);
+        brands.add(brand3);
+
+        String s = JSON.toJSONString( brands );
+
+        resp.getWriter().print( s );
+
+    }
+
+
+}

BIN
day13/web/WEB-INF/lib/fastjson-1.2.76.jar


BIN
day13/web/WEB-INF/lib/servlet-api.jar


+ 6 - 0
day13/web/WEB-INF/web.xml

@@ -0,0 +1,6 @@
+<?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_4_0.xsd"
+         version="4.0">
+</web-app>

+ 47 - 0
day13/web/ajax.jsp

@@ -0,0 +1,47 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/2/10
+  Time: 11:27
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+<head>
+    <title>Ajax</title>
+</head>
+<body>
+<div style="width: 800px; margin: 0 auto">
+    <button onclick="sendData()" > sendData </button>
+    <div id="ajax-respone" style="width: 800px; height: 390px; border: 1px solid red; overflow: auto;">
+
+    </div>
+
+</div>
+
+<script>
+
+    var sendData = function(){
+        var xmlHttpRequest = new XMLHttpRequest();
+        xmlHttpRequest.onreadystatechange = function () {
+            if ( xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200 ) {
+                //document.getElementById("ajax-respone").innerHTML = xmlHttpRequest.responseText;
+                var parse = JSON.parse( xmlHttpRequest.responseText );
+                for (let i = 0; i < parse.length; i++) {
+                    let str = parse[i].brandName + " "
+                            + parse[i].companyName + " "
+                            + parse[i].description + " "
+                            + parse[i].id + " "
+                    var htmlSpanElement = document.createElement("p");
+                    htmlSpanElement.innerText = str
+                    document.getElementById("ajax-respone").append( htmlSpanElement )
+                }
+            }
+        }
+        xmlHttpRequest.open("GET", "<%=request.getContextPath()%>/jsonRequest", true)
+        xmlHttpRequest.send()
+    }
+
+</script>
+</body>
+</html>

+ 44 - 0
day13/web/index.jsp

@@ -0,0 +1,44 @@
+<%--
+  Created by IntelliJ IDEA.
+  User: 武恒
+  Date: 2023/2/10
+  Time: 9:10
+  To change this template use File | Settings | File Templates.
+--%>
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<html>
+  <head>
+    <title>Ajax</title>
+  </head>
+  <body>
+
+
+  <div style="width: 500px; margin: 0 auto">
+    <button onclick="sendData()" > sendData </button>
+    <div id="ajax-respone" style="width: 400px; height: 400px; border: 1px solid red">
+
+    </div>
+
+  </div>
+
+
+  <script>
+
+    var sendData = function(){
+      var xmlHttpRequest = new XMLHttpRequest();
+      xmlHttpRequest.onreadystatechange = function (state) {
+        if ( xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200 ) {
+          document.getElementById("ajax-respone").innerHTML = xmlHttpRequest.responseText;
+          //alert( xmlHttpRequest.responseText )
+        }
+      }
+      xmlHttpRequest.open("GET", "<%=request.getContextPath()%>/ajaxRequest", true)
+      xmlHttpRequest.send()
+
+    }
+
+  </script>
+
+
+  </body>
+</html>