-1

In my Servlet response, I would like to perform a line break for each println call. Currently the response text shows up all on one line (i.e. "first line second line"). I have experimented with "\n" and setting the setContentType to "text/html" with <br/> to no avail. Please advise.

Here is my test code:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
 response.setContentType("text/plain;charset=UTF-8");
 PrintWriter pw = response.getWriter();
 pw.println("first line");
 pw.println("second line");
}

Rob Hoth
  • 17
  • 2
  • return a String object then you can make a line break. – juju Apr 24 '20 at 22:11
  • I believe this is what you mean ... String s = new String("first line"); pw.println(s); s = "second line"; pw.println(s); This outputs both lines on the same line with no line break. – Rob Hoth Apr 24 '20 at 23:38

1 Answers1

0

I was able to figure it out. I had to do the following:

  1. In the JQuery call in your jsp file, you need to specify .html (i.e. $('#div').html(responseText);) and not .text
  2. In the Servlet, set response content type to response.setContentType("text/html;charset=UTF-8") and not "text/plain"
  3. Add
    to each println statement (e.g. pw.println("first line<br/>") where pw is the PrintWriter object)
Rob Hoth
  • 17
  • 2