求个实现日期时间动态显示(就是会自动1秒1秒地加上去)的代码,用JAVA做脚本,我知道静态显示很简单,可是不知道如何动态显示,顺便再加个星期几吧

解决方案 »

  1.   

    http://www.25175.com/200609/25175/25175_html/2007-06/1884.html
      

  2.   

    是我没说清楚吗  我想用JAVA语言实现,而不是JS,谢谢
      

  3.   

    setTimeout 去定时执行某个函数
      

  4.   

    能给个代码吗?因为我才开始学JSP,而且对JAVA也不是太熟,所以....
      

  5.   

    你要显示的时间是服务器时间还是用户客户机的时间,有点不一样啊
    1.客户机时间(简单)
    <body>
    <span id="aa1"></span>
    <script>
      showTime();
      function showTime(){
        var now = new Date();
        document.getElementById("aa1").innerHTML=now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
        setTimeout("showTime()",1000);
      }
    </script>
    </body>
    2.服务器时间(也不难)
    <%@ page language="java" import="java.util.*" contentType="text/html;charset=GBK" %>
    <html>
    <body>
    <span id="aa1"></span>
    <script>
      var now = new Date();
      now.setTime(<%=new Date().getTime()%>);
      showTime();
      function showTime(){
        document.getElementById("aa1").innerHTML=now.getHours()+":"+now.getMinutes()+":"+now.getSeconds();
    var c=now.getTime();
    c+=1000;
    now.setTime(c);
        setTimeout("showTime()",1000);
      }
    </script>
    </body>
    </html>
      

  6.   

    lz,你要是非要用java不用js,还要每间隔断时间变化一次,那没办法,ajax吧!!
      

  7.   

    非得用JAVA啊,老师布置的作业,让我们往JSP页面中添加这个功能,但必须是用JAVA
      

  8.   

    可以用javascript  <div id="divTime">
    <script language="javascript">setInterval("divTime.innerHTML=new Date().toLocaleString()+'星期'+'日一二三四五六'.charAt(new Date().getDay())",500)
    </script>
    </div>
      

  9.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Time</title>
    <script>
    var xmlHttp;
    function createXMLHttpRequest(){
    if(window.ActiveXObject){
    xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
    }else if(window.XMLHttpRequest){
    xmlHttp=new XMLHttpRequest();
    }
    }
    function exec(){
    createXMLHttpRequest();
    xmlHttp.open("GET","/Ajax/servlet/DateUpdateServlet",true);
    xmlHttp.onReadyStateChange=handleFun;
    xmlHttp.send(null);
    }
    function handleFun(){
    if(xmlHttp.readyState==4){
    if(xmlHttp.status==200){
    //document.getElementById("div").innerText="";
    document.getElementById("div").innerText=xmlHttp.responseTEXT;
    }
    }
    }
    function a(){
    alert(1);
    }
    setInterval("exec()",1000);
    </script>
    </head>
    <body>
    <div id="div"></div>
    </body>
    </html>
    package com.myself;import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;public class DateUpdateServlet extends HttpServlet { /**
     * The doGet method of the servlet. <br>
     *
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request the request send by the client to the server
     * @param response the response send by the server to the client
     * @throws ServletException if an error occurred
     * @throws IOException if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
    String time = sdf.format(date);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.write(time);
    out.flush();
    out.close();
    } public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request,response);
    }
    }
    还有一中方式:
    <%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.Date,java.text.SimpleDateFormat"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <%
    Date date = new Date();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
    out.println(sdf.format(date));
    response.setHeader("refresh","1;URL=date.jsp");
    %>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    </body>
    </html>
    第一种是ajax
      

  10.   

    哈哈,谁叫你手动刷新的,response.setHeader("refresh","1;URL=date.jsp"); 的意思不明白吗??
      

  11.   

    楼主说话有点颠三倒四,什么是用java作脚本?
      

  12.   

    是JAVA的applet吧?不过这个东西已经太老了 现在好像很少人用做脚本的
    用java做那个大材小用了  还是建议你用JS WEB的用JS做这些很方便