aspose file tools*
The moose likes Servlets and the fly likes question related to out.println( ) and HTML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "question related to out.println( ) and HTML" Watch "question related to out.println( ) and HTML" New topic
Author

question related to out.println( ) and HTML

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
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

Joined: Sep 16, 2003
Posts: 224
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).


"Ignorance is bliss"
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sharad Agarwal
Ranch Hand

Joined: Sep 11, 2002
Posts: 167
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.


Alco-Haul: We move spirits.
Demented Deliberations of a Dilettante
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61421
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question related to out.println( ) and HTML