ajax的简单应用

jsp文件:

<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/8/3
Time: 12:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="true" %>
<html>
<head>
<title>今进</title>
</head>
<body>

javascript时钟:<h1 id="jst"></h1>
ajax <h2 id="we"></h2>

<script>
//ajax js代码配合下面的jsp中的java代码实现时钟的走动
let a=new XMLHttpRequest(); //声明 ajax变量
a.onreadystatechange=function(){
if (a.status==200 && a.readyState==4){
document.getElementById("we").innerHTML=a.responseText;
}
}

function sst(){
a.open("post","test1.jsp",true);
a.setRequestHeader("Content-type","application/x-www-form-rulencoded")
a.send(null);
}
sst();
setInterval(sst,1000);


//只用js写日期;
function st() {
let d=new Date();
let nian=d.getFullYear();
let yue =(d.getMonth()+1).toString().padStart(2,0);
let ri =d.getDate().toString().padStart(2,0);
let shi =d.getHours().toString().padStart(2,0);
let fen =d.getMinutes().toString().padStart(2,0);
let miao =d.getSeconds().toString().padStart(2,0);
let day =d.getDay().toString().padStart(2,0);
let da="日一二三四五六".charAt(day);
let info =`${nian}年${yue}月${ri}日星期${da} ${shi}:${fen}:${miao}`
document.getElementById("jst").innerHTML=info;

}
st();
setInterval(st,1000);
</script>

</body>
</html>

jsp文件:
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Date" %><%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2020/8/10
Time: 20:26
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>今进</title>
</head>
<body>
<% //注意再此处的java代码配合上面的jsp中js代码
SimpleDateFormat jt =new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
out.print(jt.format(new Date()));
%>
</body>
</html>
上一篇:ES6变量的解构赋值、字符串的新增方法


下一篇:code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)