Pass JSP date to Servlet

Asked

Viewed 83 times

0

I have a Datepicker bootstrap as below, and I have a button that must pass the date selected in datepicker to the Servlet that will use this date as parameter for a mysql search. How can I pass this date to Servlet? I have tried with several examples I found on the web, but without success.

Knob:

<img src="IMG/check.png" style="cursor:pointer" onclick="UpdateGrafico();" />

Datepicker:

<!--datepicker1 -->
   <div class="container" style="width: 100%; 
        margin: 0.5em auto; 
        padding: 1em;
        border: 1px solid greenyellow; 
        border-radius: 0.5em">
    <div class="row">
        <div class='col-sm-3' style="margin-left:10px;">
            <div class="form-group">
                <div class='input-group date' id='datetimepicker1' >
                    <input type='text' class="form-control" id="d"/>
                    <span class="input-group-addon">
                        <span class="glyphicon glyphicon-calendar"></span>
                    </span>
                </div>
            </div>
        </div> 

        <img src="IMG/check.png" style="cursor:pointer" onclick="UpdateGrafico();" />
        <script type="text/javascript">
            $i(function () {
                $i('#datetimepicker1').datetimepicker({
         format: 'DD/MM/YYYY HH:mm'
    });

            });
        </script>

    </div>
</div>
<!--datepicker1 -->
  • You want to pass via AJAX?

  • Yes via ajax...

  • You must have already created Servlet, so just send the desired information to the Servlet url. There in Servlet you can use a request.getParameter

  • Yes I already have Servlet, but I can’t pass these date parameters to it.

  • How’s that function you put there on onclick?

  • I put the function there....

Show 1 more comment

1 answer

0

function UpdateGrafico(){

   document.getElementById("loading").style.display = "block";      
       var queryObject="";
       var queryObjectLen="";  
       var processed_json = new Array(); 
       var processed_temp = new Array();
    $j(function () {

                //---------------------

      $j.ajax({  
     data: "",  
     dataType: 'json',  
     url: './GetValores',  
     type: 'POST',  
     success: function(data){  

          queryObject = eval('(' + JSON.stringify(data) + ')');
          queryObjectLen = queryObject.jsonArray.length;


   for(var i=0; i < queryObjectLen;i++){ 

                    var corrente = queryObject.jsonArray[i].valorCorrente;
                    var id = +new Date(queryObject.jsonArray[i].pk);
                    var tmp = queryObject.jsonArray[i].temp;




                    processed_json.push([id, corrente]);  
                    processed_temp.push([id,tmp]);

                }

                //alert(ini);
                //alert(fim);
               if(queryObjectLen === 0){
                    alert("Sem dados para o período pesquisado!");
                }

                //---------------------
                     Highcharts.setOptions({global: { useUTC: false } });     
                    // draw chart

                    $j('#container').highcharts({

                    chart: {
                        type: "spline",
                        zoomType: 'x',
                        backgroundColor: '#d8f5ac'
                    },
                    title: {
                        text: "Current data"
                    },                    
                    xAxis: {
                        type: 'datetime',
                        // max :ini /** Date in timestamp for the end the day */,
                        // min : fim/** Date in timestamp for the beginning the day */,
                        //categories: 'datetime',

                        allowDecimals: true,
                        tickInterval: 3600 * 1000,
                        dateTimeLabelFormats: {
            day: '%d/%b '

        },

                        title: {
                            text: null
                        }

                    },
                    yAxis: {
                        title: {
                            text: "Value (A)"
                        }
                    },
                    plotOptions: {
            series: {
                turboThreshold: 0, //libera para plotar mais de 1000 pontos no chart

            }
        },

                    series: [{
                    name: 'Valor de corrente',
                        data: processed_json,
                        color: '#FF0000'
                    },{
                        name: 'Temperatura',
                        data: processed_temp   
                    }

            ]


                }); 
             document.getElementById("loading").style.display = "none";//oculta div de loading
           //----------
            },  

     error: function() {  

       alert("Ocorreu um erro na requisição ajax");  
     }  
   });
           //----------

        });
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.