gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 02:43:46 25 4
gpt4 key购买 nike

我必须在我的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 在客户端运行,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>
  • Ajax 。
  • 使用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 由浏览器执行。

    关于javascript - 如何在jsp中将javascript变量传递给java scriptlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47393053/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com