Исках да покажа съобщение за грешка в jsp файла, когато полетата за въвеждане са нулеви или празни. Получих съобщение за грешка в сървлета, но не можах да го покажа в jsp файла. Когато щракна върху запазване или актуализиране за студент и информацията му е празна, бутонът за изпращане не може да бъде възприет.
Как мога да го поправя?
Ето моята страница за сервлети и jsp.
private void addStudent(HttpServletRequest request, HttpServletResponse response) throws Exception {
// read student info from form data
String firstName = request.getParameter("studentNameInput");
String lastName = request.getParameter("studentLastNameInput");
String email = request.getParameter("studentEmailInput");
// Image
InputStream inputStream = null; // input stream of the upload file
Part filePart = request.getPart("imageFileUpload");
if(firstName!= "" && lastName!="" && email!="" && filePart.getSize()!=0) {
if (filePart != null && filePart.getSize() > 0) {
// prints out some information for debugging
System.out.println(filePart.getName());
System.out.println(filePart.getSize());
System.out.println(filePart.getContentType());
// obtains input stream of the upload file
inputStream = filePart.getInputStream();
}
// Convert Byte from InputStream
//byte[] image = IOUtils.toByteArray(inputStream);
// create a new student object
Student student = new Student(firstName, lastName, email);
// add the student to the database
studentUtil.addStudent(student,inputStream);
// send back to main page (the student list)
// send back to main page (the student list)
// SEND AS REDIRECT to avoid multiple-browser reload issue
response.sendRedirect(request.getContextPath() + "/StudentServlet?command=LIST");
}else {
checkInputField(request, response, firstName, lastName, email, filePart);
}
}
checkInputField
private void checkInputField(HttpServletRequest request, HttpServletResponse response, String firstName,
String lastName, String email, Part filePart) throws IOException, ServletException {
// First Name
if(firstName.equals("")) {
// place student in the request attribute
String errorMessage = "Please enter first name";
request.setAttribute("errorMessageFirstName", errorMessage);
}
// Last Name
if(lastName.equals("")) {
String errorMessage = "Please enter last name";
request.setAttribute("errorMessageLastName", errorMessage);
}
// Email
if(email.equals("")) {
String errorMessage = "Please enter email address";
request.setAttribute("errorMessageEmail", errorMessage);
}
// Image
if(filePart.getSize()==0) {
String errorMessage = "Please upload image file";
request.setAttribute("errorMessageFilePart", errorMessage);
}
// send back to main page (the student list)
// SEND AS REDIRECT to avoid multiple-browser reload issue
response.sendRedirect(request.getContextPath() + "/StudentServlet?command=LIST");
}
jsp файл
<form action="StudentServlet" method="POST" enctype="multipart/form-data" name="studentForm" id="studentForm">
<!-- ADD FORM -->
<c:if test='${param.command != "LOAD"}'>
<input type="hidden" name="command" value="ADD" />
</c:if>
<!-- UPDATE FORM -->
<c:if test='${param.command == "LOAD"}'>
<input type="hidden" name="command" value="UPDATE" />
<input type="hidden" name="ID" value="${param.ID}" />
</c:if>
<div class="form-group row">
<label for="studentName" class="col-sm-2 col-form-label">Student Name:</label>
<div class="col-sm-10">
<c:if test='${param.command == "LOAD"}'>
<input type="text" class="form-control" id="studentNameInput" name="studentNameInput" value="${student.firstName}">
</c:if>
<c:if test='${param.command != "LOAD"}'>
<input type="text" class="form-control" id="studentNameInput" name="studentNameInput" placeholder="Enter Student Name" required>
</c:if>
<c:if test="${not empty errorMessageFirstName}">
<div class="alert alert-danger col-sm-3">
${errorMessageFirstName}
</div>
</c:if>
</div>
</div>
<!-- <button type="submit" class="btn btn-primary">Submit</button>-->
<c:if test='${param.command == "LOAD"}'>
<tr>
<button type="submit" class="btn btn-primary">Update</button>
</tr>
</c:if>
<c:if test='${param.command != "LOAD"}'>
<tr>
<button type="submit" class="btn btn-primary">Save</button>
</tr>
</c:if>
</form>