无法将客户端的时间戳发送到服务器

时间:2015-09-08 07:46:58

标签: java jsp servlets timestamp server

请检查以下代码

JSP

<%-- 
    Document   : index
    Created on : Sep 8, 2015, 10:13:49 AM
    Author     : Yohan
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page contentType="text/html" import="java.util.Date" %>
<%@page contentType="text/html" import="java.sql.Timestamp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>

        <script>
            function time()
            {
                 var elem = document.getElementById("hiddenTxt");
                 elem.value = "<%= new Date().getTime()%>";
            }
        </script>

        <script type="text/javascript">
             function time2()
            {
                 var elem = document.getElementById("hiddenTxt");
                 elem.value = Date.now();
            }
        </script>

        <script type="text/javascript">
             function time3()
            {
                 alert(<%= new Date().getTime()%>);
            }
        </script>
    </head>
    <body>
        <button onclick="time3()" value="click" >Click</button>

        <form action="TimestampClass" method="post" onsubmit="time2()">
            Name: <input type="text" name="nameTxt">
            <input type="hidden" name="hiddenTxt" id="hiddenTxt" >
            <input type="submit" value="Submit">
        </form>
    </body>
</html>

的Servlet

package test;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

/**
 *
 * @author Yohan
 */
public class TimestampClass extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

//        long currentTimeMillis = System.currentTimeMillis();
//        Timestamp timestamp = new Timestamp(currentTimeMillis);
//        System.out.println(currentTimeMillis);
//        System.out.println(timestamp);

        String name = request.getParameter("nameTxt");
        long timeStampLong = Long.parseLong(request.getParameter("hiddenTxt"));

        PrintWriter out = response.getWriter();
        out.println(name);
        out.println("<br>");
        out.println("Script Time: "+getSQLCurrentTimeStamp( timeStampLong));
        out.println("<br>");
        out.println("Normal Time: "+getSQLCurrentTimeStamp());


        }

    public static java.sql.Timestamp getSQLCurrentTimeStamp(long timeStampLong)
    {
        Timestamp t2= new Timestamp(timeStampLong);
        return t2;
    }

     public static java.sql.Timestamp getSQLCurrentTimeStamp()
    {

        java.util.Date date= new java.util.Date();
    Timestamp t= new Timestamp(date.getTime());
        System.out.println(t);
        return t;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

我想要的只是将客户端PC的当前时间发送到服务器。我在JSP中尝试了Javascript和Java。

但是Javascript存在问题。我的服务器在亚马逊EC2 US-West,我在斯里兰卡。时差为+ 5.30GMT

当我部署代码时,javascript只是获取服务器的时间,而不是计算机中的时间。

我尝试在JSP中使用Java,但它有另一个问题。也就是说,无论我放置new Date.getTime()的哪个位置,它总是得到加载网页的时间,即使在几分钟后它也不会改变。

我在这做错了什么?我想要的只是将客户端的当前时间发送到服务器端Servlet。

2 个答案:

答案 0 :(得分:1)

摘录:

<%= new Date().getTime()%>

将始终获得服务器时间,因此忘记使用它。

要获取客户端时间戳,您需要使用JavaScript。在你的HEAD部分添加它(当然在脚本标签内):

if (!Date.now) {
    Date.now = function() { return new Date().getTime(); }
}

如果客户端使用的是Internet Explorer 8,它会设置Date.now函数,而Internet Explorer 8并不知道Date.now

我不知道JavaScript如何优化代码。也许你可以尝试这个,看看是否在函数内确定了日期是否有所不同:

    <form action="TimestampClass" method="post" onsubmit="document.getElementById('hiddenTxt') = Date.now();">
        Name: <input type="text" name="nameTxt">
        <input type="hidden" name="hiddenTxt" id="hiddenTxt" >
        <input type="submit" value="Submit">
    </form>

答案 1 :(得分:0)

尝试使用JS函数来获取时间

function time3(){
    alert(new Date().getTime());
}