我有一个问题,我在使用servlet在会话中存储变量,但是当我尝试在另一个servlet上获取相同的变量时,我得到了这个错误:
java.lang.NumberFormatException: For input string: ""
这是我存储信息的servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String appointmentId = request.getParameter("selectEditPatientAppointment");
System.out.println(appointmentId);
request.getSession().setAttribute("appointmentToEdit", appointmentId);
response.sendRedirect("doctorEditAppointmentForm.jsp");
}
这是我尝试获取变量的那个,我得到了错误:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Date date = convertStringToDate(request.getParameter("appointmentDate"));
String time = request.getParameter("appointmentTime");
String doctorId = request.getParameter("selectEditDoctor");
String patientId = request.getParameter("selectEditPatient");
String description = request.getParameter("description");
String results = request.getParameter("results");
String test = request.getParameter("appointmentToEdit");
System.out.println("Esto: " + test);
int appointmentId = Integer.parseInt(request.getParameter("appointmentToEdit"));
ListService service = new ListService();
Doctor doctor = service.getDoctor(doctorId);
Patient patient = service.getPatient(patientId);
Set patientappointmentses = new HashSet(patient.getPatientrecord().getPatientappointmentses());
Patientrecord patientRecord = new Patientrecord(patient,patientappointmentses);
patientRecord.setPatientRecordId(patientId);
RegisterService registerService = new RegisterService();
registerService.patientRecordExists(patientRecord);
PatientappointmentsId patientappointmentsId = new PatientappointmentsId(appointmentId,patientId);
Patientappointments patientAppointments = new Patientappointments(patientappointmentsId,doctor,patientRecord,date,time,description,results);
try {
EditService editService = new EditService();
editService.edit(patientAppointments, patientappointmentsId);
response.sendRedirect("doctorAppointmentEdited.jsp");
} finally {
out.close();
}
}
变量名是appointmentToEdit
,当我打印到控制台时,我在第一个servlet上正确获取了信息。
感谢您的帮助。
答案 0 :(得分:2)
您存储在会话中的variable
所以
你应该通过这种方式得到变量
request.getSession().getAttribute("appointmentToEdit")
希望这有帮助