如何将数据从servlet传递到jsp代码(表单外)?

时间:2017-06-30 04:38:28

标签: javascript java jsp servlets

我编写了jsp代码,通过从servlet获取数据来绘制图表。 我在表单旁边保留了“提交”按钮,并且没有绘制正确的图表。 但是,当我在窗体中调用一个按钮而不是图表时,我正在使用正确的数据获取数据表。所以我认为servlet Data已经传递到不在表单一侧的表单。

这是我的jsp代码

 <form  action="NewServlet1" method="Post" >
<center>
        <input type="text" name="website" list="website" placeholder="Enter your website">
        <datalist id="website">
        <option value="https://www.google.lk/">GOOGLE</option>
        <option value="https://www.yahoo.com/">YAHOO</option>
        <option value="https://www.hackerrank.com/">HACKER RANK</option>

        </datalist><br>
        <button id="button2"  name="button2" onclick="submit()" class="btn btn-primary">submit</button>
         <br><br><br>


    </center></form>

     <div style="width: 400px; height: 400px">
        <canvas id="myChart" width="10000" height="10000" ></canvas>
    </div>
<center> <div>
       <button id="button1"  name="button1" onclick="submit()" class="btn btn-primary">submit</button>
    </div></center>
    <script>
        function Test(today, time) {
            var ctx = new Chart(document.getElementById("myChart")

            , {

                type: 'line',
                data: {
                    labels: today,
                    datasets: [{
                            data: time,
                            label: "Web Service 1",
                            borderColor: "red",
                            fill: false
                        }
                    ]
                },
                options:
                        {
                            scales: {xAxes: [{display: true, scaleLabel: {display: true, labelString: 'date and time'}}], yAxes: [{display: true, ticks: {beginAtZero: true, steps: 100, stepValue: 50, max: 6000}}]},

                            hover: {intersect: false },
                            title: {display: true, text: 'response time of selected website'},
                              tooltips: { mode: 'nearest'}

                        }
            });
}
function submit()
{

    $.post("NewServlet1",
            {

            },
            function (data)
            {

                Test(data.today, data.time);
            });
}

这是我的servlet代码

 protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
       response.setContentType("text/html"); //1st line
       String website = request.getParameter("website"); //2nd line
        NewClass1 myclass1 = new NewClass1();
        NewClass2 myclass2 = new NewClass2();
        //String website1 = "https://www.google.lk/";    /*when I use this line instead of 1st
                                                         //and 2nd lines
                                                         //I'm getting perfect graph but I want
                                                         // to take user input not fixed input*/

    try {
        myclass2 = myclass1.myclass(website);


    } catch (Exception ex) {
        Logger.getLogger(NewServlet1.class.getName()).log(Level.SEVERE, null, ex);
    }
    int time[] = NewClass1.time1;
    String today[]=NewClass1.today;
    NewClass3 myclass3 = new NewClass3();
    myclass3.time=time;
    myclass3.today=today;
    Gson gson = new Gson();
    String jsonString = gson.toJson(myclass3);
    response.setContentType("application/json");
    response.getWriter().write(jsonString);


}

有人可以犯错误

1 个答案:

答案 0 :(得分:0)

  1. 您没有明确提及提交功能,而是使用Form的action参数。因此,当您从表单内部调用submit函数时,它会使用输入字段调用Action NewServlet1表单。
  2. 您从表单外部进行的调用会调用您已明确定义的函数Submit。因此,定义提交方法并最好使用表单操作和输入类型提交并不是一个好习惯。因此,只需指定具有所需操作的表单,或重命名该方法。