簡體   English   中英

如何在JSP中將JavaScript變量傳遞給Java Scriplet

[英]How to pass javascript variable to java scriplet in jsp

我必須在我的Java腳本中傳遞此processID,以便我可以基於特定的ID查詢數據庫。 processID來自下拉列表的onchange函數。 但我無法做到這一點。

<script>
function load_division(processID){

    var id = processID.toString();

    <% String connectionUrl = "jdbc:oracle:thin:@PRA:1540:";
        String dbName = "System";
        String userId = "Prakhar";
        String password = "PS#5QW8";
        %> 
    <% String i%>= id; //giving Error
        PreparedStatement statement2 = null;
        ResultSet divSet = null;
    try {
        Connection connection1 = DriverManager.getConnection(
            connectionUrl + dbName, 
            userId, 
            password
        );
        statement2 = connection1.prepareStatement(
            "SELECT distinct ID, " + 
            "Name FROM process_group " + 
            "WHERE ID =" + i + 
            " ORDER BY name"
        );
        divSet = statement2.executeQuery();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
    }%>

    var x = document.getElementById("division");
    var option = document.createElement("option"); 
    var docfrag = document.createDocumentFragment();
    <% while (divSet.next()) {

        divSet.getInt("ID");%>
        docfrag.appendChild(new Option(
            "<%=divSet.getString("Name")%>",
            "<%=divSet.getInt("ID")%>"
        ));

        x.appendChild(docfrag);
    <%}%>    
}
</script>

您不能直接進行服務器調用。 您需要發出服務器請求。

javascript在客戶端播放,而JSP在服務器播放。

您需要的是必須發出服務器請求。 並將該字符串作為查詢參數發送。

實現此目的的兩個選擇。

  1. HTML表格

    Sample.html


    請介紹一下你自己

     <form action="SimpleFormHandler.jsp" method="get"> Name: <input type="text" name="firstName"> <input type="text" name="lastName"><br> Sex: <input type="radio" checked name="sex" value="male">Male <input type="radio" name="sex" value="female">Female <p> What Java primitive type best describes your personality: <select name="javaType"> <option value="boolean">boolean</option> <option value="byte">byte</option> <option value="char" selected>char</option> <option value="double">double</option> <option value="float">float</option> <option value="int">int</option> <option value="long">long</option> </select> <br> <input type="submit"> </form> </body> </html> 

SampleFormHandler.jsp

<html>
<body>

<%

// Grab the variables from the form.
  String firstName = request.getParameter("firstName");
  String lastName = request.getParameter("lastName");
  String sex = request.getParameter("sex");
  String javaType = request.getParameter("javaType");
%>
<%-- Print out the variables. --%>
<h1>Hello, <%=firstName%> <%=lastName%>!</h1>
I see that you are <%=sex%>. You know, you remind me of a
<%=javaType%> variable I once knew.

</body>
</html>
  1. 阿賈克斯

使用post發送數據並將結果放入div

$.ajax({
  url: "/YourServlet",
  type: "post",
  data: values,
  success: function(){
      alert("success");
       $("#result").html('submitted successfully');
  },
  error:function(){
      alert("failure");
      $("#result").html('there is error while submit');
  }   
}); 



不要混淆JSP和Java腳本存在於同一文檔(或文件)上。 是的,但是JSP部分在服務器端編譯,JavaScript由瀏覽器執行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM