【问题标题】:How to pass javascript variable to java scriplet in jsp如何在jsp中将javascript变量传递给java scriptlet
【发布时间】:2018-05-03 17:36:18
【问题描述】:

我必须在我的 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>

【问题讨论】:

    标签: javascript java jsp variables scriptlet


    【解决方案1】:

    您不能直接进行服务器调用。您需要发出服务器请求。

    javascript 在客户端播放,JSP 在服务器端播放。

    你需要的是你必须发出一个服务器请求。并将该字符串作为查询参数发送。

    实现这一目标的两种选择。

    1. HTML 表单

      示例.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 由浏览器执行。

    【讨论】:

    • 为此 +1。我告诉开发人员的一件事是,您的 Javascript(以及与此相关的 HTML)是任何 Java 代码的输出 - 包括 JSP 中的 scriptlet。 Java 不仅在服务器端运行,Java 代码也在浏览器中呈现和执行 Javascript 之前运行。
    猜你喜欢
    • 2011-08-07
    • 2019-01-12
    • 1970-01-01
    • 2011-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多