gpt4 book ai didi

java - 如何调用Ajax请求从数据库获取记录并在页面加载时使用servlet显示在jsp上

转载 作者:太空宇宙 更新时间:2023-11-04 09:41:27 24 4
gpt4 key购买 nike

我在数据库中有不同的字段,我需要在jsp中显示所有记录,但是当我向servlet发出ajax请求时,它将所有结果绑定(bind)到所有字段。我希望名字应该与名字绑定(bind),姓氏应该与姓氏绑定(bind)。目前它与名字和姓氏绑定(bind)。

我已尽一切努力来解决我的问题,但我认为问题出在我正在发出的ajax请求上。

   <html>
<head></head>
<body>
<div class="form-row">
<div class="col-md-9">
<div class="form-row pad-left">
<div class="col-md-6 mb-1">
<label for="validationCustomUsername"><b>Birth Name:</b>
<span id='birthName'></span>
</div>
<div class="col-md-6 mb-3">
<label for="validationCustomUsername"><b>Initiated Name:</b>
<span id='initiatedName'></span>
</div>
</div>

<!-- SECOND ROW STARTS HERE -->
<div class="form-row pad-left">
<div class="col-md-6 mb-1">
<label for="validationCustomUsername"><b>Place Of Birth: </b>
<span id='placeOfBirth'></span>
</div>
</div>
<div class="form-row pad-left">
<div class="col-md-6 mb-1">
<label for="validationCustomUsername"><b>Caste:</b>
<span id='caste'></span>
</div>

</body>

</html>

Servlet Code

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");

int userID = UserDetails.getInstance().getLastRegisteredID();
Connection con = DBConnection.connectDB();
String query = "Select * from PERSONS inner join
PersonsDetails on persons.PersonID=PersonsDetails.PersonId "
+ "where PERSONS.PersonID="+userID;
try {
ResultSet rs = DBConnection.getDBResultSet(con, query);
UserDetails user = new UserDetails();
while(rs.next()) {
String birthName =rs.getString("BirthName");
String initiatedName =rs.getString("InitiatedName");
String placeOfBirth =rs.getString("PlaceOfBirth");
String caste =rs.getString("Caste");



response.getWriter().write(birthName);
response.getWriter().write(initiatedName);
response.getWriter().write(placeOfBirth);
response.getWriter().write(caste);

}
} catch (SQLException e) {
e.printStackTrace();
}finally {
DBConnection.closeDBConnection(con);
}


}

Ajax Call
function userHomeDetails(){
var username = $('#username');
var url = "http://localhost:8080/IskconDevotteeMarriage/page/UserHome"
$(document).ready(function(){
var url=url
$.post("../UserHomeController", function(responseText) {
/*document.getElementById('birthName').innerHTML ="birthName"*/
$('#birthName').html(responseText);
$('#initiatedName').html(responseText);
$('#placeOfBirth').html(responseText);
$('#caste').html(responseText);
alert(responseText);
});
});

}

最佳答案

您可以使用 JSONObject ,首先添加 json jar 文件,然后在您的 servlet 类中创建 JSONObject 对象,如下所示:

JSONObject ob= new JSONObject(); 

然后输入您的参数,如下所示:

try {
ob.put("birthName",birthName);
ob.put("initiatedName",initiatedName);
ob.put("placeOfBirth",placeOfBirth);
ob.put("caste",caste);
} catch (JSONException e) {
e.printStackTrace();
}

现在,将上述参数传递给您的 ajax 调用,如下所示:

 response.getWriter().write(obj);

在你的ajax调用集中dataType: "json"和你的function(responseText)中你可以得到这个参数,如下所示:

document.getElementById('birthName').value = responseText.birthName;//setting values to span with id birthName
document.getElementById('initiatedName').value = responseText.initiatedName;
document.getElementById('placeOfBirth').value = responseText.placeOfBirth;
document.getElementById('caste').value = responseText.caste;

希望这有帮助!

关于java - 如何调用Ajax请求从数据库获取记录并在页面加载时使用servlet显示在jsp上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55938752/

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