我在数据库中有不同的字段,我需要显示jsp中的所有记录,但是当我向servlet发出ajax请求时,它将所有结果绑定到所有字段。我希望firstname应该与firstname绑定,lastname应该与lastname绑定。当前它与带有firstanamelastname的frstname绑定。
我尽了最大的努力来解决我的问题,但是我认为问题出在我提出的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);
});
});
}
答案 0 :(得分:1)
您可以使用JSONObject
,首先添加json jar
文件,然后在servlet
类中创建JSONObject
的对象,如下所示:
JSONObject ob= new JSONObject();
然后将您的parameter
如下所示:
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;
希望这会有所帮助!