如何发送客户端's时间戳到Servlet
How to send Client's Timestamp to Servlet?
我试图获得提交HTML表单时客户机机器的时间。目前我在servlet中使用这个,但它会产生时区问题。因此,为了摆脱这种情况,我决定在客户端机器中找到Timestamp
,并在提交表单时将其发送到服务器。下面是我的代码
<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>
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 timeStamp = Long.parseLong(request.getParameter("hiddenTxt"));
PrintWriter out = response.getWriter();
out.println(name);
out.println(timeStamp);
}
// <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>
}
然而,它没有工作,NumberFormatException
被抛出时间戳字段。那么,我如何才能正确地将客户机的时间戳发送给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)
为表单元素添加submit回调
<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()
功能中需要一个return
。
function time() {
return (new Date()).getTime()
}
我认为你还需要一个value=[time value]
属性在你的hiddenTxt
字段。这可能是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>
这将设置客户端时间并在提交按钮点击时提交表单。
转换为long之前,检查是否为空:
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();
}
这样你就可以摆脱NFException。
相关文章:
- 如何每10.6秒从时间戳增加+1
- 使用AngularJS从时间戳中获取工作日
- 将时间戳转换为java中的ISO860,
- Javascript Unix时间戳转换
- JavaScript JSON关联对象键和值都作为日期时间戳
- 时间戳与mongoskin如何
- 用时间戳获取两个不同日期之间的时间差
- JavaScript到PHP的时间戳失败,增加了大约一个半月的时间
- 转换unix时间戳转换
- 带有unix时间戳的d3.js平分线
- 当转换为Python日期时间时,Node的时间戳落后3分钟
- 如何检索AMD的XHR响应代码(+时间戳)'大型Dojo
- javascript时间戳插入字符串变量
- Moment.js unix时间戳,以分钟为单位显示以前的时间
- 事件时间戳谷歌分析
- 时间戳到文本
- Unix时间戳从现在输出为1970
- 如何使用javascript在嵌入式youtube视频上创建可点击的时间戳链接
- 如何在时间标签和相同的类中以UTC格式重新格式化ISO 8601时间戳,并通过JavaScript根据用户区域设置和时区
- 如何发送客户端's时间戳到Servlet