How to fetch date from html form using jsp/servlet ( java ) and save it to database with column type date ??



Answered by Manish Kumar 

There are various ways of fetching Current Date and Time, 

  • Either you can use Javascript to fetch date/time from client side and send it to jsp/servlet page.
  • You can also fetch date/time from server side code.

Here is the example of fetching data from javascript and send it to jsp/servlet page using html form.

<html>
<head>
</head>
<body>

<form>
<h2> Getting Current Date From Html Page and sending Date to Jsp/Servlet using Html Form  </h2>

Enter name : <input type="text" name="name" /> <br/><br/>

Current date :<input type="text" id="mydate" name="date" />

<input type="submit" value="save" />

</form>

<script>

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; 
var yyyy = today.getFullYear();

if(dd<10) {
    dd='0'+dd

if(mm<10) {
    mm='0'+mm

today = mm+'/'+dd+'/'+yyyy;
//document.write(today);

document.getElementById("mydate").value = today;

</script>

</body>
</html>