0

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();
                    }
                }
            }
        }

        ...

    }
}
worrier
  • 81
  • 1
  • 7
  • For some clarification: Do you want the raw html text to have new lines or the result if you see it in the browser? If you are talking about the second case, you have to replace the "\r\n" in the loop over the errors by a "
    " I guess.
    – Christian Jun 25 '21 at 11:26
  • Hi @ChristianLutz, thanks for replying. For each error, I want it to be printed out in a new line. I solved this by adding

    tags to the beginning and end of the error
    – worrier Jun 27 '21 at 09:23

0 Answers0