0

I am using PrintWriter in one my servlet to flush html like below

PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("</head>");
out.println("<body>");
out.println("</body>");
out.println("</html>");

Say now i want to insert one line in html body. Is there a way to insert the data at some specific line at later stage or we have to go sequentially.

maverick
  • 1,458
  • 5
  • 20
  • 28

1 Answers1

1

It's not possible to insert lines.

If you really want to produce HTML pages this way (via PrintWriter and println()), you can create a String that contains something like a Template:

String template = "<html><head></head> <body> <p>__PLACEHOLDER1__</p> <p>__PLACEHOLDER2__</p> ... </body></html>";

During execution you replace the placeholders whenever you want.

Another way: you clean the buffer as described here and restart the output over all.

But I suggest to create the HTML via JSP, JSF or something like that

Community
  • 1
  • 1
JimHawkins
  • 4,843
  • 8
  • 35
  • 55