java 将日期作为参数从 JSP 发送到 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6186284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
sending date as parameter from JSP to servlet
提问by ken
How do I send a date type as a parameter from JSP to servlet so as to insert to MySQL database. When I use getParameter() in the servlet, it shows that I'm using incompartible types. Actually I declared a variable of java.sql.Date type which I assign to the request parameter in the servlet but I get an error that the types are incompartible. I'm trying to submit date from JSP via servlet to MySQL. How is the parameter sent and received in such a case?
如何将日期类型作为参数从 JSP 发送到 servlet,以便插入 MySQL 数据库。当我在 servlet 中使用 getParameter() 时,它表明我使用的是不可比较的类型。实际上,我声明了一个 java.sql.Date 类型的变量,我将它分配给了 servlet 中的请求参数,但是我收到一个错误,指出类型是不可比较的。我正在尝试通过 servlet 从 JSP 向 MySQL 提交日期。这种情况下参数是如何发送和接收的?
回答by Jigar Joshi
Pass it as String
and parse it to server using SimpleDateFormat
to get the Date
back
将其作为String
并解析到服务器使用SimpleDateFormat
以获取Date
返回
For example:
例如:
Date date = new SimpleDateFormat("dd-MM-yyyy").parse("10-10-2010");