I am developing a Java servlet class where the error will be displayed onto the page after the servlet class is accessed. However, I am facing a problem where the println function of my PrintWriter does not actually print a new line. The else section of the code does not print a new line. I've tried to follow this solution but to no avail. How do I fix this?
Thank you!
Here's my code:
@Named("UploadServlet")
public class UploadServlet extends HttpServlet{
private final UserAccessor userAccessor;
public UploadServlet(UserAccessor userAccessor){
this.userAccessor = userAccessor;
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
...
try{
//Parse the request to get file items
List<FileItem> fileItems = upload.parseRequest(request);
// Process the uploaded items
Iterator<FileItem> iter = fileItems.iterator();
while(iter.hasNext()){
FileItem item = iter.next();
if(!item.isFormField()){
String content = item.getString();
StringReader sReader = new StringReader(content);
Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(sReader);
Hashtable<String, String> errors = new Hashtable<String, String>();
for(CSVRecord record : records){
...
}
if(errors.isEmpty()){
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html><head>");
out.println("<meta name=\"decorator\" content=\"atl.admin\" />");
out.println("<title>Bulk User Creation Tool</title></head>");
out.println("<body style=\"text-align: center;\"><h3>User(s) created successfully!</h3></body></html>");
out.close();
}
else{
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE html>");
out.println("<html><head>");
out.println("<meta name=\"decorator\" content=\"atl.admin\" />");
out.println("<title>Bulk User Creation Tool</title></head>");
out.println("<body style=\"text-align: center;\">");
errors.forEach((k,v) -> {
out.println(k + ": " + v + "\r\n");
});
out.println("</body></html>");
out.close();
}
}
}
}
...
}
}
" I guess. – Christian Jun 25 '21 at 11:26