如何在jsp中将javascript变量传递给java scriplet

时间:2017-11-20 13:22:06

标签: javascript java jsp variables scriplets

我必须在我的java scriplet中传递此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>

1 个答案:

答案 0 :(得分:2)

您无法直接拨打服务器电话。您需要发出服务器请求。

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>
    
  2. 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. 的Ajax。
    2. 使用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由浏览器执行。