提问人:AzedClone 提问时间:3/10/2023 最后编辑:Roman CAzedClone 更新时间:3/12/2023 访问量:23
在 Servlet 中多次重新加载 Web 应用程序时无法加载数据
Can't load data when reload web app many times in Servlet
问:
我遇到了一个问题,当我多次重新加载我的 Web 应用程序时,它无法调用 Servlet 来加载数据
这是我使用 AJAX 从 DB 加载数据以调用 Servlet 的函数:
function loadData(type) {
$.post('../../loadData?type=' + type, function (data) {
if (type == "accounts") {
$('#accounts > tbody').empty();
$('h2.data-number.data-account').html(data.length);
$.each(data, function (i, val) {
const row = $('<tr>').append($('<th scope="row">').text(i + 1))
.append($('<td>').text(val.accountID))
.append($('<td>').text(val.username))
.append($('<td>').text(val.name))
.append($('<td>').text(val.email))
.append($('<td>').text(val.phone))
.append($('<td>').text((val.role == 1) ? 'Doctor' : 'Patient'));
$('#accounts > tbody').append(row);
});
} else if (type == "doctors") {
$.post('../../loadData?type=' + type, function (data) {
var aEdit = $('<a class="editDoc" data-bs-toggle="modal" data-bs-target="#form-editDoc" href=""><i class="fa-solid fa-pen text-dark me-3"></i></a>');
var aDeleteDoc = $('<a class="deleteDoc" href=""><i class="fa-solid fa-trash-can text-danger"></i></a>');
$('#doctors > tbody').empty();
$('h2.data-number.data-doctor').html(data.length);
$.each(data, function (i, val) {
const row = $('<tr>').append($('<th scope="row">').text(i + 1))
.append($('<td>').text(val.username))
.append($('<td>').text(val.name))
.append($('<td>').text(val.email))
.append($('<td>').text(val.phone))
.append($('<td class="action">'));
row.find('td.action')
.append(aEdit.clone().attr({'href': '../../loadData?username=' + val.username}))
.append(aDeleteDoc.clone().attr({'href': '../../manageDoctor?username=' + val.username + '&type=delete'}));
$('#doctors > tbody').append(row);
});
});
} else if (type == "patients") {
$.post('../../loadData?type=' + type, function (data) {
var aView = $('<a class="viewPat" data-bs-toggle="modal" data-bs-target="#form-viewPat" href=""><i class="fa-solid fa-eye text-primary me-3"></i></a>');
var aDeletePat = $('<a class="deletePat" href=""><i class="fa-solid fa-trash-can text-danger"></i></a>');
$('#patients > tbody').empty();
$('h2.data-number.data-patient').html(data.length);
$.each(data, function (i, val) {
const row = $('<tr>').append($('<th scope="row">').text(i + 1))
.append($('<td>').text(val.username))
.append($('<td>').text(val.name))
.append($('<td>').text(val.email))
.append($('<td>').text(val.phone))
.append($('<td class="action">'));
row.find('td.action')
.append(aView.clone().attr({'href': '../../loadData?username=' + val.username}))
.append(aDeletePat.clone().attr({'href': '../../managePatient?username=' + val.username + '&type=delete'}));
$('#patients > tbody').append(row);
})
})
} else if (type == "blogs") {
$.post('../../loadData?type=' + type, function (data) {
var aEdit = $('<a class="editBlog" data-bs-toggle="modal" data-bs-target="#form-editBlog" href=""><i class="fa-solid fa-pen text-dark me-3"></i></a>');
var aDeleteBlog = $('<a class="deleteBlog" href=""><i class="fa-solid fa-trash-can text-danger"></i></a>');
$('#blogs > tbody').empty();
$('h2.data-number.data-blog').html(data.length);
$.each(data, function (i, val) {
const row = $('<tr>')
.append($('<th scope="row">').text(i + 1))
.append($('<td>').text(val.title))
.append($('<td>').text(val.categoryName))
.append($('<td>').text(val.authorName))
.append($('<td>').text(val.createdDate))
.append($('<td>').html((val.status == 1 ? 'Approved<i class="fa-solid fa-circle-check text-success ms-2"></i>' : 'Pending<i class="fa-solid fa-circle-pause text-danger ms-2"></i>')))
.append($('<td>').text(val.publishedDate))
.append($('<td class="action">'));
row.find('td.action')
.append(aEdit.clone().attr({'href': '../../loadData?blogID=' + val.blogID}))
.append(aDeleteBlog.clone().attr({'href': '../../manageBlog?blogID=' + val.blogID + '&type=delete'}))
$('#blogs > tbody').append(row);
})
})
} else if (type == "categories") {
$.post('../../loadData?type=' + type, function (data) {
$('#categoryLabel').empty();
$('#categoryBlogLabel').empty();
$.each(data, function (i, val) {
$('#categoryLabel').append($('<option value="' + (i + 1) + '">').text(val.categoryName))
$('#categoryBlogLabel').append($('<option value="' + (i + 1) + '">').text(val.categoryName))
})
})
} else if (type == "services") {
$.post('../../loadData?type=' + type, function (data) {
var aEdit = $('<a class="editService" data-bs-toggle="modal" data-bs-target="#form-editService" href=""><i class="fa-solid fa-pen text-dark me-3"></i></a>');
$('#services > tbody').empty();
$.each(data, function (i, val) {
const row = $('<tr>').append($('<th scope="row">').text(i + 1))
.append($('<td>').text(val.name))
.append($('<td>').text(val.price))
.append($('<td>').html((val.status == 1 ? 'Available<i class="fa-solid fa-circle-check text-success ms-2"></i>' : 'Disabled<i class="fa-solid fa-ban text-danger ms-2"></i>')))
.append($('<td class="action">'));
row.find('td.action')
.append(aEdit.clone().attr({'href': '../../loadData?serviceID=' + val.serviceID}))
$('#services > tbody').append(row);
})
})
} else if (type == "medicines") {
$.post('../../loadData?type=' + type, function (data) {
var aEdit = $('<a class="editMedicine" data-bs-toggle="modal" data-bs-target="#form-editMedicine" href=""><i class="fa-solid fa-pen text-dark me-3"></i></a>');
$('#medicines > tbody').empty();
$.each(data, function (i, val) {
const row = $('<tr>').append($('<th scope="row">').text(i + 1))
.append($('<td>').text(val.name))
.append($('<td>').text(val.brand))
.append($('<td class="action">'));
row.find('td.action')
.append(aEdit.clone().attr({'href': '../../loadData?medicineID=' + val.medicineID}))
$('#medicines > tbody').append(row);
})
})
}
});
}
我的 Servlet:
AccountDAO accountDAO = new AccountDAO();
BlogDAO blogDAO = new BlogDAO();
AppointmentDAO appointmentDAO = new AppointmentDAO();
ServiceDAO serviceDAO = new ServiceDAO();
MedicineDAO medicineDAO = new MedicineDAO();
ExaminationDAO examinationDAO = new ExaminationDAO();
PrescriptionDAO prescriptionDAO = new PrescriptionDAO();
String type = request.getParameter("type");
String username = request.getParameter("username");
String blogID = request.getParameter("blogID");
String appointmentID = request.getParameter("appointmentID");
String serviceID = request.getParameter("serviceID");
String medicineID = request.getParameter("medicineID");
String examinationID = request.getParameter("examinationID");
String prescriptionID = request.getParameter("prescriptionID");
if (type != null) {
switch (type) {
case "accounts": {
List<Account> accounts = accountDAO.getAccounts();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(accounts));
break;
}
case "doctors": {
List<Account> doctors = accountDAO.getAccountsByRole(1);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(doctors));
break;
}
case "patients": {
List<Account> patients = accountDAO.getAccountsByRole(2);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(patients));
break;
}
case "blogs": {
List<Blog> blogs = blogDAO.getBlogs();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(blogs));
break;
}
case "categories": {
List<Category> categories = blogDAO.getCategories();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(categories));
break;
}
case "appointments": {
HttpSession session = request.getSession();
Account currentAccount = (Account) session.getAttribute("currentAccount");
if (currentAccount.getRole() == 2) {
List<Appointment> appointments = appointmentDAO.getAppointmentsForPat(currentAccount.getAccountID());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(appointments));
} else if (currentAccount.getRole() == 1) {
List<Appointment> appointments = appointmentDAO.getAppointmentsForDoc(currentAccount.getAccountID());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(appointments));
}
break;
}
case "slots": {
String bookedDate_raw = request.getParameter("bookedDate");
String doctorID = request.getParameter("doctorID");
List<Integer> slots = appointmentDAO.busySlot(Date.valueOf(bookedDate_raw), Integer.parseInt(doctorID));
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(slots));
break;
}
case "services": {
List<Service> services = serviceDAO.getServices();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(services));
break;
}
case "medicines": {
List<Medicine> medicines = medicineDAO.getMedicines();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(medicines));
break;
}
case "examinations": {
HttpSession session = request.getSession();
Account currentAccount = (Account) session.getAttribute("currentAccount");
if (currentAccount.getRole() == 2) {
List<Examination> examinations = examinationDAO.getExaminationsForPat(currentAccount.getAccountID());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(examinations));
} else if (currentAccount.getRole() == 1) {
List<Examination> examinations = examinationDAO.getExaminationsForDoc(currentAccount.getAccountID());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(examinations));
}
break;
}
case "prescriptions": {
HttpSession session = request.getSession();
Account currentAccount = (Account) session.getAttribute("currentAccount");
if (currentAccount.getRole() == 2) {
List<Prescription> prescriptions = prescriptionDAO.getPrescriptionsForPat(currentAccount.getAccountID());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(prescriptions));
} else if (currentAccount.getRole() == 1) {
List<Prescription> prescriptions = prescriptionDAO.getPrescriptionsForDoc(currentAccount.getAccountID());
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(prescriptions));
}
break;
}
default:
break;
}
} else if (username != null) {
Account account = accountDAO.getAccount(username);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
String accountJson = gsonBuilder.create().toJson(account);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(accountJson);
} else if (blogID != null) {
Blog blog = blogDAO.getBlog(Integer.parseInt(blogID));
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
String blogJson = gsonBuilder.create().toJson(blog);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(blogJson);
} else if (appointmentID != null) {
Appointment appointment = appointmentDAO.getAppointment(Integer.parseInt(appointmentID));
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
String appointmentJson = gsonBuilder.create().toJson(appointment);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(appointmentJson);
} else if (serviceID != null) {
Service service = serviceDAO.getService(Integer.parseInt(serviceID));
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(service));
} else if (medicineID != null) {
Medicine medicine = medicineDAO.getMedicine(Integer.parseInt(medicineID));
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(new Gson().toJson(medicine));
} else if (examinationID != null) {
Examination examination = examinationDAO.getExamination(Integer.parseInt(examinationID));
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
String examinationJson = gsonBuilder.create().toJson(examination);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(examinationJson);
} else if (prescriptionID != null) {
Prescription prescription = prescriptionDAO.getPrescription(Integer.parseInt(prescriptionID));
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
String prescriptionJson = gsonBuilder.create().toJson(prescription);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().write(prescriptionJson);
}
}
和错误消息:
我不知道我面临什么问题,请帮助我或告诉我我有什么问题。
答: 暂无答案
评论