Ajax axios get请求 时钟案例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>axios 时钟案例</title>
    <meta name="Keywords" content="关键字">
    <meta name="description" content="简介">
    <script src="https://unpkg.com/axios"></script>
</head>
<body>
<h3 class="st"></h3>
<hr>
<script>
    setInterval(()=>{
        axios.get("/time")
            .then(res=>{document.querySelector(".st").innerHTML=res.data})
            .catch(err=>console.error(err))})
    },1000)
</script>
</body>
</html>
package ws.wsj.servlet;

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.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 卋舉
 * @ProjectName IntelliJ IDEA
 * @ClassName Time
 * @description: TODO
 * @date 2021/10/19 21:23
 */
/*时钟效果*/
@WebServlet("/time")
public class Time extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date d = new Date();
    PrintWriter out = resp.getWriter();
    out.println(sdf.format(d));
    }
}

上一篇:【架构师面试-Java编程基本功-19】-Spring Boot工作原理与优点


下一篇:java邮件传输图片