2017-06-28 67 views
0

我已經寫了一個jsp文件和servlet。在jsp中我使用了一個表單內的數據列表,並且我想將這個用戶輸入傳遞給servlet.I已經發布了下面的代碼。如何從jsp傳遞數據列表值到servlet

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><br><br> 

    </center></form> 

    <div style="width: 400px; height: 400px"> 
     <canvas id="myChart" width="1000" height="1000" ></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 doGet(HttpServletRequest request, HttpServletResponse response) 
     throws ServletException, IOException { 

     response.setContentType("text/html;charset=UTF-8"); 

     String website = (String)request.getParameter("website"); 

但這不是working.Can有人幫我解決這個問題?

+0

你提交後從request.getParameter(「website」)''得到了什麼? –

+0

我正在計算所選網站和繪圖圖表的響應時間,通過使用這些數據,當我把這個網站變量,並提請它顯示錯誤的圖形。但是如果我使用'String website =「https://www.google.lk/」;'它顯示真實的圖 – mahi

回答

1

所有你需要的形式提交給哪條路徑被映射到NewServlet1

<form action="NewServlet1" method="Get" > 
    <center> 
      <input type="text" name="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> 
    </center> 
     <% 
     String website= request.getParameter("website"); // it is not needed 
     request.setAttribute("website",website);   // it is not needed 
     %> 
    <input type="submit"/> <!--need to add submit button to submit it to the servlet--> 
</form> 

現在你的servlet代碼的servlet首先是罰款是:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
    response.setContentType("text/html;charset=UTF-8"); 
    String website = (String)request.getParameter("website"); 
} 

注:需要寫website文本框中的某些值

或者如果您想獲得attribute之前存儲在request對象,然後使用RequestDispatcher

+0

這是不工作,因爲我想要 – mahi

+0

我怎麼才能知道你的要求是什麼? –

+0

我給你的方式來將jsp的值傳遞給servlet,如果你想要其他的東西,那麼請指定 –