Javascript 如何发送客户端';将时间戳添加到Servlet?
我试图获得提交HTML表单时客户端机器的时间。目前,我正在使用这个内部servlet,但它会产生时区问题。所以为了解决这个问题,我决定在客户机中找到Javascript 如何发送客户端';将时间戳添加到Servlet?,javascript,java,jsp,servlets,timestamp,Javascript,Java,Jsp,Servlets,Timestamp,我试图获得提交HTML表单时客户端机器的时间。目前,我正在使用这个内部servlet,但它会产生时区问题。所以为了解决这个问题,我决定在客户机中找到时间戳,并在提交表单时将其发送到服务器。下面是我的代码 JSP <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page<
时间戳
,并在提交表单时将其发送到服务器。下面是我的代码
JSP
<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 = Date.now();
}
</script>
</head>
<body>
<form action="TimestampClass" method="post">
Name: <input type="text" name="nameTxt">
<input type="hidden" name="hiddenTxt" id="hiddenTxt" onsubmit="time()">
<input type="submit" value="Submit">
</form>
</body>
</html>
但是它不起作用,NumberFormatException
正在为timestamp字段抛出。那么,如何将客户机的时间戳正确地发送到servlet并在那里处理它呢
我不知道异常是否会有任何帮助,因为我确信它正在发生,因为没有向servlet传递任何内容。反正它,;在下面
Sep 08, 2015 11:09:01 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [TimestampClass] in context with path [/ForTesting] threw exception
java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:601)
at java.lang.Long.parseLong(Long.java:631)
at test.TimestampClass.processRequest(TimestampClass.java:41)
at test.TimestampClass.doPost(TimestampClass.java:76)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
在表单元素上添加提交回调
<form action="TimestampClass" method="post" onsubmit="time()">
Name: <input type="text" name="nameTxt">
<input type="hidden" name="hiddenTxt" id="hiddenTxt" >
<input type="submit" value="Submit">
</form>
姓名:
我认为在time()函数中需要一个返回
function time() {
return (new Date()).getTime()
}
我认为在hiddenTxt
字段中还需要一个value=[time value]
属性。可能是POST参数当前为空字符串的原因。您的“request.getParameter(“hiddenTxt”)返回一个空字符串(“”),您正在将其解析为长值。所以你得到了NumberFormatException
让我们从JSP开始..尝试以下操作:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function time()
{
document.getElementById("hiddenTxt").value = Date.now();
document.getElementById('theForm').submit();
}
</script>
</head>
<body>
<form action="TimestampClass" method="post" name="theForm">
Name: <input type="text" name="nameTxt">
<input type="hidden" name="hiddenTxt" id="hiddenTxt" onsubmit="time()">
<input type="button" value="Submit" onclick="time();">
</form>
</body>
这样你可以摆脱如果NF异常 “request.getParameter(“hiddenTxt”)”的输出是什么。。?在这里发布您的异常。好的,它清楚地表明您的“request.getParameter(“hiddenTxt”)”返回一个空字符串(“”),您正在将其解析为一个长值。所以你得到了NumberFormatException。不幸的是,Javascript时间也向我显示了服务器时间,可能是你在同一台机器上运行客户机和服务器,或者是在同一个时间区域,而不是。服务器在亚马逊,我的时区不同
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
<script>
function time()
{
document.getElementById("hiddenTxt").value = Date.now();
document.getElementById('theForm').submit();
}
</script>
</head>
<body>
<form action="TimestampClass" method="post" name="theForm">
Name: <input type="text" name="nameTxt">
<input type="hidden" name="hiddenTxt" id="hiddenTxt" onsubmit="time()">
<input type="button" value="Submit" onclick="time();">
</form>
</body>
PrintWriter out = response.getWriter();
long timeStamp = 0;
String timeVal = request.getParameter("hiddenTxt");
try{
if(("").equals(timeVal) && timeVal != null){
timeStamp = Long.parseLong(timeVal);
}
out.println(name);
out.println(timeStamp);
}catch(NumberFormatException e){
e.printStackTrace();
}