I have different fields in database and I need to show all the records in jsp but when I am making ajax request to servlet, it is binding all the results to all fields. I want firstname should be bind with firstname, lastname should be bind with lastname. Currently it is binding with frstname with firstanamelastname.
I've tried all level best to solve my problem but I think, the problem is with ajax request which I am making.
<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);
});
});
}