• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question related to out.println( ) and HTML

 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have seen in many coding examples


PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head>");
pw.println("</head>");
pw.println("<body>");
pw.println("<h3>Hello World!</h3>");
pw.println("</body>");
pw.println("</html>");

is it not possible to combine all html related code in single out.println()
 
Mishra Anshu
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Definately.
If you are not making the html generated through the JSP (as compared with Servlets), it is better to use StringBuffer of the related Html code. And just use pw.println(theStringBuffer).
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's certainly possible but it's usually more clear if you do it this way - especially if you have a lot of HTML. It also makes it easier to add and remove lines without breaking the code.
 
Sharad Agarwal
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And, if it is HTML that you are trying to return to the client, then perhaps a JSP is the better vehicle. It will save you from repeatedly typing in the println() statements.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sharad makes a good point. One very common, and good, pattern is to let the servlet perform all the Java processing, place whatever data is relevant onto the request as scoped variables, and then forward to a JSP page to render the view.

This takes advantage of the best of both worlds: Java processing is best performed in Java classes (rather than in a JSP), and HTML generation is much easier within a JSP.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic